Given a positive integer N, the task is to determine the maximum possible integer that can be formed by performing the following operations on the given integer N:
- Convert the integer into its binary representation.
- Swap only unequal bits in its binary representation.
Examples:
Input : 11
Output : 14
Explanation :
- (11)10 = (1011)2
- Swap 0th bit with 2nd bit to get(1110)2 = (14)10
Input : 9
Output : 12
Approach: Follow the given steps to solve the problem
- Count the number of set bits in the number N.
- In any binary string, using the above given operations, all set bits could be moved to the left and all unset bits can be shifted towards the right. This is because any pair of unequal bits (0, 1) can be swapped, i.e. “0110110” can be converted to “1111000” using 3 operations
- Since, by segregating all set bits towards the left and all unset bits towards the right maximizes the given integer.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxNum( int num)
{
bitset<4> b(num);
string binaryNumber = b.to_string();
string maxBinaryNumber = "" ;
int count0 = 0, count1 = 0;
int N = 4;
for ( int i = 0; i < N; i++) {
if (binaryNumber[i] == '1' ) {
count1++;
}
else {
count0++;
}
}
for ( int i = 0; i < count1; i++) {
maxBinaryNumber += '1' ;
}
for ( int i = 0; i < count0; i++) {
maxBinaryNumber += '0' ;
}
return stoi(maxBinaryNumber, 0, 2);
}
int main()
{
int N = 11;
cout << findMaxNum(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int findMaxNum( int num)
{
String binaryNumber
= Integer.toBinaryString(num);
String maxBinaryNumber = "" ;
int count0 = 0 , count1 = 0 ;
int N = binaryNumber.length();
for ( int i = 0 ; i < N; i++) {
if (binaryNumber.charAt(i) == '1' ) {
count1++;
}
else {
count0++;
}
}
for ( int i = 0 ; i < count1; i++) {
maxBinaryNumber += '1' ;
}
for ( int i = 0 ; i < count0; i++) {
maxBinaryNumber += '0' ;
}
return Integer.parseInt(
maxBinaryNumber, 2 );
}
public static void main(String[] args)
{
int N = 11 ;
System.out.println(findMaxNum(N));
}
}
|
Python3
def findMaxNum(num):
binaryNumber = bin (num)[ 2 :]
maxBinaryNumber = ""
count0, count1 = 0 , 0
N = len (binaryNumber)
for i in range (N):
if (binaryNumber[i] = = '1' ):
count1 + = 1
else :
count0 + = 1
for i in range (count1):
maxBinaryNumber + = '1'
for i in range (count0):
maxBinaryNumber + = '0'
return int (maxBinaryNumber, 2 )
if __name__ = = '__main__' :
N = 11
print (findMaxNum(N))
|
C#
using System;
public class GFG
{
public static int findMaxNum( int num)
{
string binaryNumber = Convert.ToString(num, 2);
string maxBinaryNumber = "" ;
int count0 = 0, count1 = 0;
int N = binaryNumber.Length;
for ( int i = 0; i < N; i++)
{
if (binaryNumber[i] == '1' )
{
count1++;
}
else
{
count0++;
}
}
for ( int i = 0; i < count1; i++)
{
maxBinaryNumber += '1' ;
}
for ( int i = 0; i < count0; i++)
{
maxBinaryNumber += '0' ;
}
return Convert.ToInt32(maxBinaryNumber, 2);
}
static public void Main()
{
int N = 11;
Console.WriteLine(findMaxNum(N));
}
}
|
Javascript
<script>
function findMaxNum(num)
{
var binaryNumber
= Number(num).toString(2);;
var maxBinaryNumber = "" ;
var count0 = 0, count1 = 0;
var N = binaryNumber.length;
for (i = 0; i < N; i++) {
if (binaryNumber.charAt(i) == '1' ) {
count1++;
}
else {
count0++;
}
}
for (i = 0; i < count1; i++) {
maxBinaryNumber += '1' ;
}
for (i = 0; i < count0; i++) {
maxBinaryNumber += '0' ;
}
return parseInt(maxBinaryNumber,2);
}
var N = 11;
document.write(findMaxNum(N));
</script>
|
Time Complexity : O(log(N))
Auxiliary Space : O(1)