![]() |
VOOZH | about |
Given two positive integers N and K, we need to determine if there exists a positive integer X less than or equal to N such that the number of 1s in the binary representation of X is equal to k (i.e., f(X) = K). If such an X exists, we also need to find the maximum value of X and print -1 if there is no such positive integer X.
Constraints:
1 <= X <=109
0 <= K <= 31
Examples:
Input: X = 8, K = 2
Output:6Input: X = 9, K = 4
Output: -1
Approach: Follow the steps to solve the problem:
Below is the implementation for the above approach:
6 -1
Time Complexity: O(Log(N))
Auxiliary Space: O(1)