# How to check whether a number is in the range[low, high] using one comparison ?

This is simple, but interesting programming puzzle. Given three integers, low, high and x such that high >= low. How to check if x lies in range [low, high] or not using single comparison. For example, if range is [10, 100] and number is 30, then output is true and if the number is 5, then output is false for same range.

A simple solution is compare x with low and high

#include <iostream>

using namespace std;

// Returns true if x is in range [low..high], else false

bool inRange(unsigned low, unsigned high, unsigned x)

{

return (low <= x && x <= high);

}

int main()

{

inRange(10, 100, 30)? cout << "Yes\n": cout <<"No\n";

inRange(10, 100, 5)? cout << "Yes\n": cout <<"No\n";

}

Output:

Yes No

The above solution does two comparisons, Can we do the same task using one comparison?

**We strongly recommend you to minimize your browser and try this yourself first.**

The idea is to compare “x-low” with “high-x”. x is in range [low, high] if and only if x is greater than or equal to low and smaller than or equal to high.

`#include <iostream> ` `using` `namespace` `std; ` ` ` `// Returns true if x is in range [low..high], else false ` `bool` `inRange(unsigned low, unsigned high, unsigned x) ` `{ ` ` ` `return` `((x-low) <= (high-low)); ` `} ` ` ` `int` `main() ` `{ ` ` ` `inRange(10, 100, 30)? cout << ` `"Yes\n"` `: cout <<` `"No\n"` `; ` ` ` `inRange(10, 100, 5)? cout << ` `"Yes\n"` `: cout <<` `"No\n"` `; ` `} ` |

*chevron_right*

*filter_none*

Output:

Yes No

How does this work for [10, 100] and x = 5?

When we subtract 10 from 5, we get -5 which is considered as UNIT_MAX-4 in unsigned int form. UNIT_MAX is maximum possible unsigned int value. The assumption here is, numbers are stored in 2’s complement form. In 2’s complement form, -1 represents UINT_MAX, -2 represets UINT_MAX-1,..etc.

Thanks to Utkarsh for suggesting this solution.

**A Solution that works for negative numbers also**

The idea is to multiply (x-low) and (x-high). If x is in range, then it must be greater than or equal to low, i.e., (x-low) >= 0. And must be smaller than or equal to high i.e., (high – x) <= 0. So if result of the multiplication is less than or equal to 0, then x is in range. Else not. Thanks to eva for suggesting this method.

`#include <iostream> ` `using` `namespace` `std; ` ` ` `// Returns true if x is in range [low..high], else false ` `bool` `inRange(` `int` `low, ` `int` `high, ` `int` `x) ` `{ ` ` ` `return` `((x-high)*(x-low) <= 0); ` `} ` ` ` `int` `main() ` `{ ` ` ` `inRange(10, 100, 30)? cout << ` `"Yes\n"` `: cout <<` `"No\n"` `; ` ` ` `inRange(10, 100, 5)? cout << ` `"Yes\n"` `: cout <<` `"No\n"` `; ` `} ` |

*chevron_right*

*filter_none*

Output:

Yes No

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

## Recommended Posts:

- Measure execution time with high precision in C/C++
- Comparison of a float with a value in C
- Results of comparison operations in C and C++
- Ratio Manipulations in C++ | Set 2 (Comparison)
- Assigning an integer to float and comparison in C/C++
- Comparison of Java with other programming languages
- Comparison of static keyword in C++ and Java
- Comparison of Exception Handling in C++ and Java
- Comparison of boolean data type in C++ and Java
- C program to Check Whether a Number is Positive or Negative or Zero
- NaN in C++ - What is it and how to check for it?
- Program to check if two strings are same or not
- Check if a key is present in a C++ map or unordered_map
- Check if a given graph is Bipartite using DFS
- Quick way to check if all the characters of a string are same