PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4
Setter: Lavish Gupta
Tester: Aryan, Takuki Kurokawa
Editorialist: Pratiyush Mishra
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Alice has an array A of length N. She is interested in finding the number of pairs (i,j) such that 1≤i<j≤N and A_i \oplus A_j<A_i\; \& \;A_j
\oplus represents the Bitwise XOR operator. \& represents the Bitwise AND operator.
Explanation
Taking two elements from the array, say A_i and A_j and assuming their most significant bit to be x and y respectively. Then if,
Now if we group the elements having same most significant bits, then in each such group if we pick any two elements, say A_i and A_j then for them A_i\;\oplus\; A_j < A_i\; \& \; A_j.
Total number of ways of picking any two elements from a group of size N is:
Thus we would sum up all possible pairs from each group to get the final answer.
Suppose from the given array we form corresponding groups as \{G_1,G_2, G_3........G_k\} where the size of of the group say, G_i is g_i, then
TIME COMPLEXITY:
Calculation of the MSB position of a number can be done in O(1) time. Further looping through the array to calculate MSB and grouping them can be done in O(N) time. Thus final time complexity of the solution would be O(N)
SOLUTION:
Editorialist’s Solution
Setter’s Solution
Tester-1’s Solution
Tester-2’s Solution