Given a number N, the task is to convert it into a number in which all distinct digits have the same frequency, by rotating its bits either clockwise or anti-clockwise. If it is not possible to convert the number print -1, otherwise print the number
Examples:
Input: N = 331
Output: 421
Explanation:
Binary representation of 331: 101001011
After an anti-clockwise rotation – 421: 110100101
421 is a steady number
Input: N = 58993
Output: 51097
Approach: The idea is to check all the possible scenarios, after rotating the bits of the number. Follow the below steps to solve this problem:
- Use a map to keep track of the frequencies of the digits.
- Use a set to check if all the frequencies are the same or not.
- If the number has the same frequencies for all digits, then print it as the answer
- Else rotate the binary representation of the number and check again.
- If after all the rotations, no number can be found having the same frequencies of all digits, then print -1.
Below is the representation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int rotate( int N, int bits)
{
int ls = N & 1;
return (N >> 1) | ( int ( pow (2, (bits - 1))) * ls);
}
bool checkStead( int N)
{
map< char , int > mp;
for ( auto i : to_string(N))
mp[i]++;
set< int > se;
for ( auto it = mp.begin(); it != mp.end(); ++it)
se.insert(it->second);
if (se.size() == 1)
return true ;
return false ;
}
void solve( int N)
{
int bits = int (log2(N)) + 1;
bool flag = true ;
for ( int i = 1; i < bits; ++i) {
if (checkStead(N)) {
cout << N << "\n" ;
flag = false ;
break ;
}
N = rotate(N, bits);
}
if (flag)
cout << -1;
}
int main()
{
int N = 331;
solve(N);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.HashSet;
class GFG
{
public static int rotate( int N, int bits) {
int ls = N & 1 ;
return (N >> 1 ) | ( int ) (Math.pow( 2 , (bits - 1 ))) * ls;
}
public static boolean checkStead( int N)
{
HashMap<String, Integer> mp = new HashMap<String, Integer>();
for (String i : Integer.toString(N).split( "" )){
if (mp.containsKey(i)){
mp.put(i, mp.get(i) + 1 );
} else {
mp.put(i, 1 );
}
}
HashSet<Integer> se = new HashSet<Integer>();
for (String it : mp.keySet())
se.add(mp.get(it));
if (se.size() == 1 )
return true ;
return false ;
}
public static void solve( int N)
{
int bits = ( int )(Math.log(N) / Math.log( 2 )) + 1 ;
boolean flag = true ;
for ( int i = 1 ; i < bits; ++i) {
if (checkStead(N)) {
System.out.println(N);
flag = false ;
break ;
}
N = rotate(N, bits);
}
if (flag)
System.out.println(- 1 );
}
public static void main(String args[]) {
int N = 331 ;
solve(N);
}
}
|
Python3
import math
def solve(N):
bits = int (math.log(N, 2 )) + 1
flag = True
for i in range ( 1 , bits):
if checkStead(N):
print (N)
flag = False
break
N = rotate(N, bits)
if flag:
print ( - 1 )
def rotate(N, bits):
ls = N & 1
return (N >> 1 ) | ( 2 * * (bits - 1 ) * ls)
def checkStead(N):
mp = {}
for i in str (N):
if i in mp:
mp[i] + = 1
else :
mp[i] = 1
if len ( set (mp.values())) = = 1 :
return True
return False
N = 331
solve(N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int rotate( int N, int bits)
{
int ls = N & 1;
return (N >> 1)
| ( int )(Math.Pow(2, (bits - 1))) * ls;
}
public static bool checkStead( int N)
{
Dictionary< char , int > mp
= new Dictionary< char , int >();
foreach ( char i in N.ToString())
{
if (mp.ContainsKey(i)) {
mp[i]++;
}
else {
mp[i] = 1;
}
}
HashSet< int > se = new HashSet< int >();
foreach (KeyValuePair< char , int > it in mp)
se.Add(it.Value);
if (se.Count == 1)
return true ;
return false ;
}
public static void solve( int N)
{
int bits = ( int )(Math.Log(N) / Math.Log(2)) + 1;
bool flag = true ;
for ( int i = 1; i < bits; ++i) {
if (checkStead(N)) {
Console.WriteLine(N);
flag = false ;
break ;
}
N = rotate(N, bits);
}
if (flag)
Console.WriteLine(-1);
}
public static void Main()
{
int N = 331;
solve(N);
}
}
|
Javascript
<script>
function checkStead(N)
{
let mp = new Map();
let s = [];
while (N != 0)
{
s.push(N % 10);
N = Math.floor(N / 10)
}
for (let i = 0; i < s.length; i++)
{
if (mp.has(s[i]))
{
mp.set(s[i], mp.get(s[i]) + 1)
}
else
{
mp.set(s[i], 1);
}
}
let st = new Set([...mp.values()]);
if (st.size == 1)
return true ;
else
return false
}
function rotate(N, bits)
{
let ls = N & 1;
return ((N >> 1) | (Math.pow(2, bits - 1) * ls))
}
function solve(N)
{
let bits = Math.floor(Math.log2(N)) + 1
let flag = true
for (let i = 1; i < bits; i++)
{
if (checkStead(N) == 1)
{
document.write(N)
flag = false
break
}
N = rotate(N, bits)
}
if (flag)
document.write(-1)
}
N = 331
solve(N)
</script>
|
Time Complexity: O(log2N)
Auxiliary Space: O(log10N)
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 :
24 Dec, 2021
Like Article
Save Article