Special bits
Practice
3.9 (23 votes)
Bit manipulation
Basic programming
Basics of bit manipulation
Algorithms
Problem
54% Success 5708 Attempts 50 Points 2s Time Limit 256MB Memory 1001 KB Max Code

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
\(1 ≤ L ≤ R ≤ 10^{18}\\ 1 ≤ k ≤ 64\\ 1 ≤ T ≤ 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
7 votes
Tags:
Basic ProgrammingBit Manipulation
Points:50
9 votes
Tags:
Bit ManipulationBasic ProgrammingBasics of Bit ManipulationGreedy algorithm