Generate an N-digit number made up of 1 or 2 only which is divisible by 2N
Last Updated :
28 Jul, 2022
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
#include <bits/stdc++.h>
using namespace std;
long long int power( long long int x,
unsigned long long int y)
{
long long int res = 1;
while (y > 0) {
if (y & 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
void printNth( int N)
{
for ( long long int i = 1;
i <= power(2, N); i++) {
string s = bitset<64>(i).to_string();
string s1 = s.substr(
s.length() - N, s.length());
for ( long long int j = 0; s1[j]; j++) {
if (s1[j] == '0' ) {
s1[j] = '2' ;
}
}
long long int res = stoll(s1, nullptr, 10);
if (res % power(2, N) == 0) {
cout << res << endl;
break ;
}
}
}
int main()
{
int N = 15;
printNth(N);
}
|
Java
import java.lang.*;
import java.util.*;
class GFG
{
static long power( long x, long y)
{
long res = 1 ;
while (y > 0 ) {
if ((y & 1 ) == 1 )
res = (res * x);
y = y >> 1 ;
x = (x * x);
}
return res;
}
static void printNth( int N)
{
for ( long i = 1 ; i <= power( 2 , N); i++) {
String s = Long.toBinaryString(i);
s = String.format( "%" + 64 + "s" , s)
.replace( ' ' , '0' );
char [] s1
= s.substring(s.length() - N, s.length())
.toCharArray();
for ( int j = 0 ; j < s1.length; j++) {
if (s1[j] == '0' ) {
s1[j] = '2' ;
}
}
long res = Long.parseLong( new String(s1));
if (res % power( 2 , N) == 0 ) {
System.out.println(res);
break ;
}
}
}
public static void main(String[] args)
{
int N = 15 ;
printNth(N);
}
}
|
Python3
def power(x, y):
res = 1
while (y > 0 ):
if (y & 1 ):
res = (res * x)
y = y >> 1
x = (x * x)
return res
def printNth(N):
for i in range ( 1 ,power( 2 , N) + 1 ):
s = "{:064b}" . format (i)
s1 = s[ len (s) - N: 2 * len (s) - N]
j = 0
while (j < len (s1)):
if (s1[j] = = '0' ):
s1 = s1[:j] + '2' + s1[j + 1 :]
j + = 1
res = int (s1)
if (res % power( 2 , N) = = 0 ):
print (res)
break
N = 15
printNth(N)
|
C#
using System;
class GFG
{
static long power( long x, long y)
{
long res = 1;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void printNth( int N)
{
for ( long i = 1;
i <= power(2, N); i++) {
string s = Convert.ToString(i, 2);
s = s.PadLeft(64, '0' );
char [] s1 = s.Substring(
s.Length - N).ToCharArray();
for ( int j = 0; j < s1.Length; j++) {
if (s1[j] == '0' ) {
s1[j] = '2' ;
}
}
long res = Convert.ToInt64( new string (s1), 10);
if (res % power(2, N) == 0) {
Console.WriteLine(res);
break ;
}
}
}
public static void Main( string [] args)
{
int N = 15;
printNth(N);
}
}
|
Javascript
function power(x, y)
{
let res = 1;
while (y > 0) {
if (y & 1 != 0)
res = (res * x);
y = y >> 1;
x *= x;
}
return res;
}
function printNth(N)
{
for ( var i = 1;
i <= power(2, N); i++) {
let s = i.toString(2).padStart(64, '0' );
let s1 = s.substring(s.length - N, 2 * s.length - N);
s1 = s1.split( "" );
for ( var j = 0; s1[j]; j++) {
if (s1[j] == '0' ) {
s1[j] = '2' ;
}
}
let res = parseInt(s1.join( "" ), 10);
if (res % power(2, N) == 0) {
console.log(res);
break ;
}
}
}
let N = 15;
printNth(N);
|
Time Complexity: O(2N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...