Maximize a given unsigned number by swapping bits at it's extreme positions.
Last Updated :
15 Nov, 2022
Given a number maximize it by swapping bits at its extreme positions i.e. at the first and last position, second and second last position, and so on.
Examples:
Input : 4 (0000...0100)
Output : 536870912 (0010...0000)
In the above example swapped 3rd and 3rd last
bit to maximize the given unsigned number.
Input : 12 (00000...1100)
Output : 805306368 (0011000...0000)
In the above example 3rd and 3rd last bit and
4th and 4th last bit are swapped to maximize
the given unsigned number.
Naive Approach:
1. Convert the number into its bit representation and store its bit representation in an array.
2. Traverse the array from both ends, if the less significant bit of the bit representation is greater than the more significant bit i.e. if the less significant bit is 1 and the more significant bit is 0 then swap them else take no action.
3. Convert the obtained binary representation back to the number.
Efficient Approach:
1. Create a copy of the original number because the original number would be modified, iteratively obtaining the bits at the extreme positions.
2. If less significant bit is 1 and the more significant bit is 0 then swap the bits in the bit from only, and continue the process until less significant bit's position is less than more significant bit's position.
3. Display the maximized number.
C++
// C++ program to find maximum number by
// swapping extreme bits.
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
ull findMax(ull num)
{
ull num_copy = num;
/* Traverse bits from both extremes */
int j = sizeof(unsigned long long int) * 8 - 1;
int i = 0;
while (i < j) {
// Obtaining i-th and j-th bits
int m = (num_copy >> i) & 1;
int n = (num_copy >> j) & 1;
/* Swapping the bits if lesser significant
is greater than higher significant
bit and accordingly modifying the number */
if (m > n) {
int x = (1 << i | 1 << j);
num = num ^ x;
}
i++;
j--;
}
return num;
}
// Driver code
int main()
{
ull num = 4;
cout << findMax(num);
return 0;
}
Java
// Java program to find maximum number by
// swapping extreme bits.
class GFG {
static int findMax(int num) {
byte size_of_int = 4;
int num_copy = num;
/* Traverse bits from both extremes */
int j = size_of_int * 8 - 1;
int i = 0;
while (i < j) {
// Obtaining i-th and j-th bits
int m = (num_copy >> i) & 1;
int n = (num_copy >> j) & 1;
/* Swapping the bits if lesser significant
is greater than higher significant
bit and accordingly modifying the number */
if (m > n) {
int x = (1 << i | 1 << j);
num = num ^ x;
}
i++;
j--;
}
return num;
}
// Driver code
static public void main(String[] args) {
int num = 4;
System.out.println(findMax(num));
}
}
// This code is contributed by 29AjayKumar
Python 3
# Python 3 program to find maximum number
# by swapping extreme bits.
def findMax( num):
num_copy = num
# Traverse bits from both extremes
j = 4 * 8 - 1;
i = 0
while (i < j) :
# Obtaining i-th and j-th bits
m = (num_copy >> i) & 1
n = (num_copy >> j) & 1
# Swapping the bits if lesser significant
# is greater than higher significant
# bit and accordingly modifying the number
if (m > n) :
x = (1 << i | 1 << j)
num = num ^ x
i += 1
j -= 1
return num
# Driver code
if __name__ == "__main__":
num = 4
print(findMax(num))
# This code is contributed by ita_c
C#
// C# program to find maximum number by
// swapping extreme bits.
using System;
public class GFG {
static int findMax(int num) {
byte size_of_int = 4;
int num_copy = num;
/* Traverse bits from both extremes */
int j = size_of_int * 8 - 1;
int i = 0;
while (i < j) {
// Obtaining i-th and j-th bits
int m = (num_copy >> i) & 1;
int n = (num_copy >> j) & 1;
/* Swapping the bits if lesser significant
is greater than higher significant
bit and accordingly modifying the number */
if (m > n) {
int x = (1 << i | 1 << j);
num = num ^ x;
}
i++;
j--;
}
return num;
}
// Driver code
static public void Main() {
int num = 4;
Console.Write(findMax(num));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program to find maximum number by
// swapping extreme bits.
function findMax(num)
{
let num_copy = num;
/* Traverse bits from both extremes */
let j = 4 * 8 - 1;
let i = 0;
while (i < j) {
// Obtaining i-th and j-th bits
let m = (num_copy >> i) & 1;
let n = (num_copy >> j) & 1;
/* Swapping the bits if lesser significant
is greater than higher significant
bit and accordingly modifying the number */
if (m > n) {
let x = (1 << i | 1 << j);
num = num ^ x;
}
i++;
j--;
}
return num;
}
// Driver code
let num = 4;
document.write(findMax(num));
// This code is contributed by Manoj.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Maximize given integer by swapping pairs of unequal bits 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 : 11Output : 14E
7 min read
Minimize cost to bring maximum element at Kth position by swapping Given two integers N and K and an array of N positive integers (i.e., a0, a1, a2...., an-1), the task is to find the minimum cost to bring maximum value to Kth position by swapping (1-based indexing). The cost of swapping the elements is defined as the sum of values of swapping elements. Example: In
5 min read
Maximum Number by swapping digits between positions with same parity Given a positive integer N, the task is to find the largest possible value of N after any number of swaps is made between digits that are present in positions with the same parity. Examples: Input: N = 12345Output: 54321Explanation: Swap 1 with 5 [both in odd positions], and 2 with 4 [both in even p
7 min read
Smallest number whose set bits are maximum in a given range Given a positive integer 'l' and 'r'. Find the smallest number 'n' such that l <= n <= r and count of the number of set bits(number of '1's in binary representation) is as maximum as possible. Examples : Input: 1 4Output: 3Explanation:Binary representation from '1' to '4':110 = 0012210 = 01023
9 min read
Maximize Bitwise AND of Array by replacing at most one element Given an array arr[] containing N positive integers, the task is to maximize bitwise AND of the arr[] by picking at most one element of the arr[] and increment or decrement it by any value. Examples: Input: arr[] = {1, 2, 3}Output: 2Explanation: Following are the operations performed to maximize the
5 min read