Find the maximum between N and the number formed by reversing 32-bit binary representation of N
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:
- Calculate the binary string of the number N and store it in a string S.
- Reverse the string S.
- Calculate the decimal value of the newly reversed string S in a variable, say M.
- After completing the above steps, print the maximum value of N and M as the result.
Below is the implementation of the above approach:
C++
// C++ program for tha above approach#include <bits/stdc++.h>using namespace std;// Function that obtains the number// using said operations from Nint reverseBin(int N){ // Stores the binary representation // of the number N string S = ""; int i; // Find the binary representation // of the number N for (i = 0; i < 32; i++) { // Check for the set bits if (N & (1LL << i)) S += '1'; else S += '0'; } // Reverse the string S reverse(S.begin(), S.end()); // Stores the obtained number int M = 0; // Calculating the decimal value for (i = 0; i < 32; i++) { // Check for set bits if (S[i] == '1') M += (1LL << i); } return M;}// Function to find the maximum value// between N and the obtained numberint maximumOfTwo(int N){ int M = reverseBin(N); return max(N, M);}// Driver Codeint main(){ int N = 6; cout << maximumOfTwo(N); return 0;} |
Java
// Java program for the above approachimport java.io.*;import java.lang.*;import java.util.*;public class GFG { // Function that obtains the number // using said operations from N static int reverseBin(int N) { // Stores the binary representation // of the number N String S = ""; int i; // Find the binary representation // of the number N for (i = 0; i < 32; i++) { // Check for the set bits if ((N & (1L << i)) != 0) S += '1'; else S += '0'; } // Reverse the string S S = (new StringBuilder(S)).reverse().toString(); // Stores the obtained number int M = 0; // Calculating the decimal value for (i = 0; i < 32; i++) { // Check for set bits if (S.charAt(i) == '1') M += (1L << i); } return M; } // Function to find the maximum value // between N and the obtained number static int maximumOfTwo(int N) { int M = reverseBin(N); return Math.max(N, M); } // Driver Code public static void main(String[] args) { int N = 6; System.out.print(maximumOfTwo(N)); }}// This code is conntributed by Kingash. |
Python3
# Python3 program for tha above approach# Function that obtains the number# using said operations from Ndef reverseBin(N): # Stores the binary representation # of the number N S = "" i = 0 # Find the binary representation # of the number N for i in range(32): # Check for the set bits if (N & (1 << i)): S += '1' else: S += '0' # Reverse the string S S = list(S) S = S[::-1] S = ''.join(S) # Stores the obtained number M = 0 # Calculating the decimal value for i in range(32): # Check for set bits if (S[i] == '1'): M += (1 << i) return M# Function to find the maximum value# between N and the obtained numberdef maximumOfTwo(N): M = reverseBin(N) return max(N, M)# Driver Codeif __name__ == '__main__': N = 6 print(maximumOfTwo(N))# This code is contributed by SURENDRA_GANGWAR |
C#
// C# program for tha above approachusing System;class GFG{static string ReverseString(string s) { char[] array = s.ToCharArray(); Array.Reverse(array); return new string(array); } // Function that obtains the number// using said operations from Npublic static int reverseBin(int N){ // Stores the binary representation // of the number N string S = ""; int i; // Find the binary representation // of the number N for (i = 0; i < 32; i++) { // Check for the set bits if ((N & (1L << i)) != 0) S += '1'; else S += '0'; } // Reverse the string S S = ReverseString(S); // Stores the obtained number int M = 0; // Calculating the decimal value for (i = 0; i < 32; i++) { // Check for set bits if (S[i] == '1') M += (1 << i); } return M;}// Function to find the maximum value// between N and the obtained numberstatic int maximumOfTwo(int N){ int M = reverseBin(N); return Math.Max(N, M);}// Driver Codestatic void Main(){ int N = 6; Console.Write(maximumOfTwo(N));}}// This code is contributed by SoumikMondal |
Javascript
<script>// JavaScript program for tha above approach// Function that obtains the number// using said operations from Nfunction reverseBin(N){ // Stores the binary representation // of the number N let S = ""; let i; // Find the binary representation // of the number N for (i = 0; i < 32; i++) { // Check for the set bits if (N & (1 << i)) S += '1'; else S += '0'; } // Reverse the string S S = S.split("").reverse().join(""); // Stores the obtained number let M = 0; // Calculating the decimal value for (i = 0; i < 32; i++) { // Check for set bits if (S[i] == '1') M += (1 << i); } return M;}// Function to find the maximum value// between N and the obtained numberfunction maximumOfTwo(N){ let M = reverseBin(N); return Math.max(N, M);}// Driver Code let N = 6; document.write(maximumOfTwo(N));</script> |
1610612736
Time Complexity: O(32)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


