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
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);
inRange(10, 100, 30)? cout << "Yes\n": cout <<"No\n";
inRange(10, 100, 5)? cout << "Yes\n": cout <<"No\n";
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.
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Measure execution time with high precision in C/C++
- Comparison of static keyword in C++ and Java
- Results of comparison operations in C and C++
- Comparison of a float with a value in C
- Ratio Manipulations in C++ | Set 2 (Comparison)
- Assigning an integer to float and comparison in C/C++
- Comparison of boolean data type in C++ and Java
- Comparison of Java with other programming languages
- Comparison of Python with Other Programming Languages
- Comparison of Exception Handling in C++ and Java
- C program to Check Whether a Number is Positive or Negative or Zero
- C++ program to check whether a String is a Pangram or not
- Print pattern using only one loop | Set 1 (Using setw)
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Write a one line C function to round floating point numbers
- Change/add only one character and print '*' exactly 20 times
- Write one line functions for strcat() and strcmp()
- C program to copy contents of one file to another file
- C++ program to append content of one text file to another
- Program to copy the contents of one array into another in the reverse order
Improved By : jit_t