Special bits

3.9

23 votes
Bit Manipulation, Basic Programming, Basics of Bit Manipulation, Algorithms
Problem

There are T test cases. Each test case contains the following:

You are given three integers L and R that denote a range and k. Your task is to find a number in between the range L to R (inclusive) which has a set bit count as k.

If there are multiple such numbers present in the range, print the minimum among such numbers. If no number satisfies the above condition, print -1.

Note

  • 14 can be written as 1110 in binary and its set bit count is 3.
  • 8 can be written as 1000 in binary and its set bit count is 1.

Input format

  • The first line contains one integer T denoting the number of test cases.
  • The first line of each test case consists of three integers L, R, and k.

Output format

For every test case, print one integer denoting the answer to the problem.

Constraints
1LR10181k641T105

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The minimum number in range 1 - 10 with 1 set bit is 1 (0001).

The minimum number in range 1 - 10 with 2 set bit is 3 (0011).

The minimum number in range 1 - 10 with 3 set bit is 7 (0111).

No number is present in the range 1 -10 with 4 set bit count.

 

Editor Image

?