1400. Find the Largest Number in Range with Max Set Bits

HardBit ManipulationBit MathGreedyRange

Given a range [left, right], find the integer in that inclusive range with the greatest number of set bits. If several integers share the maximum set-bit count, return the largest of them.

Input: A JSON object {"left": <range start>, "right": <range end>} with left <= right.

Output: Return the largest integer in [left, right] attaining the maximum popcount.

Examples

Example 1
Input: {"left":5,"right":6}
Output: 6
Explanation: Both have two set bits, so the larger value 6 is returned.
Example 2
Input: {"left":1,"right":7}
Output: 7
Explanation: 7 has three set bits, the most in the range.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →