Given an array arr[] consisting of N integers, the task is to modify the array by replacing each array element with the number obtained by reversing their respective binary representations and sort the modified array.
Examples:
Input: arr[ ] = {43, 422, 132}
Output: 33 53 203
Explanation:
The binary representation of the array elements are {101011, 110100110, 10000100}.
Reversed binary representations are {110101, 011001011, 0010000}.
Equivalent numeric values of the reversed binary representations are {53, 203, 33}.
Sorted order of these elements are {33, 53, 203}.
Input: arr[ ] ={ 98, 43, 66, 83}
Output: 33 35 53 101
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int binaryToDecimal(string n)
{
string num = n;
int dec_value = 0;
int base = 1;
int len = num.length();
for ( int i = len - 1; i >= 0; i--) {
if (num[i] == '1' )
dec_value += base;
base = base * 2;
}
return dec_value;
}
string decimalToBinary( int n)
{
string binstr = "" ;
while (n > 0) {
binstr += (n % 2 + 48);
n = n / 2;
}
return binstr;
}
int reversedBinaryDecimal( int N)
{
string decimal_to_binar
= decimalToBinary(N);
int binary_to_decimal
= binaryToDecimal(decimal_to_binar);
return binary_to_decimal;
}
void printSortedArray( int arr[], int size)
{
sort(arr, arr + size);
for ( int i = 0; i < size; i++)
cout << arr[i] << " " ;
cout << endl;
}
void modifyArray( int arr[], int size)
{
for ( int i = 0; i < size; i++) {
arr[i] = reversedBinaryDecimal(
arr[i]);
}
printSortedArray(arr, size);
}
int main()
{
int arr[] = { 98, 43, 66, 83 };
int n = sizeof (arr) / sizeof (arr[0]);
modifyArray(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int binaryToDecimal(String n)
{
String num = n;
int dec_value = 0 ;
int base = 1 ;
int len = num.length();
for ( int i = len - 1 ; i >= 0 ; i--)
{
if (num.charAt(i) == '1' )
dec_value += base;
base = base * 2 ;
}
return dec_value;
}
static String decimalToBinary( int n)
{
String binstr = "" ;
while (n > 0 )
{
binstr += ( char )(n % 2 + 48 );
n = n / 2 ;
}
return binstr;
}
static int reversedBinaryDecimal( int N)
{
String decimal_to_binar = decimalToBinary(N);
int binary_to_decimal = binaryToDecimal(
decimal_to_binar);
return binary_to_decimal;
}
static void printSortedArray( int arr[], int size)
{
Arrays.sort(arr);
for ( int i = 0 ; i < size; i++)
System.out.print(arr[i] + " " );
System.out.println();
}
static void modifyArray( int arr[], int size)
{
for ( int i = 0 ; i < size; i++)
{
arr[i] = reversedBinaryDecimal(arr[i]);
}
printSortedArray(arr, size);
}
public static void main(String[] args)
{
int arr[] = { 98 , 43 , 66 , 83 };
int n = arr.length;
modifyArray(arr, n);
}
}
|
Python3
def binaryToDecimal(n):
num = n
dec_value = 0
base = 1
length = len (num)
for i in range (length - 1 , - 1 , - 1 ):
if (num[i] = = '1' ):
dec_value + = base
base = base * 2
return dec_value
def decimalToBinary(n):
binstr = ""
while (n > 0 ):
binstr + = chr (n % 2 + 48 )
n = n / / 2
return binstr
def reversedBinaryDecimal(N):
decimal_to_binar = decimalToBinary(N)
binary_to_decimal = binaryToDecimal(
decimal_to_binar)
return binary_to_decimal
def printSortedArray(arr, size):
arr.sort()
for i in range (size):
print (arr[i], end = " " )
print ()
def modifyArray(arr, size):
for i in range (size):
arr[i] = reversedBinaryDecimal(arr[i])
printSortedArray(arr, size)
if __name__ = = "__main__" :
arr = [ 98 , 43 , 66 , 83 ]
n = len (arr)
modifyArray(arr, n)
|
Javascript
<script>
function binaryToDecimal(n)
{
let num = n;
let dec_value = 0;
let base = 1;
let len = num.length;
for (let i = len - 1; i >= 0; i--)
{
if (num[i] == '1' )
dec_value += base;
base = base * 2;
}
return dec_value;
}
function decimalToBinary(n)
{
let binstr = "" ;
while (n > 0)
{
binstr += String.fromCharCode(n % 2 + 48);
n = Math.floor(n / 2);
}
return binstr;
}
function reversedBinaryDecimal(N)
{
let decimal_to_binar = decimalToBinary(N);
let binary_to_decimal = binaryToDecimal(
decimal_to_binar);
return binary_to_decimal;
}
function printSortedArray(arr, size)
{
arr.sort( function (a, b){ return a - b;});
for (let i = 0; i < size; i++)
document.write(arr[i] + " " );
document.write( "<br>" )
}
function modifyArray(arr, size)
{
for (let i = 0; i < size; i++)
{
arr[i] = reversedBinaryDecimal(
arr[i]);
}
printSortedArray(arr, size);
}
let arr = [ 98, 43, 66, 83 ];
let n = arr.length;
modifyArray(arr, n);
</script>
|
C#
using System;
using System.Linq;
class GFG
{
static int BinaryToDecimal( string n)
{
string num = n;
int dec_value = 0;
int base1 = 1;
int len = num.Length;
for ( int i = len - 1; i >= 0; i--)
{
if (num[i] == '1' )
dec_value += base1;
base1 = base1 * 2;
}
return dec_value;
}
static string DecimalToBinary( int n)
{
string binstr = "" ;
while (n > 0)
{
binstr += ( char )(n % 2 + 48);
n = n / 2;
}
return new string (binstr.ToArray());
}
static int ReversedBinaryDecimal( int N)
{
string decimal_to_binar = DecimalToBinary(N);
int binary_to_decimal = BinaryToDecimal(
decimal_to_binar);
return binary_to_decimal;
}
static void PrintSortedArray( int [] arr, int size)
{
Array.Sort(arr);
for ( int i = 0; i < size; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
}
static void ModifyArray( int [] arr, int size)
{
for ( int i = 0; i < size; i++)
{
arr[i] = ReversedBinaryDecimal(arr[i]);
}
PrintSortedArray(arr, size);
}
public static void Main( string [] args)
{
int [] arr = { 98, 43, 66, 83 };
int n = arr.Length;
ModifyArray(arr, n);
}
}
|
Time Complexity: O(NlogN)
Auxiliary Space: O(log2M), where M denotes the maximum element present in the array.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Jan, 2023
Like Article
Save Article