Middle of Three | Practice | GeeksforGeeks

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.

Example 2:
Input:
A = 162, B = 934, C = 200
Output:
200
Exaplanation:
Since 200>162 && 200<934,
So, 200 is the middle element.

Your Task:
You don't need to read input or print anything.Your task is to complete the function middle() which takes three integers A,B and C as input parameters and returns the number which has middle value.

Expected Time Complexity:O(1)
Expected Auxillary Space:O(1)


Constraints:
1<=A,B,C<=109
A,B,C are distinct. 
0
itsmemritu5 days ago
return (A+B+C)-( min(A,min(B,C)) + max(A,max(B,C)));
0
mayank180919991 week ago
 int middle(int A, int B, int C){
        //code here//Position this line where user code will be pasted.
        vector<int>v;
        v.push_back(A);
        v.push_back(B);
        v.push_back(C);
        sort(v.begin(),v.end());
       return v[1];
    }
0
ashutos17sharma89892 weeks ago
    PriorityQueue<Integer> p = new PriorityQueue<>((a,b) -> b-a);       p.add(A);       p.add(B);       p.add(C);       p.poll();       return p.peek();
0
rjpatil20012 weeks ago
int middle(int A, int B, int C){       //code here//Position this line where user code will be pasted.       vector<int> ans;       ans.push_back(A);       ans.push_back(B);       ans.push_back(C);       sort(ans.begin(), ans.end());       return ans[1];          }
+1
kartikeyashokgautam2 weeks ago
JAVA Soution   
int middle(int A, int B, int C){              if(A>B && A<C || A<B && A>C) return A;       else if(B>A && B<C || B<A && B>C) return B;              return C;   }
0
kmohannayak123 weeks ago
Easy Java Solution
----------------------------
class Solution{   int middle(int A, int B, int C){       //code here       int arr[]={A,B,C};       Arrays.sort(arr);       int mid=arr[1];       return mid;   }}
0
siddharthpaul20063 weeks ago
 int middle(int a, int b, int c){       //code here//Position this line where user code will be pasted.       if((a>b and a<c) or (a>c and a<b)){           return a;       }       else if((b>a and b<c) or (b>c and b<a)){           return b;       }       else if((c>a and c<b) or (c>b and c<a)){           return c;       }   }
0
aks190620003 weeks ago
int middle(int A, int B, int C)
{
       //code here       int a[]={A,B,C};       Arrays.sort(a);       return(a[1]);
}
+3
ansarizia93351 month ago
class Solution{ public:   int middle(int A, int B, int C){       //code here//Position this line where user code will be pasted.       long long a=A,b=B,c=C;       if((a-b)*(a-c)<=0)       return a;       else if((b-c)*(b-a)<=0)       return b;       else        return c;   }};
0
dev1711 month ago
3 COMPARISON WITHOUT USING ANY MIN MAX FUNCTION.
Note a comparison is when any logical operator is used such as
‘==’, '>' ,'>','≥','≤'.
so at max only 3 comparisons will be made ..
 int middle(int A, int B, int C){
        int min=A,max=A,mid=A;
        if(B>C){ 			//1st Comp
            if(max<B){		//2nd comp
            max=B;
            }else{
                mid=B;
            }
            if(min>C){		//3rd comp
            min=C;
            }else{
                mid=C;
            }
        }else{
            if(max<C){			//2nd comp
            max=C;
            }else{
                mid=C;
            }
            if(min>B){			//3rd comp
                min=B;
            }else{
                mid=B;
            }
        }
        return mid;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.