# Nearest power of 2 of frequencies of each digit of a given number

The article describes a method to calculate the closest power of 2 for the frequency of each digit in a given number. The term "frequencies" refers to the count of occurrences of each unique digit in the number.

### Problem Statement

Determine the occurrence count of each digit in a positive integer N. Then, for each digit, find the nearest power of 2 to its frequency. If there are two nearest powers of 2 for any frequency, print the larger one.

### Example

Input

n = 677755


Output

5 -> 2
6 -> 1
7 -> 4


Explanation

For n =677755, the frequencies of each digit are:

Digit 5: 2

Digit 6: 1

Digit 7: 3

The nearest powers of 2 to these frequencies are:

Digit 5: 2

Digit 6: 1

Digit 7: 4

Input

n = 9990110466996

Output

1 -> 2
0 -> 2
4 -> 1
6 -> 4
9 -> 8


Explanation

For n = 9990110466996, the frequencies of each digit are:

Digit 6: 3

Digit 9: 5

Digit 4: 1

Digit 0: 2

Digit 1: 2

The nearest powers of 2 to these frequencies are:

Digit 0: 2

Digit 1: 2

Digit 4: 1

Digit 6: 4

Digit 9: 8

## Solution Approach

A viable strategy for this task involves utilizing a map data structure to store the frequency of occurrence of each digit in the given number and later adjust it to the nearest equal or greater power of 2.

### Pseudocode

• Start the program.

• Declare a variable n of type long long intand initialize it.

• Create an empty unordered_map freq to store the frequency of each digit.

• Iterate over the digits of n by performing the following steps until n becomes zero:

• Get the last digit of n by calculating n % 10.

• Increment the frequency of that digit in the freq map by using freq[digit]++.

• Remove the last digit from n by dividing it by 10 (n /= 10).

• Iterate over the key-value pairs in the freq map by performing the following steps:

• For each pair, declare an integer variable power and set it to 1.

• Iterate through the powers of 2 until encountering a power that is greater than or equal to the corresponding frequency value (it.second):

• Multiply power by 2 (power *= 2).

• Print the digit (it.first) and the nearest power of 2 (power).

• End the program.

### Example: C++ Program

In the following C++ program we output the closest power of 2 for the frequencies of each unique digit in the given number. We make use of a frequency map to store the actual number of occurrences of each digit and then employ the while loop to adjust the frequency to the nearest power of 2.

### Example

// The following C++ program returns the closest power of 2 of the frequencies of each digit present in N.

#include <iostream>
#include <unordered_map>
#include <cmath>
using namespace std;

int main() {
long long int n;
n = 9990110466996;
// Declare a map to keep track of the frequency of each digit.
unordered_map<int, int> freq;
// Iterate over the input number and increment the frequency of each digit.
while (n) {
freq[n % 10]++;
n /= 10;
}
// Iterate over the map and print the nearest power of 2 for each frequency.
for (auto it : freq) {
// Declare a variable to store the nearest power of 2.
int power = 1;
// Iterate over the powers of 2 until we find a power that is greater than or equal to the frequency.
while (power < it.second) {
power *= 2;
}
// Print the digit and the nearest power of 2.
cout << it.first << " -> " << power << endl;
}
return 0;
}


### Output

1 -> 2
0 -> 2
4 -> 1
6 -> 4
9 -> 8


## Time and Space Complexity Analysis

### Time Complexity: O(log n)

Iterating over the input number and incrementing the frequency of each digit takes O(log n) time, where n denotes the input number. This is due to the fact that the number of digits in a number n is proportional to log n.

Iterating over the map and printing the nearest power of 2 for each frequency takes O(k) time, where k is the number of unique digits in the input number. In the worst case, k can be considered as a constant.

### Space Complexity: O(k)

The space complexity of creating an unordered map to store the frequency of each digit in a number is proportional to the number of unique digits in the number. In the worst case the space complexity is constant.

## Conclusion

This article presents an approach to determine the closest power of 2 for the frequency of each digit in a given number. The solution approach uses a map data structure to store the frequency of occurrence of each digit in the given number and later adjusts it to the nearest equal or greater power of 2.

The solution approach has a time complexity of O(log n) and a space complexity of O(k), where n is the value of the input number and k is the number of unique digits in the input number. Implementing this approach is relatively straightforward.

Updated on: 27-Aug-2023

25 Views