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.
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.
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
2
2 3 1
6 8 9
2
8
In the first test case, the three numbers are 2, 3 and 1. The middle element is 2, as 1 < 2 < 3
In the second test case, the three numbers are 6, 8 and 9. The middle element is 8, as 6 < 8 < 9
2
15 2 3
2 3 4
3
3
In the first test case, the three numbers are 15, 2 and 3. The middle element is 3, as 2 < 3 < 15
In the second test case, the three numbers are 2, 3 and 4. The middle element is 3, as 2 < 3 < 4
Can you think of checking each element?
The basic idea is to check for each element to be the middle element. Steps are as follows:
The number of comparisons: 8, in the worst case.
O(1)
Since we are always doing linear comparisons irrespective of input, the time complexity will be O(1).
O(1)
Since we are not using any extra space, the overall space complexity will be O(1).