Last Updated: 8 Feb, 2021

Middle Of Three

Easy
Asked in company
MAQ Software

Problem statement

You have been given three distinct integers ‘X’, ‘Y’ and ‘Z’. You need to find the number with a value in the middle.

For example :

X = 4, Y = 6, Z = 2
Here the element with value in the middle is 4, because 2 < 4 < 6.

Note :

You need to try doing it using minimum comparisons.
Input Format :
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain three space-separated distinct integers ‘X’, ‘Y’, and ‘Z’.
Output Format :
For each test case, print the number with a value in the middle.

Output for every test case will be printed in a separate line.
Note :
You don’t need to print anything, It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 50
1 <= X, Y, Z <= 10^9

Where ‘T’ is the number of test cases.
Where ‘X’, ‘Y’, and ‘Z’ denote the distinct numbers.

Time limit: 1 sec

Approaches

01 Approach

The basic idea is to check for each element to be the middle element. Steps are as follows:

  1. Check for X
    • If Y is less than X AND simultaneously X is less than Z, OR, if Z is less than X AND simultaneously X is less than Y, then X is the middle element, hence return X.
  2. If the above conditions are not true, then check for Y
    • If X is less than Y AND simultaneously Y is less than Z, OR, if Z is less than Y AND simultaneously Y is less than X, then Y is the middle element, hence return Y.
  3. If these conditions are also not true, then neither X nor Y is the middle elements, which implies that Z is the middle element, hence return Z.

The number of comparisons: 8, in the worst case.

02 Approach

This time, the basic idea is to divide the problem into two parts and then compare the values. 

  1. Check whether X is greater than Y:
    • If true:
      • Check if Y is greater than Z, if yes then return Y.
      • Else check if X is greater than Z, if yes then return Z.
      • If both the above conditions are false, then X will be the middle element, so return X.
    • If false:
      • Check if X is greater than Z, if yes then return X.
      • Else check if Y is greater than Z, if yes then return Z.
      • If both the above conditions are false, then Y will be the middle element, so return Y.

This approach is efficient and uses fewer comparisons than the last one because only one of the outer If-else statements will be executed.

The number of comparisons: 3, in the worst case

03 Approach

In this approach, the basic idea is to use the basic properties of multiplication, that multiplication of two negative numbers is positive, multiplication of two positive numbers is positive and multiplication of one positive number and one negative number is negative.

  1. If (X - Y) * (X - Z) is negative, then either (X - Y) is negative, OR (X - Z) is negative. In both cases, X is bigger than one of Y and Z, meaning X is the middle number, hence return X.
  2. If (Y - X) * (Y - Z) is negative, then either (Y - X) is negative, OR (Y - Z) is negative. In both cases, Y is bigger than one of X and Z, meaning Y is the middle number, hence return Y.
  3. If both the above conditions are false, then Z must be the middle number, hence return Z.

The number of comparisons: 2, in the worst case.