Given a positive 32-bit integer N, the task is to find the maximum between the value of N and the number obtained by decimal representation of reversal of binary representation of N in a 32-bit integer.
Examples:
Input: N = 6
Output: 1610612736
Explanation:
Binary representation of 6 in a 32-bit integer is 00000000000000000000000000000110 i.e., (00000000000000000000000000000110)2 = (6)10.
Reversing this binary string gives (01100000000000000000000000000000)2 = (1610612736)10.
The maximum between N and the obtained number is 1610612736. Therefore, print 1610612736.
Input: N = 1610612736
Output: 1610612736
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 reverseBin( int N)
{
string S = "" ;
int i;
for (i = 0; i < 32; i++) {
if (N & (1LL << i))
S += '1' ;
else
S += '0' ;
}
reverse(S.begin(), S.end());
int M = 0;
for (i = 0; i < 32; i++) {
if (S[i] == '1' )
M += (1LL << i);
}
return M;
}
int maximumOfTwo( int N)
{
int M = reverseBin(N);
return max(N, M);
}
int main()
{
int N = 6;
cout << maximumOfTwo(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static int reverseBin( int N)
{
String S = "" ;
int i;
for (i = 0 ; i < 32 ; i++) {
if ((N & (1L << i)) != 0 )
S += '1' ;
else
S += '0' ;
}
S = ( new StringBuilder(S)).reverse().toString();
int M = 0 ;
for (i = 0 ; i < 32 ; i++) {
if (S.charAt(i) == '1' )
M += (1L << i);
}
return M;
}
static int maximumOfTwo( int N)
{
int M = reverseBin(N);
return Math.max(N, M);
}
public static void main(String[] args)
{
int N = 6 ;
System.out.print(maximumOfTwo(N));
}
}
|
Python3
def reverseBin(N):
S = ""
i = 0
for i in range ( 32 ):
if (N & ( 1 << i)):
S + = '1'
else :
S + = '0'
S = list (S)
S = S[:: - 1 ]
S = ''.join(S)
M = 0
for i in range ( 32 ):
if (S[i] = = '1' ):
M + = ( 1 << i)
return M
def maximumOfTwo(N):
M = reverseBin(N)
return max (N, M)
if __name__ = = '__main__' :
N = 6
print (maximumOfTwo(N))
|
C#
using System;
class GFG{
static string ReverseString( string s)
{
char [] array = s.ToCharArray();
Array.Reverse(array);
return new string (array);
}
public static int reverseBin( int N)
{
string S = "" ;
int i;
for (i = 0; i < 32; i++) {
if ((N & (1L << i)) != 0)
S += '1' ;
else
S += '0' ;
}
S = ReverseString(S);
int M = 0;
for (i = 0; i < 32; i++) {
if (S[i] == '1' )
M += (1 << i);
}
return M;
}
static int maximumOfTwo( int N)
{
int M = reverseBin(N);
return Math.Max(N, M);
}
static void Main()
{
int N = 6;
Console.Write(maximumOfTwo(N));
}
}
|
Javascript
<script>
function reverseBin(N)
{
let S = "" ;
let i;
for (i = 0; i < 32; i++) {
if (N & (1 << i))
S += '1' ;
else
S += '0' ;
}
S = S.split( "" ).reverse().join( "" );
let M = 0;
for (i = 0; i < 32; i++) {
if (S[i] == '1' )
M += (1 << i);
}
return M;
}
function maximumOfTwo(N)
{
let M = reverseBin(N);
return Math.max(N, M);
}
let N = 6;
document.write(maximumOfTwo(N));
</script>
|
Time Complexity: O(32)
Auxiliary Space: O(1)