使用每个数字具有相同频率的 N 的位旋转形成一个数字
给定一个数字N ,任务是通过顺时针或逆时针旋转其位,将其转换为所有不同数字具有相同频率的数字。如果不能转换数字打印-1,否则打印数字
例子:
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
方法:这个想法是在旋转数字的位之后检查所有可能的情况。请按照以下步骤解决此问题:
- 使用地图来跟踪数字的频率。
- 使用一组来检查所有频率是否相同。
- 如果数字对所有数字具有相同的频率,则将其打印为答案
- 否则旋转数字的二进制表示并再次检查。
- 如果在所有旋转之后,找不到所有数字的频率相同的数字,则打印 -1。
下面是上述方法的表示。
C++
// C++ code for the above approach
#include
using namespace std;
// Function to rotate
// the number by one place
int rotate(int N, int bits)
{
// Getting the least significant bit
int ls = N & 1;
// Rotating the number
return (N >> 1) | (int(pow(2, (bits - 1))) * ls);
}
// Function to check
// if a number is steady or not
bool checkStead(int N)
{
// Map for the frequency of the digits
map mp;
for (auto i : to_string(N))
mp[i]++;
// Checking if all the
// frequencies are same or not
set 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)
{
// Getting the number of bits needed
int bits = int(log2(N)) + 1;
bool flag = true;
// Checking all the possible numbers
for (int i = 1; i < bits; ++i) {
if (checkStead(N)) {
cout << N << "\n";
flag = false;
break;
}
N = rotate(N, bits);
}
if (flag)
cout << -1;
}
// Driver Code
int main()
{
int N = 331;
solve(N);
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java code for the above approach
import java.util.HashMap;
import java.util.HashSet;
class GFG
{
// Function to rotate
// the number by one place
public static int rotate(int N, int bits) {
// Getting the least significant bit
int ls = N & 1;
// Rotating the number
return (N >> 1) | (int) (Math.pow(2, (bits - 1))) * ls;
}
// Function to check
// if a number is steady or not
public static boolean checkStead(int N)
{
// Map for the frequency of the digits
HashMap mp = new HashMap();
for (String i : Integer.toString(N).split("")){
if(mp.containsKey(i)){
mp.put(i, mp.get(i) + 1);
}else{
mp.put(i, 1);
}
}
// Checking if all the
// frequencies are same or not
HashSet se = new HashSet();
for (String it : mp.keySet())
se.add(mp.get(it));
if (se.size() == 1)
return true;
return false;
}
public static void solve(int N)
{
// Getting the number of bits needed
int bits = (int)(Math.log(N) / Math.log(2)) + 1;
boolean flag = true;
// Checking all the possible numbers
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);
}
// Driver Code
public static void main(String args[]) {
int N = 331;
solve(N);
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# Python code for the above approach
import math
def solve(N):
# Getting the number of bits needed
bits = int(math.log(N, 2))+1
flag = True
# Checking all the possible numbers
for i in range(1, bits):
if checkStead(N):
print(N)
flag = False
break
N = rotate(N, bits)
if flag:
print(-1)
# Function to rotate
# the number by one place
def rotate(N, bits):
# Getting the least significant bit
ls = N & 1
# Rotating the number
return (N >> 1) | (2**(bits-1)*ls)
# Function to check
# if a number is steady or not
def checkStead(N):
# Map for the frequency of the digits
mp = {}
for i in str(N):
if i in mp:
mp[i] += 1
else:
mp[i] = 1
# Checking if all the
# frequencies are same or not
if len(set(mp.values())) == 1:
return True
return False
# Driver Code
N = 331
solve(N)
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to rotate
// the number by one place
public static int rotate(int N, int bits)
{
// Getting the least significant bit
int ls = N & 1;
// Rotating the number
return (N >> 1)
| (int)(Math.Pow(2, (bits - 1))) * ls;
}
// Function to check
// if a number is steady or not
public static bool checkStead(int N)
{
// Map for the frequency of the digits
Dictionary mp
= new Dictionary();
foreach(char i in N.ToString())
{
if (mp.ContainsKey(i)) {
mp[i]++;
}
else {
mp[i] = 1;
}
}
// Checking if all the
// frequencies are same or not
HashSet se = new HashSet();
foreach(KeyValuePair it in mp)
se.Add(it.Value);
if (se.Count == 1)
return true;
return false;
}
public static void solve(int N)
{
// Getting the number of bits needed
int bits = (int)(Math.Log(N) / Math.Log(2)) + 1;
bool flag = true;
// Checking all the possible numbers
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);
}
// Driver Code
public static void Main()
{
int N = 331;
solve(N);
}
}
// This code is contributed by ukasp.
Javascript
输出:
421
时间复杂度: O(log 2 N)
辅助空间: O(log 10 N)