Generate an N-digit number made up of 1 or 2 only which is divisible by 2N

• Last Updated : 03 Mar, 2021

Given an integer N, the task is to generate an N-digit number which is comprising only of digits 1 or 2 and is divisible by 2N.

Examples:

Input: N = 4
Output: 2112
Explanation: Since 2112 is divisible by 24 ( = 16).

Input: N = 15
Output: 211111212122112

Approach: Follow the steps below to solve the problem:

• Iterate over all values in the range [1, 2N].
• For each integer i in that range, generate its binary representation using bitset and store it in a string, say S.
• Reduce the length of the string S to N.
• Traverse the bits of S and if Si == ‘0’, set Si = ‘2’.
• Convert the obtained string to an integer, say res using stoll().
• If res is divisible by 2N, print the integer and break.

Below is the implementation of the above approach:

C++14

 // C++ program for the above approach#include using namespace std; // Utility function to find x^y in O(log(y))long long int power(long long int x,                    unsigned long long int y){    // Stores the result    long long int res = 1;     // Update x if it is >= p    while (y > 0) {        // If y is odd        if (y & 1)             // Multiply x with res            res = (res * x);         // y must be even now        // Set y = y/2        y = y >> 1;        x = (x * x);    }    return res;} // Function to generate the N digit number// satisfying the given conditionsvoid printNth(int N){    // Find all possible integers upto 2^N    for (long long int i = 1;         i <= power(2, N); i++) {         // Generate binary representation of i        string s = bitset<64>(i).to_string();         // Reduce the length of the string to N        string s1 = s.substr(            s.length() - N, s.length());         for (long long int j = 0; s1[j]; j++) {             // If current bit is '0'            if (s1[j] == '0') {                s1[j] = '2';            }        }         // Convert string to equivalent integer        long long int res = stoll(s1, nullptr, 10);         // If condition satisfies        if (res % power(2, N) == 0) {             // Print and break            cout << res << endl;            break;        }    }} // Driver Codeint main(){    int N = 15;    printNth(N);}

Python3

 # Python program for the above approach # Utility function to find x^y in O(log(y))def power(x, y):         # Stores the result    res = 1         # Update x if it is >= p    while (y > 0):               # If y is odd        if (y & 1):                         # Multiply x with res            res = (res * x)                     # y must be even now        # Set y = y/2        y = y >> 1        x = (x * x)    return res # Function to generate the N digit number# satisfying the given conditionsdef printNth(N):         # Find all possible integers upto 2^N    for i in range(1,power(2, N) + 1):                 # Generate binary representation of i        s = "{:064b}".format(i)                 # Reduce the length of the string to N        s1 = s[len(s)- N: 2*len(s)-N]                 j = 0        while(j < len(s1)):                       # If current bit is '0'            if (s1[j] == '0'):                s1 = s1[:j] + '2' + s1[j + 1:]            j += 1                 # Convert string to equivalent integer        res = int(s1)                 # If condition satisfies        if (res % power(2, N) == 0):                         # Prand break            print(res)            break # Driver CodeN = 15printNth(N) # This code is contributed by shubhamsingh10
Output:
211111212122112

Time Complexity: O(2N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up