# Largest of two distinct numbers without using any conditional statements or operators

In this problem set, we will be given any two distinct positive numbers, let’s say a and b, we need to return the largest of two distinct numbers without using any conditional statements (if-else) or any operators(<,>,==,!=,etc.) in c++.

The main difficulty of the problem includes that we need to determine the largest of any two distinct positive numbers without using any operators or conditional statements.

For example,

INPUT: x=12, y=20

OUTPUT: 20

INPUT: x=3, y=2

OUTPUT: 3

Below is the algorithm that we will be using to solve this problem.

## Algorithm

We will use type casting to solve this problem. Type casting includes the conversion of one data type into another data type. For example, we can convert int data type into long long data type using type casting or we can convert int data type into boolean data type using it.

Here in this problem we will use type casting of int into boolean to determine the largest of any two distinct positive integers. It can be better explained with the below expression −

$$\mathrm{x *\:(bool)\:(\frac{x}{y})\:+\:y\:*\:(bool)\:(\frac{y}{x})}$$

(data type) expression is the syntax for type casting where we can pass any data type we want to convert into.

The above equation will work as follow to give us the desired output −

• The result of the value ${\frac{x}{y}}$ after type casting it to boolean is 1 if x>y and it is 0 if y>x.

• The same goes for ${\frac{y}{x}}$ It will return 1 if y>x and 0 if x>y after type casting it to boolean.

• In simple words, type casting any value from int data type into boolean data type will return 1 if int value is greater than or equal to1 and 0 if it is less than 1.

Hence the above expression will return x+0 or 0+y depending upon which one is greater x or y. Let’s understand this better with an example.

Let’s say we are given x=5 and y=3.

The above expression will be $\mathrm{5 *\:(bool)\:(\frac{5}{3})\:+\:3\:*\:(bool)\:(\frac{3}{5})}$

Since 5/3 is greater than 1 so type casting it into a boolean data type will give 1 and 3/5 is less than 1 so it will give 0 after type casting. There, the expression will become −

$$\mathrm{5 * 1 + 0, which\:is\:equal\:to\:5.}$$

5 is a greater number between 5 and 3. Hence, 5 is the desired output.

## Approach

• We will make a function to determine the larger number between the two.

• Initialize a variable named num to store the value given by the expression, $\mathrm{x *\:(bool)\:(\frac{x}{y})\:+\:y\:*\:(bool)\:(\frac{y}{x})}$.

• It will give either x or y whichever will be greater.

• Return num which is our required output.

### Example

Below is the C++ implementation of the above approach −

#include <stdio.h>
#include<bits/stdc++.h>

using namespace std;

//function to get the larger number among two distinct positive numbers
int larger(int x, int y){
//type casting to boolean data type
int num= x * (bool) (x/y) + y * (bool) (y/x); //to store larger number

return num;
}

//Driver Code
int main(){
int x=28,y=20;
cout<<larger(x,y)<<endl;

x=8, y=13;
cout<<larger(x,y)<<endl;

return 0;
}


### Output

28
13


Time Complexity: O(1), since constant time is taken to perform the operation.

Space Complexity: O(1), since it uses no extra space.

## Conclusion

In this article, we tried to solve the problem to determine the largest number among two distinct positive numbers without using any conditional statements or operators. We used the concept of type casting to boolean to get the required output using a particular expression.

I hope you find this article helpful and it cleared all your concepts regarding this problem.