Generate an Array such with elements maximized through swapping bits
Last Updated :
12 May, 2021
Given an array arr[], the task is to generate a modified array such that all its elements are maximized by swapping of bits.
Examples:
Input: arr[] = {10, 15}
Output: 12, 15
Explanation:
Binary representation of (10)10 = (1010)2. Swap the second and third bit to get the binary representation as (1100)2 = (12)10.
For 15, its binary representation is 1111, which can not be further changed to get greater value.
Input: arr[] = {8, 15, 9, 10, 14}
Output: 8, 15, 12, 12, 14
Approach:
Follow the steps below to solve the problem:
- Count the number of set and unset bits in every array element.
- Shift all the set bits to the left(MSB) and all the unset bits to the right(LSB) to maximize the array elements.
- If count of set bits or unset bits is equal to the number of bits of the array element, then that element cannot be altered( e.g. (7)10 = (111)2
- Print the maximized elements in the array
Below is the implementation of the above approach:
C++
// C++ implementation to
// find maximum sequence by
// swapping bits
#include <bits/stdc++.h>
using namespace std;
// Function to generate the maximized
// array elements
void maximizedArray(
int arr[], int N)
{
int num, i = 0;
// Traverse the array
while (N--) {
num = arr[i];
int one = 0;
int zero = 0;
// Iterate to count set and
// unset bits
while (num) {
// Count of unset bits
if (num % 2 == 0) {
zero++;
}
else {
// Count of set bits
one++;
}
// Bitwise right shift
num = num >> 1;
}
for (int j = zero; j < (one + zero);
j++) {
// Shifting all 1's to MSB
// and 0's to LSB
num += (1 << j);
}
cout << num;
i++;
if (N > 0)
cout << ", ";
}
}
// Driver Code
int main()
{
int arr[] = { 8, 15, 9, 10, 14 };
int N = sizeof(arr) / sizeof(arr[0]);
maximizedArray(
arr, N);
return 0;
}
Java
// Java implementation to find
// maximum sequence by swapping bits
class GFG{
// Function to generate the maximized
// array elements
public static void maximizedArray(int arr[],
int N)
{
int num, i = 0;
// Traverse the array
for(int l = N; l > 0; l--)
{
num = arr[i];
int one = 0;
int zero = 0;
// Iterate to count set and
// unset bits
while (num != 0)
{
// Count of unset bits
if (num % 2 == 0)
{
zero++;
}
else
{
// Count of set bits
one++;
}
// Bitwise right shift
num = num >> 1;
}
for(int j = zero; j < (one + zero); j++)
{
// Shifting all 1's to MSB
// and 0's to LSB
num += (1 << j);
}
System.out.print(num);
i++;
if (N > 0)
System.out.print(", ");
}
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 8, 15, 9, 10, 14 };
int N = arr.length;
maximizedArray(arr, N);
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 implementation to find
# maximum sequence by swapping bits
# Function to generate the maximized
# array elements
def maximizedArray(arr, N):
i = 0
# Traverse the array
while (N > 0):
num = arr[i]
one = 0
zero = 0
# Iterate to count set and
# unset bits
while (num):
# Count of unset bits
if (num % 2 == 0):
zero += 1
else:
# Count of set bits
one += 1
# Bitwise right shift
num = num >> 1
for j in range(zero, (one + zero)):
# Shifting all 1's to MSB
# and 0's to LSB
num += (1 << j)
print(num, end = "")
i += 1
if (N > 0):
print(", ", end = "")
N -= 1
# Driver Code
if __name__ == "__main__":
arr = [ 8, 15, 9, 10, 14 ]
N = len(arr)
maximizedArray(arr, N)
# This code is contributed by chitranayal
C#
// C# implementation to find
// maximum sequence by swapping bits
using System;
class GFG{
// Function to generate the maximized
// array elements
public static void maximizedArray(int []arr,
int N)
{
int num, i = 0;
// Traverse the array
for(int l = N; l > 0; l--)
{
num = arr[i];
int one = 0;
int zero = 0;
// Iterate to count set and
// unset bits
while (num != 0)
{
// Count of unset bits
if (num % 2 == 0)
{
zero++;
}
else
{
// Count of set bits
one++;
}
// Bitwise right shift
num = num >> 1;
}
for(int j = zero; j < (one + zero); j++)
{
// Shifting all 1's to MSB
// and 0's to LSB
num += (1 << j);
}
Console.Write(num);
i++;
if (N > 0)
Console.Write(", ");
}
}
// Driver Code
public static void Main(String []args)
{
int []arr = { 8, 15, 9, 10, 14 };
int N = arr.Length;
maximizedArray(arr, N);
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// Javascript implementation to find
// maximum sequence by swapping bits
// Function to generate the maximized
// array elements
function maximizedArray(arr, N)
{
let num, i = 0;
// Traverse the array
for(let l = N; l > 0; l--)
{
num = arr[i];
let one = 0;
let zero = 0;
// Iterate to count set and
// unset bits
while (num != 0)
{
// Count of unset bits
if (num % 2 == 0)
{
zero++;
}
else
{
// Count of set bits
one++;
}
// Bitwise right shift
num = num >> 1;
}
for(let j = zero; j < (one + zero); j++)
{
// Shifting all 1's to MSB
// and 0's to LSB
num += (1 << j);
}
document.write(num);
i++;
if (N > 0)
document.write(", ");
}
}
// Driver Code
let arr = [ 8, 15, 9, 10, 14 ];
let N = arr.length;
maximizedArray(arr, N);
</script>
Output: 8, 15, 12, 12, 14
Time Complexity: O(Nlog2N)
Auxiliary Space: O(1)
Similar Reads
Find element with the maximum set bits in an array Given an array arr[]. The task is to find an element from arr[] which has the maximum count of set bits.Examples: Input: arr[] = {10, 100, 1000, 10000} Output: 1000 Binary(10) = 1010 (2 set bits) Binary(100) = 1100100 (3 set bits) Binary(1000) = 1111101000 (6 set bits) Binary(10000) = 10011100010000
5 min read
Maximize total set bits of elements in N sized Array with sum M Given two integers N and M denoting the size of an array and the sum of the elements of the array, the task is to find the maximum possible count of total set bits of all the elements of the array such that the sum of the elements is M. Examples: Input: N = 1, M = 15Output: 4Explanation: Since N =1,
8 min read
Maximize count of elements reaching the end of an Array Given an array arr[] consisting of N integers, where each element denotes the maximum number of elements that can be placed on that index and an integer X, which denotes the maximum indices that can be jumped from an index, the task is to find the number of elements that can reach the end of the arr
15 min read
Maximum set bit sum in array without considering adjacent elements Given an array of integers arr[]. The task is to find the maximum sum of set bits(of the array elements) without adding the set bits of adjacent elements of the array. Examples: Input : arr[] = {1, 2, 4, 5, 6, 7, 20, 25} Output : 9 Input : arr[] = {5, 7, 9, 5, 13, 7, 20, 25} Output : 11 Approach: Fi
8 min read
Maximize the last Array element as per the given conditions Given an array arr[] consisting of N integers, rearrange the array such that it satisfies the following conditions: arr[0] must be 1.Difference between adjacent array elements should not exceed 1, that is, arr[i] - arr[i-1] ? 1 for all 1 ? i < N. The permissible operations are as follows: Rearran
5 min read