Middle of three using minimum comparisons
Last Updated :
07 Jul, 2022
Given three distinct numbers a, b and c find the number with a value in middle.
Examples:
Input : a = 20, b = 30, c = 40
Output : 30
Input : a = 12, n = 32, c = 11
Output : 12
Simple Approach:
C++
// CPP program to find middle of three distinct
// numbers
#include <bits/stdc++.h>
using namespace std;
int middleOfThree(int a, int b, int c)
{
// Checking for b
if ((a < b && b < c) || (c < b && b < a))
return b;
// Checking for a
else if ((b < a && a < c) || (c < a && a < b))
return a;
else
return c;
}
// Driver Code
int main()
{
int a = 20, b = 30, c = 40;
cout << middleOfThree(a, b, c);
return 0;
}
Java
// Java program to find middle of
// three distinct numbers
import java.util.*;
class Middle
{
// Function to find the middle of three number
public static int middleOfThree(int a, int b,
int c)
{
// Checking for b
if ((a < b && b < c) || (c < b && b < a))
return b;
// Checking for a
else if ((b < a && a < c) || (c < a && a < b))
return a;
else
return c;
}
// driver code
public static void main(String[] args)
{
int a = 20, b = 30, c = 40;
System.out.println( middleOfThree(a, b, c) );
}
}
// This code is contributed by rishabh_jain
Python3
# Python3 program to find middle
# of three distinct numbers
def middleOfThree(a, b, c):
# Checking for b
if ((a < b and b < c) or (c < b and b < a)) :
return b;
# Checking for a
if ((b < a and a < c) or (c < a and a < b)) :
return a;
else :
return c
# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))
# This code is contributed by rishabh_jain
C#
// C# program to find middle of
// three distinct numbers
using System;
class Middle
{
// Function to find the middle of three number
public static int middleOfThree(int a, int b,
int c)
{
// Checking for b
if ((a < b && b < c) || (c < b && b < a))
return b;
// Checking for a
else if ((b < a && a < c) || (c < a && a < b))
return a;
else
return c;
}
// Driver code
public static void Main()
{
int a = 20, b = 30, c = 40;
Console.WriteLine( middleOfThree(a, b, c) );
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to find middle
// of three distinct numbers
function middleOfThree($a, $b, $c)
{
// Checking for b
if (($a < $b && $b < $c) or
($c < $b && $b < $a))
return $b;
// Checking for a
else if (($b < $a and $a < $c) or
($c < $a and $a < $b))
return $a;
else
return $c;
}
// Driver Code
$a = 20;
$b = 30;
$c = 40;
echo middleOfThree($a, $b, $c);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to find middle of three distinct
// numbers
function middleOfThree( a, b, c)
{
// Checking for b
if ((a < b && b < c) || (c < b && b < a))
return b;
// Checking for a
else if ((b < a && a < c) || (c < a && a < b))
return a;
else
return c;
}
// driver code
let a = 20, b = 30, c = 40;
document.write(middleOfThree(a, b, c));
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
But approach used above is not efficient because of extra comparisons, which can be easily minimized. In case first part is false, it will execute remaining half to check the condition. This problem remains same if we are checking for a also.
Better Approach (Needs less comparison):
C++
// CPP program to find middle of three distinct
// numbers
#include <bits/stdc++.h>
using namespace std;
// Function to find the middle of three numbers
int middleOfThree(int a, int b, int c)
{
// Compare each three number to find middle
// number. Enter only if a > b
if (a > b)
{
if (b > c)
return b;
else if (a > c)
return c;
else
return a;
}
else
{
// Decided a is not greater than b.
if (a > c)
return a;
else if (b > c)
return c;
else
return b;
}
}
// Driver Code
int main()
{
int a = 20, b = 30, c = 40;
cout << middleOfThree(a, b, c);
return 0;
}
Java
// Java program to find middle of
// three distinct numbers
import java.util.*;
class Middle
{
// Function to find the middle of three number
public static int middleOfThree(int a, int b,
int c)
{
// Compare each three number to find
// middle number. Enter only if a > b
if (a > b)
{
if (b > c)
return b;
else if (a > c)
return c;
else
return a;
}
else
{
// Decided a is not greater than b.
if (a > c)
return a;
else if (b > c)
return c;
else
return b;
}
}
// driver code
public static void main(String[] args)
{
int a = 20, b = 30, c = 40;
System.out.println(middleOfThree(a, b, c));
}
}
// This code is contributed by rishabh_jain
Python3
# Python3 program to find middle
# of three distinct numbers
# Function to find the middle of three numbers
def middleOfThree(a, b, c) :
# Compare each three number to find
# middle number. Enter only if a > b
if a > b :
if (b > c):
return b
elif (a > c) :
return c
else :
return a
else:
# Decided a is not greater than b.
if (a > c) :
return a
elif (b > c) :
return c
else :
return b
# Driver Code
a = 20
b = 30
c = 40
print( middleOfThree(a, b, c) )
# This code is contributed by rishabh_jain
C#
// C# program to find middle of
// three distinct numbers
using System;
class Middle
{
// Function to find the middle of three number
public static int middleOfThree(int a, int b,
int c)
{
// Compare each three number to find
// middle number. Enter only if a > b
if (a > b)
{
if (b > c)
return b;
else if (a > c)
return c;
else
return a;
}
else
{
// Decided a is not greater than b.
if (a > c)
return a;
else if (b > c)
return c;
else
return b;
}
}
// Driver code
public static void Main()
{
int a = 20, b = 30, c = 40;
Console.WriteLine(middleOfThree(a, b, c));
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to find middle
// of three distinct numbers
// Function to find the middle
// of three numbers
function middleOfThree( $a, $b, $c)
{
// Compare each three number
// to find middle number.
// Enter only if a > b
if ($a > $b)
{
if ($b > $c)
return $b;
else if ($a > $c)
return $c;
else
return $a;
}
else
{
// Decided a is not
// greater than b.
if ($a > $c)
return $a;
else if ($b > $c)
return $c;
else
return $b;
}
}
// Driver Code
$a = 20; $b = 30;
$c = 40;
echo middleOfThree($a, $b, $c);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to find middle of three distinct numbers
// Function to find the middle of three number
function middleOfThree(a, b, c)
{
// Compare each three number to find
// middle number. Enter only if a > b
if (a > b)
{
if (b > c)
return b;
else if (a > c)
return c;
else
return a;
}
else
{
// Decided a is not greater than b.
if (a > c)
return a;
else if (b > c)
return c;
else
return b;
}
}
let a = 20, b = 30, c = 40;
document.write(middleOfThree(a, b, c));
// This code is contributed by divyesh072019.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
This approach is efficient and having less number of comparisons. Outer IF loop will only be executed if a > b otherwise, outer ELSE will be executed.
Other Efficient Approach:
This approach is condensed version of the 1st approach.
(a>b and b>c) or (a<b and b<c) can also be decoded as a-b>0, b-c>0 or a-b<0,b-c<0 means the difference of a, b and b, c should be of same sign. So let x = a-b and y = b-c and if x, y have same sign then their result will be always positive. So b is middle element.
Similarly we can check for c and a.
let z = c - a and if (b < a and a < c) or (c < a and a < b) can be decoded as a-b>0 , c-a>0 or a-b < 0, c-a<0 means x*z > 0. So the middle element will be a.
C++
// CPP program to find middle of three distinct
// numbers
#include <bits/stdc++.h>
using namespace std;
// Function to find the middle of three number
int middleOfThree(int a, int b, int c)
{
// x is positive if a is greater than b.
// x is negative if b is greater than a.
int x = a - b;
int y = b - c; // Similar to x
int z = a - c; // Similar to x and y.
// Checking if b is middle (x and y both
// are positive)
if (x * y > 0)
return b;
// Checking if c is middle (x and z both
// are positive)
else if (x * z > 0)
return c;
else
return a;
}
// Driver Code
int main()
{
int a = 20, b = 30, c = 40;
cout << middleOfThree(a, b, c);
return 0;
}
Java
//java program to find middle of three distinct
// numbers
import java.util.*;
class Middle
{
// Function to find the middle of three number
public static int middleOfThree(int a, int b,
int c)
{
// x is positive if a is greater than b.
// x is negative if b is greater than a.
int x = a - b;
int y = b - c; // Similar to x
int z = a - c; // Similar to x and y.
// Checking if b is middle (x and y
// both are positive)
if (x * y > 0)
return b;
// Checking if c is middle (x and z
// both are positive)
else if (x * z > 0)
return c;
else
return a;
}
// driver code
public static void main(String[] args)
{
int a = 20, b = 30, c = 40;
System.out.println( middleOfThree(a, b, c) );
}
}
// This code is contributed by rishabh_jain
Python3
# Python3 program to find middle
# of three distinct numbers
# Function to find the middle of three number
def middleOfThree(a, b, c) :
# x is positive if a is greater than b.
# x is negative if b is greater than a.
x = a - b
# Similar to x
y = b - c
# Similar to x and y.
z = a - c
# Checking if b is middle (x and y
# both are positive)
if x * y > 0:
return b
# Checking if c is middle (x and z
# both are positive)
elif (x * z > 0) :
return c
else :
return a
# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))
# This code is contributed by rishabh_jain
C#
//C# program to find middle of three distinct
// numbers
using System;
class Middle
{
// Function to find the middle of three number
public static int middleOfThree(int a, int b,
int c)
{
// x is positive if a is greater than b.
// x is negative if b is greater than a.
int x = a - b;
// Similar to x
int y = b - c;
// Similar to x and y.
int z = a - c;
// Checking if b is middle (x and y
// both are positive)
if (x * y > 0)
return b;
// Checking if c is middle (x and z
// both are positive)
else if (x * z > 0)
return c;
else
return a;
}
// Driver code
public static void Main()
{
int a = 20, b = 30, c = 40;
Console.WriteLine( middleOfThree(a, b, c) );
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to find middle
// of three distinct numbers
// Function to find the
// middle of three number
function middleOfThree($a, $b, $c)
{
// x is positive if a
// is greater than b.
// x is negative if b
// is greater than a.
$x = $a - $b;
// Similar to x
$y = $b - $c;
// Similar to x and y.
$z = $a - $c;
// Checking if b is
// middle (x and y both
// are positive)
if ($x * $y > 0)
return $b;
// Checking if c is
// middle (x and z both
// are positive)
else if ($x * $z > 0)
return $c;
else
return $a;
}
// Driver Code
$a = 20;
$b = 30;
$c = 40;
echo middleOfThree($a, $b, $c);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// javascript program to find middle of
// three distinct numbers
// Function to find the middle of three number
function middleOfThree(a, b, c)
{
// x is positive if a is greater than b.
// x is negative if b is greater than a.
let x = a - b;
let y = b - c; // Similar to x
let z = a - c; // Similar to x and y.
// Checking if b is middle (x and y
// both are positive)
if (x * y > 0)
return b;
// Checking if c is middle (x and z
// both are positive)
else if (x * z > 0)
return c;
else
return a;
}
// Driver code
let a = 20, b = 30, c = 40;
document.write( middleOfThree(a, b, c) );
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Maximum and minimum of an array using minimum number of comparisons
Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons.Examples:Input: arr[] = {3, 5, 4, 1, 9}Output: Minimum element is: 1 Maximum element is: 9Input: arr[] = {22, 14, 8, 17, 35, 3}Output: Minimum element is: 3 Maximum
15+ min read
Find the minimised number using given operations
Given an array arr[] of n elements. In one operation you can pick two indices i and j, such that arr[i] ⥠arr[j] and replace the value of arr[i] with (arr[i] - arr[j]), the task is to minimize the values of the array after performing any number of such operations. Examples: Input: n = 3, arr[] = {3,
11 min read
How to find largest of three numbers using JavaScript ?
To find the largest of three numbers using JavaScript, we have multiple approaches. In this article, we are going to learn how to find the largest of three numbers using JavaScript. Below are the approaches to finding the largest of three numbers using JavaScript: Table of Content Using Conditional
3 min read
Maximize minimum element of an Array using operations
Given an array A[] of N integers and two integers X and Y (X ⤠Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ⤠Y. Examples: Input: N= 3, A[] = {1,
8 min read
Compare two Version numbers
A version number is a string that is used to identify the unique state of a software product. A version number looks like a.b.c.d, where a, b, etc are numbers, so the version number is a string in which numbers are separated by dots. These numbers generally represent the hierarchy from major to mino
15+ min read
How to check whether a number is in the range[low, high] using one comparison ?
This is simple, but interesting programming puzzle. Given three integers, low, high and x such that high >= low. How to check if x lies in range [low, high] or not using single comparison. For example, if range is [10, 100] and number is 30, then output is true and if the number is 5, then output
3 min read
Find triplet with minimum sum
Given an array of distinct integers arr[]. The task is to find a triplet(a group of 3 elements) that has the minimum sum.Note: The size of the array is always greater than two.Examples: Input : arr[] = {1, 2, 3, 4, -1, 5, -2} Output : -2 1 - 1 - 2 = -2 Input : arr[] = {5, 6, 0, 0, 1} Output : 1 0 +
10 min read
Smallest of three integers without comparison operators
Given three integers a, b, and c, the task is to find the smallest integer without using any comparison operators. Examples:Input: a = 2, b = 5, c = 7Output: 2Explanation: The smallest of the 3 integers is a, which is equal to 2.Input: a = 14, b = 13, c = 10Output: 10Explanation: The smallest of the
10 min read
Count ways to form minimum product triplets
Given an array of positive integers arr[], the task is to find how many distinct triplets (i, j, k) exist such that i < j < k and the product a[i] * a[j] * a[k] is minimum possible. Examples:Input: arr[] = [1, 3, 2, 3, 4]Output: 2Explanation: The minimum product is 1 Ã 3 Ã 2 = 6. It occurs in
13 min read
Array element with minimum sum of absolute differences | Set 2
Given an array arr[] consisting of N positive integers, the task is to find an array element X such that sum of its absolute differences with every array element is minimum. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Explanation: For element arr[0](= 1): |(1 - 1)| + |(2 - 1)| + |(3 - 1)| + |(4
7 min read