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

Popular posts from this blog

4.Sort an array of 0's, 1's & 2's.

1. Reverse

3. Height of Binary tree