Rearrange array to make decimal equivalents of reversed binary representations of array elements sorted
Last Updated :
10 Oct, 2022
Given an array arr[] consisting of N positive integers, the task is to rearrange the array such that the reversed binary representation of all the array elements is sorted.
If the decimal equivalent of reversed binary representations of two or more array elements is equal, then the original value is taken into consideration while rearranging the array.
Examples:
Input: arr[] = {43, 52, 61, 41}
Output: 52 41 61 43
Explanation:
Below are the reversed binary representation of the array elements:
- 43 –> (101011)2 –> reversed –> 53.
- 52 –> (110100)2 –> reversed –> 11.
- 61 –> (111101)2 –> reversed –> 47.
- 41 –> (101001)2 –> reversed –> 37.
Therefore, after rearranging the array element as {52, 41, 61, 43}, the reversed binary representation of rearranged array elements is in sorted order.
Input: arr[] = {5, 3, 6, 2, 4}
Output: 2 4 3 6 5
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int keyFunc( int n)
{
int rev = 0;
while (n > 0)
{
rev = rev << 1;
if (n & 1 == 1)
rev = rev ^ 1;
n = n >> 1;
}
return rev;
}
vector<vector< int >> getNew(vector< int > arr)
{
vector<vector< int >> ans;
for ( int i:arr)
ans.push_back({keyFunc(i), i});
return ans;
}
vector< int > getArr(vector<vector< int > > arr){
vector< int > ans;
for ( auto i:arr)
ans.push_back(i[1]);
return ans;
}
void sortArray(vector< int > arr)
{
vector<vector< int > > newArr = getNew(arr);
sort(newArr.begin(),newArr.end());
arr = getArr(newArr);
int n = arr.size();
cout<< "[" ;
for ( int i = 0; i < n - 1; i++)
cout << arr[i] << ", " ;
cout << arr[n - 1] << "]" ;
}
int main()
{
vector< int > arr = {43, 52, 61, 41};
sortArray(arr);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int keyFunc( int n)
{
int rev = 0 ;
while (n > 0 )
{
rev = rev << 1 ;
if ((n & 1 ) == 1 )
rev = rev ^ 1 ;
n = n >> 1 ;
}
return rev;
}
static int [][] getNew( int arr[])
{
int ans[][] = new int [arr.length][ 2 ];
for ( int i = 0 ; i < arr.length; i++)
ans[i] = new int []{ keyFunc(arr[i]), arr[i] };
return ans;
}
static int [] getArr( int [][] arr)
{
int ans[] = new int [arr.length];
int idx = 0 ;
for ( int i[] : arr)
ans[idx++] = i[ 1 ];
return ans;
}
static void sortArray( int arr[])
{
int [][] newArr = getNew(arr);
Arrays.sort(newArr, (a, b) -> {
if (Integer.compare(a[ 0 ], b[ 0 ]) == 0 )
return Integer.compare(a[ 1 ], b[ 1 ]);
return Integer.compare(a[ 0 ], b[ 0 ]);
});
arr = getArr(newArr);
int n = arr.length;
System.out.print( "[" );
for ( int i = 0 ; i < n - 1 ; i++)
System.out.print(arr[i] + ", " );
System.out.print(arr[n - 1 ] + "]" );
}
public static void main(String[] args)
{
int arr[] = { 43 , 52 , 61 , 41 };
sortArray(arr);
}
}
|
Python3
def keyFunc(n):
rev = 0
while (n > 0 ):
rev = rev << 1
if (n & 1 = = 1 ):
rev = rev ^ 1
n = n >> 1
return rev
def getNew(arr):
ans = []
for i in arr:
ans.append([keyFunc(i), i])
return ans
def getArr(arr):
ans = []
for i in arr:
ans.append(i[ 1 ])
return ans
def sortArray(arr):
newArr = getNew(arr)
newArr.sort()
arr = getArr(newArr)
print (arr)
arr = [ 43 , 52 , 61 , 41 ]
sortArray(arr)
|
C#
using System;
class GFG {
static int keyFunc( int n)
{
int rev = 0;
while (n > 0) {
rev = rev << 1;
if ((n & 1) == 1)
rev = rev ^ 1;
n = n >> 1;
}
return rev;
}
static int [][] getNew( int [] arr)
{
int [][] ans = new int [arr.Length][];
for ( int i = 0; i < arr.Length; i++)
ans[i] = new int [] { keyFunc(arr[i]), arr[i] };
return ans;
}
static int [] getArr( int [][] arr)
{
int [] ans = new int [arr.Length];
int idx = 0;
foreach ( var i in arr) ans[idx++] = i[1];
return ans;
}
static void sortArray( int [] arr)
{
int [][] newArr = getNew(arr);
Array.Sort(
newArr, new Comparison< int []>((a, b) => {
return (a[0] == b[0]) ? (a[1] - b[1])
: (a[0] - b[0]);
}));
arr = getArr(newArr);
int n = arr.Length;
Console.Write( "[" );
for ( int i = 0; i < n - 1; i++)
Console.Write(arr[i] + ", " );
Console.Write(arr[n - 1] + "]" );
}
public static void Main( string [] args)
{
int [] arr = { 43, 52, 61, 41 };
sortArray(arr);
}
}
|
Javascript
<script>
function keyFunc(n) {
let rev = 0;
while (n > 0) {
rev = rev << 1;
if (n & 1 == 1)
rev = rev ^ 1;
n = n >> 1;
}
return rev;
}
function getNew(arr) {
let ans = [];
for (let i of arr)
ans.push([keyFunc(i), i]);
return ans;
}
function getArr(arr) {
let ans = [];
for (let i of arr)
ans.push(i[1]);
return ans;
}
function sortArray(arr) {
let newArr = getNew(arr);
newArr.sort();
arr = getArr(newArr);
let n = arr.length;
document.write( "[" );
for (let i = 0; i < n - 1; i++)
document.write(arr[i] + ", " );
document.write(arr[n - 1] + "]" );
}
let arr = [43, 52, 61, 41];
sortArray(arr);
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Space-Optimized Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int keyFunc( int n)
{
int rev = 0;
while (n > 0) {
rev = rev << 1;
if (n & 1 == 1)
rev = rev ^ 1;
n = n >> 1;
}
return rev;
}
bool compare( int a, int b)
{
return keyFunc(a) < keyFunc(b);
}
void sortArray( int arr[], int n)
{
sort(arr, arr + n, compare);
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 43, 52, 61, 41 };
int n = 4;
sortArray(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int keyFunc(String num)
{
int n = Integer.parseInt(num);
int rev = 0 ;
while (n > 0 ) {
rev = rev << 1 ;
if ((n & 1 ) == 1 )
rev = rev ^ 1 ;
n = n >> 1 ;
}
return rev;
}
public static void sortArray(List<Integer> arrInt,
int n)
{
List<String> arr = new ArrayList<String>();
for ( int i = 0 ; i < arrInt.size(); i++) {
arr.add(Integer.toString(arrInt.get(i)));
}
Collections.sort(arr, new Comparator<String>() {
@Override
public int compare( final String a, String b)
{
if (keyFunc(a) < keyFunc(b))
return - 1 ;
return 1 ;
}
});
for ( int i = 0 ; i < n; i++)
System.out.print(arr.get(i) + " " );
}
public static void main(String[] args)
{
List<Integer> arr = new ArrayList<Integer>();
arr.add( 43 );
arr.add( 52 );
arr.add( 61 );
arr.add( 41 );
int n = 4 ;
sortArray(arr, n);
}
}
|
Python3
def keyFunc(n):
rev = 0
while (n > 0 ):
rev = rev << 1
if (n & 1 = = 1 ):
rev = rev ^ 1
n = n >> 1
return rev
def sortArray(arr):
arr = sorted (arr, key = keyFunc)
print (arr)
arr = [ 43 , 52 , 61 , 41 ]
sortArray(arr)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int keyFunc( int n)
{
int rev = 0;
while (n > 0) {
rev = rev << 1;
if ((n & 1) == 1)
rev = rev ^ 1;
n = n >> 1;
}
return rev;
}
public static int compare( int a, int b)
{
return keyFunc(a) - keyFunc(b);
}
public static void sortArray( int [] arr, int n)
{
Array.Sort(arr, compare);
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
public static void Main( string [] args)
{
int [] arr = { 43, 52, 61, 41 };
int n = 4;
sortArray(arr, n);
}
}
|
Javascript
function keyFunc(n)
{
var rev = 0;
while (n > 0)
{
rev = rev << 1;
if (n & 1 == 1)
rev = rev ^ 1;
n = n >> 1;
}
return rev;
}
function sortArray(arr)
{
arr.sort((a, b) => keyFunc(a) > keyFunc(b));
console.log(arr);
}
var arr = [43, 52, 61, 41];
sortArray(arr);
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...