Largest number less than equal to N having exactly K set bits
Last Updated :
15 Oct, 2023
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:6
Input: X = 9, K = 4
Output: -1
Approach: Follow the steps to solve the problem:
- Count the number of set bits in the integer X.
- If a number of set bits is equal to K then return that number itself.
- Else traverse every bit of the number.
- If the number of set bits in X is less than the required set bits (K).
- Then count the number of non set bits we have to convert from the current bit and toggle them.
- Else count the number of set bits we have to convert from 1's to 0's and toggle them from right to left.
- If no such number is possible to create return -1.
- Else return that maximum number which is less than X and has total K set bits.
Below is the implementation for the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the greatest number <= X
// having at most K set bits.
int greatestKBits(int X, int K)
{
// Finding total number of Set Bits
// in the number X
int set_bit_count = __builtin_popcountll(X);
// If set_bits are equal to K then answer
// will be that number itself
if (set_bit_count == K)
return X;
// Initializing ans
int ans = -1;
// Variables to store count of 1's and 0's
// from right to left in binary
// representation of the X
int zero = 0, one = 0;
// Traversing every bit
for (int i = 0; i < 31; i++) {
// If current bit is set i.e. '1' is in
// this place then follow below case
if (((X & (1 << i)) != 0)) {
// Increase the count of one
++one;
// Remaining bits we have to set
int rem = K - set_bit_count + 1;
// If number of set bits are less than
// number of set bits required in this
// case we have to convert some 0's
// into 1's
if (set_bit_count < K
and zero >= K - set_bit_count + 1
and zero >= rem) {
int new_ans = X;
// Removing the current bit from
// the number
new_ans -= (1 << i);
for (int j = i - 1; j >= 0; j--) {
// When we still have to convert
// bits from zero to one
if (rem > 0) {
if ((new_ans & (1 << j)) == 0)
rem--;
new_ans |= (1 << j);
}
}
// Store the maximun number in the
// ans variable
ans = max(ans, new_ans);
}
// When number of set bits are greater
// than required set bits in this case
// we have to convert some 1's into 0's
else if (set_bit_count > K
and (set_bit_count - one) <= K) {
int new_ans = X;
// Remaining number of 1's we
// still required
rem = K - (set_bit_count - one);
for (int j = i; j >= 0; j--) {
if (rem > 0
and (new_ans & (1 << j)) != 0) {
rem--;
}
else {
if ((new_ans & (1 << j)) != 0)
new_ans -= (1 << j);
}
}
// Storing the maximum value in answer
ans = max(ans, new_ans);
}
}
else {
// If current bit is not set i.e. '0'
// is in that place
++zero;
}
}
return ans;
}
// Driver code
int main()
{
// First Test Case
int X = 8, K = 2;
cout << greatestKBits(X, K) << endl;
// Second Test Case
X = 9;
K = 4;
cout << greatestKBits(X, K) << endl;
return 0;
}
Java
import java.io.*;
public class GFG {
// Function to return the greatest number <= X
// having at most K set bits.
static int greatestKBits(int X, int K) {
// Finding total number of Set Bits
// in the number X
int set_bit_count = Integer.bitCount(X);
// If set_bits are equal to K then answer
// will be that number itself
if (set_bit_count == K)
return X;
// Initializing ans
int ans = -1;
// Variables to store count of 1's and 0's
// from right to left in binary
// representation of the X
int zero = 0, one = 0;
// Traversing every bit
for (int i = 0; i < 31; i++) {
// If current bit is set i.e. '1' is in
// this place then follow below case
if ((X & (1 << i)) != 0) {
// Increase the count of one
++one;
// Remaining bits we have to set
int rem = K - set_bit_count + 1;
// If number of set bits are less than
// number of set bits required in this
// case we have to convert some 0's
// into 1's
if (set_bit_count < K
&& zero >= K - set_bit_count + 1
&& zero >= rem) {
int new_ans = X;
// Removing the current bit from
// the number
new_ans -= (1 << i);
for (int j = i - 1; j >= 0; j--) {
// When we still have to convert
// bits from zero to one
if (rem > 0) {
if ((new_ans & (1 << j)) == 0)
rem--;
new_ans |= (1 << j);
}
}
// Store the maximum number in the
// ans variable
ans = Math.max(ans, new_ans);
}
// When number of set bits are greater
// than required set bits in this case
// we have to convert some 1's into 0's
else if (set_bit_count > K
&& (set_bit_count - one) <= K) {
int new_ans = X;
// Remaining number of 1's we
// still required
rem = K - (set_bit_count - one);
for (int j = i; j >= 0; j--) {
if (rem > 0 && (new_ans & (1 << j)) != 0) {
rem--;
} else {
if ((new_ans & (1 << j)) != 0)
new_ans -= (1 << j);
}
}
// Storing the maximum value in answer
ans = Math.max(ans, new_ans);
}
} else {
// If current bit is not set i.e. '0'
// is in that place
++zero;
}
}
return ans;
}
// Driver code
public static void main(String[] args) {
// First Test Case
int X = 8, K = 2;
System.out.println(greatestKBits(X, K));
// Second Test Case
X = 9;
K = 4;
System.out.println(greatestKBits(X, K));
}
}
Python3
# Function to return the greatest number <= X
# having at most K set bits.
def greatest_k_bits(X, K):
# Finding total number of Set Bits
# in the number X
set_bit_count = bin(X).count('1')
# If set_bits are equal to K then answer
# will be that number itself
if set_bit_count == K:
return X
# Initializing ans
ans = -1
# Variables to store count of 1's and 0's
# from right to left in binary
# representation of the X
zero, one = 0, 0
# Traversing every bit
for i in range(31):
# If current bit is set i.e. '1' is in
# this place then follow below case
if X & (1 << i) != 0:
# Increase the count of one
one += 1
# Remaining bits we have to set
rem = K - set_bit_count + 1
# If number of set bits are less than
# number of set bits required in this
# case we have to convert some 0's
# into 1's
if (set_bit_count < K and zero >= K - set_bit_count + 1 and zero >= rem):
new_ans = X
# Removing the current bit from
# the number
new_ans -= (1 << i)
for j in range(i - 1, -1, -1):
# When we still have to convert
# bits from zero to one
if rem > 0:
if (new_ans & (1 << j)) == 0:
rem -= 1
new_ans |= (1 << j)
# Store the maximum number in the
# ans variable
ans = max(ans, new_ans)
# When number of set bits are greater
# than required set bits in this case
# we have to convert some 1's into 0's
elif (set_bit_count > K and (set_bit_count - one) <= K):
new_ans = X
# Remaining number of 1's we
# still required
rem = K - (set_bit_count - one)
for j in range(i, -1, -1):
if rem > 0 and (new_ans & (1 << j)) != 0:
rem -= 1
elif (new_ans & (1 << j)) != 0:
new_ans -= (1 << j)
# Storing the maximum value in answer
ans = max(ans, new_ans)
else:
# If current bit is not set i.e. '0'
# is in that place
zero += 1
return ans
# Driver code
if __name__ == "__main__":
# First Test Case
X = 8
K = 2
print(greatest_k_bits(X, K))
# Second Test Case
X = 9
K = 4
print(greatest_k_bits(X, K))
C#
using System;
public class Program
{
// Function to return the greatest number <= X
// having at most K set bits.
public static int GreatestKBits(int X, int K)
{
// Finding total number of Set Bits
// in the number X
int setBitCount = Convert.ToString(X, 2).Replace("0", "").Length;
// If set_bits are equal to K then answer
// will be that number itself
if (setBitCount == K)
{
return X;
}
// Initializing ans
int ans = -1;
// Variables to store count of 1's and 0's
// from right to left in binary
// representation of the X
int zero = 0, one = 0;
// Traversing every bit
for (int i = 0; i < 31; i++)
{
// If current bit is set i.e. '1' is in
// this place then follow below case
if ((X & (1 << i)) != 0)
{
// Increase the count of one
one += 1;
// Remaining bits we have to set
int rem = K - setBitCount + 1;
// If number of set bits are less than
// number of set bits required in this
// case we have to convert some 0's
// into 1's
if (setBitCount < K && zero >= K - setBitCount + 1 && zero >= rem)
{
// Removing the current bit from
// the number
int newAns = X;
newAns -= (1 << i);
for (int j = i - 1; j >= 0; j--)
{
// When we still have to convert
// bits from zero to one
if (rem > 0)
{
if ((newAns & (1 << j)) == 0)
{
rem -= 1;
}
newAns |= (1 << j);
}
}
// Store the maximun number in the
// ans variable
ans = Math.Max(ans, newAns);
}
// When number of set bits are greater
// than required set bits in this case
// we have to convert some 1's into 0's
else if (setBitCount > K && (setBitCount - one) <= K)
{
int newAns = X;
// Remaining number of 1's we
// still required
rem = K - (setBitCount - one);
for (int j = i; j >= 0; j--)
{
if (rem > 0 && (newAns & (1 << j)) != 0)
{
rem -= 1;
}
else if ((newAns & (1 << j)) != 0)
{
newAns -= (1 << j);
}
}
// Storing the maximum value in answer
ans = Math.Max(ans, newAns);
}
}
else
{
// If current bit is not set i.e. '0'
// is in that place
zero += 1;
}
}
return ans;
}
// Driver code
public static void Main(string[] args)
{
int X = 8;
int K = 2;
Console.WriteLine(GreatestKBits(X, K));
X = 9;
K = 4;
Console.WriteLine(GreatestKBits(X, K));
}
}
JavaScript
// Javascript implementation of the approach
// Function to return the greatest number <= X
// having at most K set bits.
function greatestKBits(X, K) {
// Finding total number of Set Bits
// in the number X
let set_bit_count = (X).toString(2).split('').filter(x => x == '1').length;
// If set_bits are equal to K then answer
// will be that number itself
if (set_bit_count == K)
return X;
// Initializing ans
let ans = -1;
// Variables to store count of 1's and 0's
// from right to left in binary
// representation of the X
let zero = 0,
one = 0;
// Traversing every bit
for (let i = 0; i < 31; i++) {
// If current bit is set i.e. '1' is in
// this place then follow below case
if (((X & (1 << i)) !== 0)) {
// Increase the count of one
++one;
// Remaining bits we have to set
let rem = K - set_bit_count + 1;
// If number of set bits are less than
// number of set bits required in this
// case we have to convert some 0's
// into 1's
if (set_bit_count < K && zero >= K - set_bit_count + 1 && zero >= rem) {
let new_ans = X;
// Removing the current bit from
// the number
new_ans -= (1 << i);
for (let j = i - 1; j >= 0; j--) {
// When we still have to convert
// bits from zero to one
if (rem > 0) {
if ((new_ans & (1 << j)) === 0)
rem--;
new_ans |= (1 << j);
}
}
// Store the maximun number in the
// ans variable
ans = Math.max(ans, new_ans);
}
// When number of set bits are greater
// than required set bits in this case
// we have to convert some 1's into 0's
else if (set_bit_count > K && (set_bit_count - one) <= K) {
let new_ans = X;
// Remaining number of 1's we
// still required
rem = K - (set_bit_count - one);
for (let j = i; j >= 0; j--) {
if (rem > 0 && (new_ans & (1 << j)) !== 0) {
rem--;
} else {
if ((new_ans & (1 << j)) !== 0)
new_ans -= (1 << j);
}
}
// Storing the maximum value in answer
ans = Math.max(ans, new_ans);
}
} else {
// If current bit is not set i.e. '0'
// is in that place
++zero;
}
}
return ans;
}
// Driver code
// First Test Case
let X = 8,
K = 2;
console.log(greatestKBits(X, K));
// Second Test Case
X = 9;
K = 4;
console.log(greatestKBits(X, K));
// This code is contributed by ragul21
Time Complexity: O(Log(N))
Auxiliary Space: O(1)
Similar Reads
Largest number less than X having at most K set bits Given an integer X > 1 and an integer K > 0, the task is to find the greatest odd number < X such that the number of 1's in its binary representation is at most K. Examples: Input: X = 10, K = 2 Output: 10 Input: X = 29, K = 2 Output: 24 Naive Approach: Starting from X - 1 check all the num
4 min read
Count numbers up to N having Kth bit set Given two integers N and K, the task is to find the count of numbers up to N having K-th (0-based indexing) bit set. Examples: Input: N = 14, K = 2Output: 7Explanation: The numbers less than equal to 14, having 2nd bit set are 4, 5, 6, 7, 12, 13, and 14. Input: N = 6, K = 1Output: 3Explanation: The
5 min read
Print first n numbers with exactly two set bits Given a number n, print first n positive integers with exactly two set bits in their binary representation.Examples : Input: n = 3Output: 3 5 6The first 3 numbers with two set bits are 3 (0011),5 (0101) and 6 (0110)Input: n = 5Output: 3 5 6 9 10 12A Simple Solution is to consider all positive intege
10 min read
Find the largest number with n set and m unset bits Given two non-negative numbers n and m. The problem is to find the largest number having n number of set bits and m number of unset bits in its binary representation.Note : 0 bits before leading 1 (or leftmost 1) in binary representation are countedConstraints: 1 <= n, 0 <= m, (m+n) <= 31Ex
6 min read
Largest set with bitwise OR equal to n Given an integer n, find the largest possible set of non-negative integers with bitwise OR equal to n.Examples: Input : n = 5 Output : arr[] = [0, 1, 4, 5] The bitwise OR of 0, 1, 4 and 5 equals 5. It is not possible to obtain a set larger than this. Input : n = 8 Output : arr[] = [0, 8] Prerequisit
5 min read