Middle of three using minimum comparisons

3

Given three distinct numbers a, b and c find the number with a value in middle.

Examples :

Input : a = 20, b = 30, c = 40
Output : 30

Input : a = 12, n = 32, c = 11
Output : 12

Simple Approach :

C++

// CPP program to find  middle of three distinct 
// numbers
#include <bits/stdc++.h>
using namespace std;

int middleOfThree(int a, int b, int c)
{
    // Checking for b
    if ((a < b && b < c) || (c < b && b < a))
       return b;

    // Checking for a
    else if ((b < a && a < c) || (c < a && a < b))
       return a;

    else
       return c;
}

// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}

Java

// Java program to find middle of 
// three distinct numbers
import java.util.*;

class Middle
{   
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // Checking for b
        if ((a < b && b < c) || (c < b && b < a))
            return b;

        // Checking for a
        else if ((b < a && a < c) || (c < a && a < b))
        return a;

        else
        return c;
    }
    
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println( middleOfThree(a, b, c) );
    }
}

// This code is contributed by rishabh_jain

Python3

# Python3 program to find middle 
# of three distinct numbers

def middleOfThree(a, b, c):
    
    # Checking for b
    if ((a < b and b < c) or (c < b and b < a)) :
        return b;

    # Checking for a
    if ((b < a and a < c) or (c < a and a < b)) :
        return a;

    else :
        return c

# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))

# This code is contributed by rishabh_jain


Output :
30

But approach used above is not efficient because of extra comparisons, which can be easily minimized. In case first part is false, it will execute remaining half to check the condition. This problem remains same if we are checking for a also.

Better Approach (Needs less comparison):

C++

// CPP program to find middle of three distinct
// numbers
#include <bits/stdc++.h>
using namespace std;

// Function to find the middle of three numbers
int middleOfThree(int a, int b, int c)
{
    // Compare each three number to find middle 
    // number. Enter only if a > b
    if (a > b) 
    {
        if (b > c)
            return b;
        else if (a > c)
            return c;
        else
            return a;
    }
    else 
    {
        // Decided a is not greater than b.
        if (a > c)
            return a;
        else if (b > c)
            return c;
        else
            return b;
    }
}

// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}

Java

// Java program to find middle of 
// three distinct numbers
import java.util.*;

class Middle
{   
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b, 
                                          int c)
    {
        // Compare each three number to find 
        // middle number. Enter only if a > b
        if (a > b) 
        {
            if (b > c)
                return b;
            else if (a > c)
                return c;
            else
                return a;
        }
        else
        {
            // Decided a is not greater than b.
            if (a > c)
                return a;
            else if (b > c)
                return c;
            else
                return b;
        }
    }
    
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println(middleOfThree(a, b, c));
    }
}

// This code is contributed by rishabh_jain

Python3

# Python3 program to find middle 
# of three distinct numbers

# Function to find the middle of three numbers
def middleOfThree(a, b, c) :
    
    # Compare each three number to find 
    # middle number. Enter only if a > b
    if a > b : 
        if (b > c):
            return b
        elif (a > c) :
            return c
        else :
            return a 
    else:
        # Decided a is not greater than b.
        if (a > c) :
            return a
        elif (b > c) :
            return c
        else :
            return b

# Driver Code
a = 20
b = 30
c = 40
print( middleOfThree(a, b, c) )

# This code is contributed by rishabh_jain


Output:

30

This approach is efficient and having less number of comparisons. Outer IF loop will only be executed if a > b otherwise, outer ELSE will be executed.
Other Efficient Approach :

C++

// CPP program to find middle of three distinct 
// numbers
#include <bits/stdc++.h>
using namespace std;

// Function to find the middle of three number
int middleOfThree(int a, int b, int c)
{
    // x is positive if a is greater than b. 
    // x is negative if b is greater than a.
    int x = a - b;

    int y = b - c;  // Similar to x
    int z = a - c;  // Similar to x and y.

    // Checking if b is middle (x and y both
    // are positive)
    if (x * y > 0)
        return b;

    // Checking if c is middle (x and z both
    // are positive)
    else if (x * z > 0)
        return c;
    else
        return a;
}

// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}

Java

//java program to find middle of three distinct 
// numbers
import java.util.*;

class Middle
{   
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b, 
                                          int c)
    {
        // x is positive if a is greater than b. 
        // x is negative if b is greater than a.
        int x = a - b;

        int y = b - c; // Similar to x
        int z = a - c; // Similar to x and y.

        // Checking if b is middle (x and y 
        // both are positive)
        if (x * y > 0)
            return b;

        // Checking if c is middle (x and z 
        // both are positive)
        else if (x * z > 0)
            return c;
        else
            return a;
    }
    
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println( middleOfThree(a, b, c) );
    }
}

// This code is contributed by rishabh_jain

Python3

# Python3 program to find middle 
# of three distinct numbers

# Function to find the middle of three number
def middleOfThree(a, b, c) :
    
    # x is positive if a is greater than b. 
    # x is negative if b is greater than a.
    x = a - b

    # Similar to x
    y = b - c
    
    # Similar to x and y.
    z = a - c 

    # Checking if b is middle (x and y 
    # both are positive)
    if x * y > 0:
        return b

    # Checking if c is middle (x and z 
    # both are positive)
    elif (x * z > 0) :
        return
    else :
        return a

# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))

# This code is contributed by rishabh_jain


Output :

30

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 2 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.