Smallest of three integers without comparison operators - GeeksforGeeks

08 Mar, 2022
Write a program to find the smallest of three integers, without using any of the comparison operators. Let 3 input numbers be x, y and z.Method 1 (Repeated Subtraction) Take a counter variable c and initialize it with 0. In a loop, repeatedly subtract x, y and z by 1 and increment c. The number which becomes 0 first is the smallest. After the loop terminates, c will hold the minimum of 3.  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to find Smallest// of three integers without// comparison operators#include <bits/stdc++.h>using namespace std;int smallest(int x, int y, int z){    int c = 0;    while (x && y && z) {        x--;        y--;        z--;        c++;    }    return c;} // Driver Codeint main(){    int x = 12, y = 15, z = 5;    cout << "Minimum of 3 numbers is "         << smallest(x, y, z);    return 0;} // This code is contributed// by Akanksha Rai
// C program to find Smallest// of three integers without// comparison operators#include <stdio.h> int smallest(int x, int y, int z){    int c = 0;    while (x && y && z) {        x--;        y--;        z--;        c++;    }    return c;} int main(){    int x = 12, y = 15, z = 5;    printf("Minimum of 3 numbers is %d", smallest(x, y, z));    return 0;}
// Java program to find Smallest// of three integers without// comparison operatorsclass GFG {     static int smallest(int x, int y, int z)    {        int c = 0;         while (x != 0 && y != 0 && z != 0) {            x--;            y--;            z--;            c++;        }         return c;    }     public static void main(String[] args)    {        int x = 12, y = 15, z = 5;         System.out.printf("Minimum of 3"                              + " numbers is %d",                          smallest(x, y, z));    }} // This code is contributed by  Smitha Dinesh Semwal.
# Python3 program to find Smallest# of three integers without# comparison operators def smallest(x, y, z):    c = 0         while ( x and y and z ):        x = x-1        y = y-1        z = z-1        c = c + 1     return c # Driver Codex = 12y = 15z = 5print("Minimum of 3 numbers is",       smallest(x, y, z)) # This code is contributed by Anshika Goyal
// C# program to find Smallest of three// integers without comparison operatorsusing System; class GFG {    static int smallest(int x, int y, int z)    {        int c = 0;         while (x != 0 && y != 0 && z != 0) {            x--;            y--;            z--;            c++;        }         return c;    }     // Driver Code    public static void Main()    {        int x = 12, y = 15, z = 5;         Console.Write("Minimum of 3"                      + " numbers is " + smallest(x, y, z));    }} // This code is contributed by Sam007
<?php// php program to find Smallest// of three integers without// comparison operatorsfunction smallest($x, $y, $z){    $c = 0;    while ( $x && $y && $z )    {        $x--; $y--; $z--; $c++;    }         return $c;} // Driver code$x = 12;$y = 15;$z = 5;echo "Minimum of 3 numbers is ".             smallest($x, $y, $z); // This code is contributed by Sam007?>
<script> // JavaScript program to find Smallest// of three integers without// comparison operators function smallest(x, y, z){    let c = 0;    while (x && y && z) {        x--;        y--;        z--;        c++;    }    return c;} // Driver Code let x = 12, y = 15, z = 5;document.write("Minimum of 3 numbers is "    + smallest(x, y, z));  // This code is contributed by Surbhi Tyagi. </script>
Output: 
Minimum of 3 numbers is 5
Time Complexity: O(min(x, y, z))
Auxiliary Space: O(1)
This method doesn’t work for negative numbers. Method 2 works for negative numbers also.Method 2 (Use Bit Operations) Use method 2 of this post to find minimum of two numbers (We can’t use Method 1 as Method 1 uses comparison operator). Once we have functionality to find minimum of 2 numbers, we can use this to find minimum of 3 numbers.  
C++
C
Java
Python3
C#
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std;#define CHAR_BIT 8 /*Function to find minimum of x and y*/int min(int x, int y){    return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));} /* Function to find minimum of 3 numbers x, y and z*/int smallest(int x, int y, int z){    return min(x, min(y, z));} // Driver codeint main(){    int x = 12, y = 15, z = 5;    cout << "Minimum of 3 numbers is "  << smallest(x, y, z);    return 0;} // This code is contributed by Code_Mech.
// C implementation of above approach#include <stdio.h>#define CHAR_BIT 8 /*Function to find minimum of x and y*/int min(int x, int y){    return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));} /* Function to find minimum of 3 numbers x, y and z*/int smallest(int x, int y, int z){    return min(x, min(y, z));} int main(){    int x = 12, y = 15, z = 5;    printf("Minimum of 3 numbers is %d", smallest(x, y, z));    return 0;}
// Java implementation of above approachclass GFG{     static int CHAR_BIT = 8; // Function to find minimum of x and ystatic int min(int x, int y){    return y + ((x - y) & ((x - y) >>               ((Integer.SIZE/8) * CHAR_BIT - 1)));} // Function to find minimum of 3 numbers x, y and zstatic int smallest(int x, int y, int z){    return Math.min(x, Math.min(y, z));} // Driver codepublic static void main (String[] args){    int x = 12, y = 15, z = 5;    System.out.println("Minimum of 3 numbers is " +                                smallest(x, y, z));}} // This code is contributed by mits
# Python3 implementation of above approachCHAR_BIT = 8 # Function to find minimum of x and ydef min(x, y):    return y + ((x - y) & \               ((x - y) >> (32 * CHAR_BIT - 1))) # Function to find minimum# of 3 numbers x, y and zdef smallest(x, y, z):    return min(x, min(y, z)) # Driver codex = 12y = 15z = 5print("Minimum of 3 numbers is ",               smallest(x, y, z)) # This code is contributed# by Mohit Kumar
// C# implementation of above approachusing System; class GFG{     static int CHAR_BIT=8; /*Function to find minimum of x and y*/static int min(int x, int y){    return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));} /* Function to find minimum of 3 numbers x, y and z*/static int smallest(int x, int y, int z){    return Math.Min(x, Math.Min(y, z));} // Driver codestatic void Main(){    int x = 12, y = 15, z = 5;    Console.WriteLine("Minimum of 3 numbers is "+smallest(x, y, z));}} // This code is contributed by mits
<script>         let CHAR_BIT = 8;    // Function to find minimum of x and y    function min(x,y)    {        return y + ((x - y) & ((x - y) >> (32 * CHAR_BIT - 1)))    }    // Function to find minimum of 3 numbers x, y and z    function smallest(x,y,z)    {         return Math.min(x, Math.min(y, z));    }         // Driver code    let  x = 12, y = 15, z = 5;         document.write("Minimum of 3 numbers is " +                                smallest(x, y, z));         // This code is contributed by avanitrachhadiya2155     </script>
Output: 
Minimum of 3 numbers is 5
Method 3 (Use Division operator) We can also use division operator to find minimum of two numbers. If value of (a/b) is zero, then b is greater than a, else a is greater. Thanks to gopinath and Vignesh for suggesting this method. 
C++
C
Java
python3
C#
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Using division operator to find// minimum of three numbersint smallest(int x, int y, int z){    if (!(y / x)) // Same as "if (y < x)"        return (!(y / z)) ? y : z;    return (!(x / z)) ? x : z;} int main(){    int x = 78, y = 88, z = 68;    cout << "Minimum of 3 numbers is " << smallest(x, y, z);    return 0;}// this code is contributed by shivanisinghss2110
#include <stdio.h> // Using division operator to find// minimum of three numbersint smallest(int x, int y, int z){    if (!(y / x)) // Same as "if (y < x)"        return (!(y / z)) ? y : z;    return (!(x / z)) ? x : z;} int main(){    int x = 78, y = 88, z = 68;    printf("Minimum of 3 numbers is %d", smallest(x, y, z));    return 0;}
// Java program of above approachclass GfG {     // Using division operator to    // find minimum of three numbers    static int smallest(int x, int y, int z)    {        if ((y / x) != 1) // Same as "if (y < x)"            return ((y / z) != 1) ? y : z;        return ((x / z) != 1) ? x : z;    }     // Driver code    public static void main(String[] args)    {        int x = 78, y = 88, z = 68;        System.out.printf("Minimum of 3 numbers"                              + " is %d",                          smallest(x, y, z));    }} // This code has been contributed by 29AjayKumar
# Using division operator to find# minimum of three numbersdef smallest(x, y, z):     if (not (y / x)): # Same as "if (y < x)"        return y if (not (y / z)) else z    return x if (not (x / z)) else z # Driver Codeif __name__== "__main__":     x = 78    y = 88    z = 68    print("Minimum of 3 numbers is",                  smallest(x, y, z)) # This code is contributed# by ChitraNayal
// C# program of above approachusing System;public class GfG {     // Using division operator to    // find minimum of three numbers    static int smallest(int x, int y, int z)    {        if ((y / x) != 1) // Same as "if (y < x)"            return ((y / z) != 1) ? y : z;        return ((x / z) != 1) ? x : z;    }     // Driver code    public static void Main()    {        int x = 78, y = 88, z = 68;        Console.Write("Minimum of 3 numbers"                          + " is {0}",                      smallest(x, y, z));    }}/* This code contributed by PrinciRaj1992 */
<script> // Javascript implementation of above approach // Using division operator to find// minimum of three numbersfunction smallest(x, y, z){    if (!(y / x)) // Same as "if (y < x)"        return (!(y / z)) ? y : z;    return (!(x / z)) ? x : z;}      let x = 78, y = 88, z = 68;    document.write("Minimum of 3 numbers is " + smallest(x, y, z)); // This is code is contributed by Mayank Tyagi </script>
Output: 
Minimum of 3 numbers is 68
Time Complexity: O(1)
Auxiliary Space: O(1)
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem. 
Sam007
ukasp
Akanksha_Rai
29AjayKumar
princiraj1992
Mithun Kumar
Code_Mech
mohit kumar 29
nidhi_biet
shivanisinghss2110
surbhityagi15
mayanktyagi1709
avanitrachhadiya2155
subhammahato348
subham348
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?
Little and Big Endian Mystery
Cyclic Redundancy Check and Modulo-2 Division
Binary representation of a given number
Program to find whether a given number is power of 2
Josephus problem | Set 1 (A O(n) Solution)