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 seggreagating 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 :
Java
// Java implementation// of the above approachimport java.io.*;class GFG { // Function to return the maximum possible // value that can be obtained from the // given integer by performing given operations public static int findMaxNum(int num) { // Convert to equivalent // binary representation String binaryNumber = Integer.toBinaryString(num); // Stores binary representation // of the maximized value String maxBinaryNumber = ""; // Store the count of 0's and 1's int count0 = 0, count1 = 0; // Stores the total Number of Bits int N = binaryNumber.length(); for (int i = 0; i < N; i++) { // If current bit is set if (binaryNumber.charAt(i) == '1') { // Increment Count1 count1++; } else { // Increment Count0 count0++; } } // Shift all set bits to left // to maximize the value for (int i = 0; i < count1; i++) { maxBinaryNumber += '1'; } // Shift all unset bits to right // to maximize the value for (int i = 0; i < count0; i++) { maxBinaryNumber += '0'; } // Return the maximized value return Integer.parseInt( maxBinaryNumber, 2); } // Driver Code public static void main(String[] args) { // Given "Input int N = 11; // Function call to find the // Maximum Possible Number System.out.println(findMaxNum(N)); }} |
Python3
# Python3 implementation# of the above approach# Function to return the maximum possible# value that can be obtained from the# given integer by performing given operationsdef findMaxNum(num): # Convert to equivalent # binary representation binaryNumber = bin(num)[2:] # Stores binary representation # of the maximized value maxBinaryNumber = "" # Store the count of 0's and 1's count0, count1 = 0, 0 # Stores the total Number of Bits N = len(binaryNumber) for i in range(N): # If current bit is set if (binaryNumber[i] == '1'): # Increment Count1 count1 += 1 else: # Increment Count0 count0 += 1 # Shift all set bits to left # to maximize the value for i in range(count1): maxBinaryNumber += '1' # Shift all unset bits to right # to maximize the value for i in range(count0): maxBinaryNumber += '0' # Return the maximized value return int(maxBinaryNumber, 2)# Driver Codeif __name__ == '__main__': # Given "Input N = 11 # Function call to find the # Maximum Possible Number print(findMaxNum(N)) # This code is contributed by mohit kumar 29. |
14
Time Complexity : O(log(N))
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

