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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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


Output:

Yes
No

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



My Personal Notes arrow_drop_up

Improved By : jit_t



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.