5. Middle of Three
5. Middle of Three
Problem link :- click here
package program;
public class Searching__5
{
/*
Given three distinct numbers A, B and C. Find the number with value in middle
A = 968 B = 518 C = 300
output : 300
lets take minimum and maximum between two elements first and
*/
static int middleOfThree(int A, int B, int C)
{
int min, max;
if( A < B)
{
min = A;
max = B;
}
else
{
min = B;
max = A;
}
if(C < min)
return min;
else if(C > max)
return max;
else
return C;
}
public static void main(String[] args)
{
int ans = middleOfThree(978, 518, 300);
System.out.println(ans);
}
}
Time complexity :- O(1)
Space complexity :- O(1)
Comments
Post a Comment