Problem · Bit Manipulation
MediumIBM logoIBMOA
See IBM hiring insights

Problem statement

For two positive integers, lo and hi, and a limit k, find two integers, a and b, satisfying the following criteria. Return the value of a e b. The symbol denotes the bitwise XOR operator.

  • lo <= a < b <= hi
  • The value of a e b is maximal for a e b ≤ k.

Function

maxXor(lo: int, hi: int, k: int) → int

Complete the function maxXor in the editor below. The function must return an integer denoting the maximum possible value of a e b for all a e b ≤ k.

maxXor has the following parameters:

  • int lo: an integer
  • int hi: an integer
  • int k: an integer

Examples

Example 1

lo = 3hi = 5k = 6return = 6

The maximal useable XORed value is 6 because it is the maximal value that is less than or equal to the limit k = 6.

Constraints

  • 1 ≤ lo < hi ≤ 10^4
  • 1 ≤ k ≤ 10^4

More IBM problems

drafts saved locally
public int maxXor(int lo, int hi, int k) {
  // write your code here
}
lo3
hi5
k6
expected6
checking account