Got asked these in Walmart coding round:
A. Given a number n. To find two numbers a and b such that
a xor b = n
b >= a and (b - a) should be minimum
for all the a and b which satisfy the above conditions a and b should be minimum
1 <= n <= 1e12
- My Soln:
for(j = 0; j <= 62; j++)
if((n >> j) & 1) b = (1 << j);
a = n - b;
- Result: Partial testcase passed
- Weightage: 20 pts.
B. Given an array A of size n and a number K. Have to find the number of subarrays [l, r] such that:
(r - l + 1) = K * (sum of all elements of subarray [l, r])
1 <= n <= 1e5
|K| >= 1
-1e6 <= A[i] <= 1e6
- My Soln: Wrote O(n * n) soln. Spoiler Alert got TLE.
- Weightage: 30 pts.
How can I have solved these???