Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify checking if a pair of numbers is (1,2) or (2,1)?

Tags:

c++

c

logic

I need to make sure that a pair of numbers is either (1, 2) or (2, 1). Both x and y are always positive. The code I am using right now:

if ((x == 2 && y == 1) ||
    (x == 1 && y == 2)) {
    return 1;
}

It looks a little bulky, and I feel like it can be simplified. However, everything I tried resulted in false positives.

like image 829
Max Pastushkov Avatar asked Dec 07 '20 03:12

Max Pastushkov


Video Answer


1 Answers

This is the most direct way to make this comparison.

It is also more readable than any alternative you may come up with, so no need to change it.

like image 90
dbush Avatar answered Sep 28 '22 18:09

dbush