Open In App

Smallest of three integers without comparison operators

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given three integers a, b, and c, the task is to find the smallest integer without using any comparison operators

Examples:

Input: a = 2, b = 5, c = 7
Output: 2
Explanation: The smallest of the 3 integers is a, which is equal to 2.

Input: a = 14, b = 13, c = 10
Output: 10
Explanation: The smallest of the 3 integers is c, which is equal to 10.

Input: a = 6, b = 1, c = 9
Output: 1
Explanation: The smallest of the 3 integers is b, which is equal to 1.

Using Repeated Subtraction - O(min({a, b, c})) Time and O(1) Space

The idea is to find the smallest number by repeatedly subtracting 1 from all three numbers until one of them reaches 0. The number of subtractions performed represents the smallest number. This works because the smallest number will determine how many times all three values can be reduced together.

C++
// C++ program to find the smallest of three numbers
// using Repeated Subtraction Method.
#include <bits/stdc++.h>
using namespace std;

// Function to find the smallest number
int findSmallest(int a, int b, int c) {
    
    int count = 0;
    
    // Keep subtracting until one of them reaches 0
    while (a > 0 && b > 0 && c > 0) {
        a--; 
        b--; 
        c--; 
        count++;
    }
    
    return count;
}

int main() {
    
    int a = 2, b = 5, c = 7;
    cout << findSmallest(a, b, c) << endl;
    
    return 0;
}
Java
// Java program to find the smallest of three numbers
// using Repeated Subtraction Method.
class GfG {
    
    // Function to find the smallest number
    static int findSmallest(int a, int b, int c) {
        
        int count = 0;
        
        // Keep subtracting until one of them reaches 0
        while (a > 0 && b > 0 && c > 0) {
            a--; 
            b--; 
            c--; 
            count++;
        }
        
        return count;
    }

    public static void main(String[] args) {
        
        int a = 2, b = 5, c = 7;
        System.out.println(findSmallest(a, b, c));
    }
}
Python
# Python program to find the smallest of three numbers
# using Repeated Subtraction Method.

# Function to find the smallest number
def findSmallest(a, b, c):
    
    count = 0
    
    # Keep subtracting until one of them reaches 0
    while a > 0 and b > 0 and c > 0:
        a -= 1
        b -= 1
        c -= 1
        count += 1
    
    return count

if __name__ == "__main__":
    
    a, b, c = 2, 5, 7
    print(findSmallest(a, b, c))
C#
// C# program to find the smallest of three numbers
// using Repeated Subtraction Method.
using System;

class GfG {
    
    // Function to find the smallest number
    static int findSmallest(int a, int b, int c) {
        
        int count = 0;
        
        // Keep subtracting until one of them reaches 0
        while (a > 0 && b > 0 && c > 0) {
            a--; 
            b--; 
            c--; 
            count++;
        }
        
        return count;
    }

    public static void Main() {
        
        int a = 2, b = 5, c = 7;
        Console.WriteLine(findSmallest(a, b, c));
    }
}
JavaScript
// JavaScript program to find the smallest of three numbers
// using Repeated Subtraction Method.

// Function to find the smallest number
function findSmallest(a, b, c) {
    
    let count = 0;
    
    // Keep subtracting until one of them reaches 0
    while (a > 0 && b > 0 && c > 0) {
        a--; 
        b--; 
        c--; 
        count++;
    }
    
    return count;
}

let a = 2, b = 5, c = 7;
console.log(findSmallest(a, b, c));

Output
2

Note: The above method doesn't work for negative numbers. Below method works for negative numbers also.

Using Bitwise Operators - O(1) Time and O(1) Space

The idea is to find the smallest number by using bitwise operations to determine which number is smaller. The approach works by checking the sign bit of the difference between two numbers to decide the minimum. This process is repeated to compare three numbers and find the smallest.

C++
// C++ program to find the smallest of three numbers
// using Bitwise Operators
#include <bits/stdc++.h>
using namespace std;

// Function to find the minimum of two numbers 
// using bitwise operations
int bitwiseMin(int x, int y) {
    
    // (x - y) >> 31 extracts the sign bit 
    // to determine min  
    return y + ((x - y) & ((x - y) >> 31));
}

// Function to find the smallest of three numbers
int findSmallest(int a, int b, int c) {
    return bitwiseMin(a, bitwiseMin(b, c));
}

int main() {
    
    int a = 2, b = 5, c = 7;
    cout << findSmallest(a, b, c) << endl;
    
    return 0;
}
Java
// Java program to find the smallest of three numbers
// using Bitwise Operators.
class GfG {
    
    // Function to find the minimum of two numbers 
    // using bitwise operations
    static int bitwiseMin(int x, int y) {
        
        // (x - y) >> 31 extracts the sign bit 
        // to determine min  
        return y + ((x - y) & ((x - y) >> 31));
    }

    // Function to find the smallest of three numbers
    static int findSmallest(int a, int b, int c) {
        return bitwiseMin(a, bitwiseMin(b, c));
    }

    public static void main(String[] args) {
        
        int a = 2, b = 5, c = 7;
        System.out.println(findSmallest(a, b, c));
    }
}
Python
# Python program to find the smallest of three numbers
# using Bitwise Operators.

# Function to find the minimum of two numbers 
# using bitwise operations
def bitwiseMin(x, y):
    
    # (x - y) >> 31 extracts the sign bit 
    # to determine min  
    return y + ((x - y) & ((x - y) >> 31))

# Function to find the smallest of three numbers
def findSmallest(a, b, c):
    return bitwiseMin(a, bitwiseMin(b, c))

if __name__ == "__main__":
    
    a, b, c = 2, 5, 7
    print(findSmallest(a, b, c))
C#
// C# program to find the smallest of three numbers
// using Bitwise Operators.
using System;

class GfG {
    
    // Function to find the minimum of two numbers 
    // using bitwise operations
    static int bitwiseMin(int x, int y) {
        
        // (x - y) >> 31 extracts the sign bit 
        // to determine min  
        return y + ((x - y) & ((x - y) >> 31));
    }

    // Function to find the smallest of three numbers
    static int findSmallest(int a, int b, int c) {
        return bitwiseMin(a, bitwiseMin(b, c));
    }

    public static void Main() {
        
        int a = 2, b = 5, c = 7;
        Console.WriteLine(findSmallest(a, b, c));
    }
}
JavaScript
// JavaScript program to find the smallest of three numbers
// using Bitwise Operators.

// Function to find the minimum of two numbers 
// using bitwise operations
function bitwiseMin(x, y) {
    
    // (x - y) >> 31 extracts the sign bit 
    // to determine min  
    return y + ((x - y) & ((x - y) >> 31));
}

// Function to find the smallest of three numbers
function findSmallest(a, b, c) {
    return bitwiseMin(a, bitwiseMin(b, c));
}

let a = 2, b = 5, c = 7;
console.log(findSmallest(a, b, c));

Output
2

Using Division Operator - O(1) Time and O(1) Space

The idea is to determine the smallest number using division by checking if one number is completely divisible by another. If a number cannot fully divide another, it is considered smaller. This process is repeated to compare all three numbers and identify the smallest.

Note : This approach uses a comparison operator "==" and might not be considered as valid

C++
// C++ program to find the smallest of three numbers
// using the Division Operator
#include <bits/stdc++.h>
using namespace std;

// Function to find the smallest number 
// using division
int findSmallest(int a, int b, int c) {
    
    // Check if 'b' is smaller than 'a'
    if (!(b / a)) {
        
        // If 'b' is also smaller than 'c',
        // return 'b'
        if (!(b / c)) {
            return b;
        } 
        
        // Otherwise, return 'c' as the smallest
        return c;
    }
    
    // If 'a' is smaller than 'c', return 'a', 
    // else return 'c'
    if (!(a / c)) {
        return a;
    }
    
    return c;
}

int main() {

    int a = 2, b = 5, c = 7;
    
    cout << findSmallest(a, b, c) << endl;
    
    return 0;
}
Java
// Java program to find the smallest of three numbers
// using the Division Operator.
class GfG {
    
    // Function to find the smallest number 
    // using division
    static int findSmallest(int a, int b, int c) {
        
        // Check if 'b' is smaller than 'a'
        if ((b / a) == 0) {
            
            // If 'b' is also smaller than 'c',
            // return 'b'
            if ((b / c) == 0) {
                return b;
            } 
            
            // Otherwise, return 'c' as the smallest
            return c;
        }
        
        // If 'a' is smaller than 'c', return 'a', 
        // else return 'c'
        if ((a / c) == 0) {
            return a;
        }
        
        return c;
    }

    public static void main(String[] args) {

        int a = 2, b = 5, c = 7;
        
        System.out.println(findSmallest(a, b, c));
    }
}
python
# Python program to find the smallest of three numbers
# using the Division Operator.

# Function to find the smallest number 
# using division
def findSmallest(a, b, c):
    
    # Check if 'b' is smaller than 'a'
    if not (b // a):
        
        # If 'b' is also smaller than 'c',
        # return 'b'
        if not (b // c):
            return b
        
        # Otherwise, return 'c' as the smallest
        return c
    
    # If 'a' is smaller than 'c', return 'a', 
    # else return 'c'
    if not (a // c):
        return a
    
    return c

if __name__ == "__main__":

    a, b, c = 2, 5, 7
    
    print(findSmallest(a, b, c))
C#
// C# program to find the smallest of three numbers
// using the Division Operator.
using System;

class GfG {
    
    // Function to find the smallest number 
    // using division
    static int findSmallest(int a, int b, int c) {
        
        // Check if 'b' is smaller than 'a'
        if ((b / a) == 0) {
            
            // If 'b' is also smaller than 'c',
            // return 'b'
            if ((b / c) == 0) {
                return b;
            } 
            
            // Otherwise, return 'c' as the smallest
            return c;
        }
        
        // If 'a' is smaller than 'c', return 'a', 
        // else return 'c'
        if ((a / c) == 0) {
            return a;
        }
        
        return c;
    }

    public static void Main() {

        int a = 2, b = 5, c = 7;
        
        Console.WriteLine(findSmallest(a, b, c));
    }
}
JavaScript
// JavaScript program to find the smallest of three numbers
// using the Division Operator.

// Function to find the smallest number 
// using division
function findSmallest(a, b, c) {
    
    // Check if 'b' is smaller than 'a'
    if (Math.trunc(b / a) === 0) {
        
        // If 'b' is also smaller than 'c',
        // return 'b'
        if (Math.trunc(b / c) === 0) {
            return b;
        } 
        
        // Otherwise, return 'c' as the smallest
        return c;
    }
    
    // If 'a' is smaller than 'c', return 'a', 
    // else return 'c'
    if (Math.trunc(a / c) === 0) {
        return a;
    }
    
    return c;
}

let a = 2, b = 5, c = 7;

console.log(findSmallest(a, b, c));

Output
2

Next Article
Article Tags :
Practice Tags :

Similar Reads