Middle of 3 Elements

Question:
                   

Given three distinct numbers A, B and C. Find the number with value in middle (Try to do it with minimum comparisons).

Example 1:

Input:
A = 978, B = 518, C = 300
Output:
518
Explanation:
Since 518>300 and 518<978, so 
518 is the middle element.

ANSWER:

  int middle(int A, int B, int C){
        int arr[3];
        arr[0]=A;
        arr[1]=B;
        arr[2]=C;
         sort(arr, arr + 3);
         return arr[1];
    }

Comments

Popular posts from this blog

Problem no 18(array):count the number of pairs in array whose sum is equal to the given number.

convert the integer number to its binary representation.

Tcs assessment problem: museum problem.