Maximize the number by flipping at most K bits
Last Updated :
16 Dec, 2022
Given an integer N, the task is to find the greatest number that can be obtained by flipping at most K bits in the binary representation of N.
Examples:
Input: N = 4, K = 1
Output: 6
The binary equivalent of 4 is 100.
On flipping the 1st 0, we get 110
which is equivalent to 6.
Input: N = 5, K = 2
Output: 7
Approach:
- If the number of 0s in the binary representation of N is less than K then flip all the 0s.
- If the number of 0s is greater than or equal to K then flip the most significant K 0s.
- Finally, print the maximized integer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void decBinary( int arr[], int n)
{
int k = log2(n);
while (n > 0) {
arr[k--] = n % 2;
n /= 2;
}
}
int binaryDec( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
int maxNum( int n, int k)
{
int l = log2(n) + 1;
int a[l] = { 0 };
decBinary(a, n);
int cn = 0;
for ( int i = 0; i < l; i++) {
if (a[i] == 0 && cn < k) {
a[i] = 1;
cn++;
}
}
return binaryDec(a, l);
}
int main()
{
int n = 4, k = 1;
cout << maxNum(n, k);
return 0;
}
|
Java
class GFG
{
static void decBinary( int arr[], int n)
{
int k = ( int )(Math.log(n) /
Math.log( 2 ));
while (n > 0 )
{
arr[k--] = n % 2 ;
n /= 2 ;
}
}
static int binaryDec( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
ans += arr[i] << (n - i - 1 );
return ans;
}
static int maxNum( int n, int k)
{
int l = ( int )(Math.log(n) /
Math.log( 2 )) + 1 ;
int a[] = new int [l];
decBinary(a, n);
int cn = 0 ;
for ( int i = 0 ; i < l; i++)
{
if (a[i] == 0 && cn < k)
{
a[i] = 1 ;
cn++;
}
}
return binaryDec(a, l);
}
public static void main (String[] args)
{
int n = 4 , k = 1 ;
System.out.println(maxNum(n, k));
}
}
|
Python3
import math
def decBinary(arr, n):
k = int (math.log2(n))
while (n > 0 ):
arr[k] = n % 2
k = k - 1
n = n / / 2
def binaryDec(arr, n):
ans = 0
for i in range ( 0 , n):
ans = ans + (arr[i] << (n - i - 1 ))
return ans
def maxNum(n, k):
l = int (math.log2(n)) + 1
a = [ 0 for i in range ( 0 , l)]
decBinary(a, n)
cn = 0
for i in range ( 0 , l):
if (a[i] = = 0 and cn < k):
a[i] = 1
cn = cn + 1
return binaryDec(a, l)
n = 4
k = 1
print (maxNum(n, k))
|
C#
using System;
class GFG
{
static void decBinary( int []arr, int n)
{
int k = ( int )(Math.Log(n) /
Math.Log(2));
while (n > 0)
{
arr[k--] = n % 2;
n /= 2;
}
}
static int binaryDec( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
static int maxNum( int n, int k)
{
int l = ( int )(Math.Log(n) /
Math.Log(2)) + 1;
int []a = new int [l];
decBinary(a, n);
int cn = 0;
for ( int i = 0; i < l; i++)
{
if (a[i] == 0 && cn < k)
{
a[i] = 1;
cn++;
}
}
return binaryDec(a, l);
}
public static void Main(String[] args)
{
int n = 4, k = 1;
Console.WriteLine(maxNum(n, k));
}
}
|
Javascript
<script>
function decBinary(arr, n) {
let k = Math.log2(n);
while (n > 0) {
arr[k--] = n % 2;
n = Math.floor(n / 2);
}
}
function binaryDec(arr, n) {
let ans = 0;
for (let i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
function maxNum(n, k) {
let l = Math.log2(n) + 1;
let a = new Array(l).fill(0);
decBinary(a, n);
let cn = 0;
for (let i = 0; i < l; i++) {
if (a[i] == 0 && cn < k) {
a[i] = 1;
cn++;
}
}
return binaryDec(a, l);
}
let n = 4, k = 1;
document.write(maxNum(n, k));
</script>
|
Time Complexity: O(log2n)
Auxiliary Space: O(log2n)
Share your thoughts in the comments
Please Login to comment...