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++
#include <bits/stdc++.h>
using namespace std;
void maximizedArray(
int arr[], int N)
{
int num, i = 0;
while (N--) {
num = arr[i];
int one = 0;
int zero = 0;
while (num) {
if (num % 2 == 0) {
zero++;
}
else {
one++;
}
num = num >> 1;
}
for ( int j = zero; j < (one + zero);
j++) {
num += (1 << j);
}
cout << num;
i++;
if (N > 0)
cout << ", " ;
}
}
int main()
{
int arr[] = { 8, 15, 9, 10, 14 };
int N = sizeof (arr) / sizeof (arr[0]);
maximizedArray(
arr, N);
return 0;
}
|
Java
class GFG{
public static void maximizedArray( int arr[],
int N)
{
int num, i = 0 ;
for ( int l = N; l > 0 ; l--)
{
num = arr[i];
int one = 0 ;
int zero = 0 ;
while (num != 0 )
{
if (num % 2 == 0 )
{
zero++;
}
else
{
one++;
}
num = num >> 1 ;
}
for ( int j = zero; j < (one + zero); j++)
{
num += ( 1 << j);
}
System.out.print(num);
i++;
if (N > 0 )
System.out.print( ", " );
}
}
public static void main(String args[])
{
int arr[] = { 8 , 15 , 9 , 10 , 14 };
int N = arr.length;
maximizedArray(arr, N);
}
}
|
Python3
def maximizedArray(arr, N):
i = 0
while (N > 0 ):
num = arr[i]
one = 0
zero = 0
while (num):
if (num % 2 = = 0 ):
zero + = 1
else :
one + = 1
num = num >> 1
for j in range (zero, (one + zero)):
num + = ( 1 << j)
print (num, end = "")
i + = 1
if (N > 0 ):
print ( ", " , end = "")
N - = 1
if __name__ = = "__main__" :
arr = [ 8 , 15 , 9 , 10 , 14 ]
N = len (arr)
maximizedArray(arr, N)
|
C#
using System;
class GFG{
public static void maximizedArray( int []arr,
int N)
{
int num, i = 0;
for ( int l = N; l > 0; l--)
{
num = arr[i];
int one = 0;
int zero = 0;
while (num != 0)
{
if (num % 2 == 0)
{
zero++;
}
else
{
one++;
}
num = num >> 1;
}
for ( int j = zero; j < (one + zero); j++)
{
num += (1 << j);
}
Console.Write(num);
i++;
if (N > 0)
Console.Write( ", " );
}
}
public static void Main(String []args)
{
int []arr = { 8, 15, 9, 10, 14 };
int N = arr.Length;
maximizedArray(arr, N);
}
}
|
Javascript
<script>
function maximizedArray(arr, N)
{
let num, i = 0;
for (let l = N; l > 0; l--)
{
num = arr[i];
let one = 0;
let zero = 0;
while (num != 0)
{
if (num % 2 == 0)
{
zero++;
}
else
{
one++;
}
num = num >> 1;
}
for (let j = zero; j < (one + zero); j++)
{
num += (1 << j);
}
document.write(num);
i++;
if (N > 0)
document.write( ", " );
}
}
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...