We have two pairs of numbers. The first pair consists of a1, a2
and the second pair
consists of b1, b2
. We wish to determine if any element of a
is in b
, or vice versa.
This is simple enough:
int a1, a2, b1, b2;
// is it possible to do better than this:
boolean hasOverlap = a1 == b1 || a1 == b2 || a2 == b1 || a2 == b2;
The question is, is it theoretically possible to do this faster? Perhaps through some clever use of bitwise arithmetic?
EDIT FOR CLARITY: For specificity, imagine we are trying to optimize a java program.
Bonus question
Assuming we had an arbitrary number of pairs, instead of just two pairs. You could imagine an array of 100 arrays of length 2. What is the most efficient to remove any pair from that list if that pair contains an element that appears in another pair on the list?
So for example: [[1,2], [3,4], [2,5]]
would be reduced to [[3,4]]