Smallest of three integers without comparison operators

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 Code
int 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

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find Smallest
// of three integers without 
// comparison operators
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;
    }
      
    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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 Code
x = 12
y = 15
z = 5
print("Minimum of 3 numbers is",
       smallest(x, y, z))
  
# This code is contributed by Anshika Goyal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find Smallest of three
// integers without comparison operators
using 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

chevron_right


php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to find Smallest
// of three integers without
// comparison operators
function smallest($x, $y, $z)
{
    $c = 0;
    while ( $x && $y && $z )
    {
        $x--; $y--; $z--; $c++;
    }
      
    return $c;
}
  
// Drive code
$x = 12;
$y = 15;
$z = 5;
echo "Minimum of 3 numbers is ".
             smallest($x, $y, $z);
  
// This code is contributed by Sam007
?>

chevron_right



This method doesn’t work for negative numbers. Method 2 works for negative nnumbers 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
// Using division operator to find minimum of three numbers
int 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;
}

chevron_right


python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Using division operator to find
# minimum of three numbers
def 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 Code
if __name__== "__main__":
  
    x = 78
    y = 88
    z = 68
    print("Minimum of 3 numbers is",
                  smallest(x, y, z))
  
# This code is contributed 
# by ChitraNayal

chevron_right



Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.



My Personal Notes arrow_drop_up

Improved By : Sam007, Ita_c, Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.