Find n’th number in a number system with only 3 and 4
Last Updated :
07 Jan, 2024
Given a number system with only 3 and 4. Find the nth number in the number system. First few numbers in the number system are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343, 3344, 3433, 3434, 3443, 3444, …
Source: Zoho Interview
We can generate all numbers with i digits using the numbers with (i-1) digits. The idea is to first add a ‘3’ as prefix in all numbers with (i-1) digit, then add a ‘4’. For example, the numbers with 2 digits are 33, 34, 43 and 44. The numbers with 3 digits are 333, 334, 343, 344, 433, 434, 443 and 444 which can be generated by first adding a 3 as prefix, then 4.
Following are detailed steps.
1) Create an array 'arr[]' of strings size n+1.
2) Initialize arr[0] as empty string. (Number with 0 digits)
3) Do following while array size is smaller than or equal to n
.....a) Generate numbers by adding a 3 as prefix to the numbers generated
in previous iteration. Add these numbers to arr[]
.....a) Generate numbers by adding a 4 as prefix to the numbers generated
in previous iteration. Add these numbers to arr[]
Thanks to kaushik Lele for suggesting this idea in a comment here. Following is the C++ implementation for the same.
C++
#include <iostream>
using namespace std;
void find( int n)
{
string arr[n + 1];
arr[0] = "" ;
int size = 1, m = 1;
while (size <= n) {
for ( int i = 0; i < m && (size + i) <= n; i++)
arr[size + i] = "3" + arr[size - m + i];
for ( int i = 0; i < m && (size + m + i) <= n; i++)
arr[size + m + i] = "4" + arr[size - m + i];
m = m << 1;
size = size + m;
}
cout << arr[n] << endl;
}
int main()
{
for ( int i = 1; i < 16; i++)
find(i);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void find( int n)
{
String[] arr = new String[n + 1 ];
arr[ 0 ] = "" ;
int size = 1 , m = 1 ;
while (size <= n) {
for ( int i = 0 ; i < m && (size + i) <= n; i++)
arr[size + i] = "3" + arr[size - m + i];
for ( int i = 0 ; i < m && (size + m + i) <= n;
i++)
arr[size + m + i] = "4" + arr[size - m + i];
m = m << 1 ;
size = size + m;
}
System.out.println(arr[n]);
}
public static void main(String[] args)
{
for ( int i = 0 ; i < 16 ; i++)
find(i);
}
}
|
Python3
def find(n):
arr = [''] * (n + 1 );
size = 1 ;
m = 1 ;
while (size < = n):
i = 0 ;
while (i < m and (size + i) < = n):
arr[size + i] = "3" + arr[size - m + i];
i + = 1 ;
i = 0 ;
while (i < m and (size + m + i) < = n):
arr[size + m + i] = "4" + arr[size - m + i];
i + = 1 ;
m = m << 1 ;
size = size + m;
print (arr[n]);
for i in range ( 1 , 16 ):
find(i);
|
C#
using System;
class GFG {
static void find( int n)
{
String[] arr = new String[n + 1];
arr[0] = "" ;
int size = 1, m = 1;
while (size <= n)
{
for ( int i = 0; i < m &&
(size + i) <= n; i++)
arr[size + i] = "3" +
arr[size - m + i];
for ( int i = 0; i < m &&
(size + m + i) <= n; i++)
arr[size + m + i] = "4" +
arr[size - m + i];
m = m << 1;
size = size + m;
}
Console.WriteLine(arr[n]);
}
public static void Main ()
{
for ( int i = 0; i < 16; i++)
find(i);
}
}
|
Javascript
<script>
function find(n)
{
var arr = Array.from({length: n + 1}, (_, i) => " " );
arr[0] = "" ;
var size = 1, m = 1;
while (size <= n)
{
for ( var i = 0; i < m && (size + i) <= n; i++)
arr[size + i] = "3" + arr[size - m + i];
for ( var i = 0; i < m && (size + m + i) <= n;
i++)
arr[size + m + i] = "4" + arr[size - m + i];
m = m << 1;
size = size + m;
}
document.write(arr[n]+ "<br>" );
}
for (i = 0; i < 16; i++)
find(i);
</script>
|
PHP
<?php
function find( $n )
{
$arr = array_fill (0, $n + 1, "" );
$size = 1;
$m = 1;
while ( $size <= $n )
{
for ( $i = 0; $i < $m &&
( $size + $i ) <= $n ; $i ++)
$arr [ $size + $i ] = "3" .
$arr [ $size - $m + $i ];
for ( $i = 0; $i < $m &&
( $size + $m + $i ) <= $n ; $i ++)
$arr [ $size + $m + $i ] = "4" .
$arr [ $size - $m + $i ];
$m = $m << 1;
$size = $size + $m ;
}
echo $arr [ $n ] . "\n" ;
}
for ( $i = 1; $i < 16; $i ++)
find( $i );
?>
|
Output:
3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
Time Complexity: O(Nlog(N)) as the program uses a while loop to generate and add 2*m numbers to the array in each iteration, where m is the number of elements added to the array in the previous iteration.
Auxiliary Space: O(N) because the program creates an array of size n+1 to store the first n numbers in the number system with only 3 and 4
Better Approach (using bits) :
This idea was suggested by Arjun J (https://auth.geeksforgeeks.org/user/camsboyfriend/profile).
The idea here is, as we are going to deal with only two numbers, i.e., 3 and 4, so we can just compare them with binary numbers.
Explanation :
1) 3 - 0 (0)
2) 4 - 1 (1)
3) 33 - 00 (0)
4) 34 - 01 (1)
5) 43 - 10 (2)
6) 44 - 11 (3)
7) 333 - 000 (0)
8) 334 - 001 (1)
9) 343 - 010 (2)
10) 344 - 011 (3)
11) 433 - 100 (4)
12) 434 - 101 (5)
13) 443 - 110 (6)
14) 444 - 111 (7)
15) 3333 - 1000 (8)
Here we can note that
- Every (n – 1)’th number gets a new digit where n is a power of 2
- Whenever a new digit is added we start the counting binary numbers from 0.
- 0 in binary form corresponds to 3 in our number system and similarly 1 corresponds to 4.
Below is the C++ implementation for the same :
C++
#include <bits/stdc++.h>
using namespace std;
int highestPowerof2(unsigned int n)
{
if (n < 1)
return 0;
int res = 1;
for ( int i = 0; i < 8 * sizeof (unsigned int ); i++) {
int curr = 1 << i;
if (curr > n)
break ;
res = curr;
}
return res;
}
vector< int > decToBinary( int n, int size)
{
vector< int > binaryNum(size + 1);
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = (n >> 1);
i++;
}
return binaryNum;
}
signed main()
{
for ( int n = 1; n < 16; n++) {
int hp2 = highestPowerof2(n + 1);
int howMany = n - hp2 + 1;
vector< int > arr
= decToBinary(howMany, log2(hp2 - 1));
for ( int i = log2(hp2 - 1); i >= 0; i--) {
if (arr[i])
cout << 4;
else
cout << 3;
}
cout << '\n' ;
}
}
|
Java
import java.io.*;
class GFG
{
static long highestPowerof2( int n)
{
if (n < 1 )
return 0 ;
long res = 1 ;
for ( int i = 0 ; i < 32 ; i++) {
long curr = 1 << i;
if (curr > n)
break ;
res = curr;
}
return res;
}
static long [] decToBinary( long n, int size)
{
long [] binaryNum = new long [size+ 1 ];
int i = 0 ;
while (n > 0 ) {
binaryNum[i] = n % 2 ;
n = (n >> 1 );
i = i + 1 ;
}
return binaryNum;
}
public static void main(String args[])
{
for ( int n = 1 ; n < 16 ; n++) {
long hp2 = highestPowerof2(n + 1 );
long howMany = n - hp2 + 1 ;
long [] arr = decToBinary(howMany, ( int )Math.floor(Math.log(hp2 - 1 )/Math.log( 2 )));
for ( int i = ( int )Math.floor(Math.log(hp2 - 1 )/Math.log( 2 )); i >= 0 ; i--) {
if (arr[i] > 0 )
System.out.print( 4 );
else
System.out.print( 3 );
}
System.out.println();
}
}
}
|
Python3
from math import floor, log2
def highestPowerof2(n):
if (n < 1 ):
return 0
res = 1
for i in range ( 32 ):
curr = 1 << i
if (curr > n):
break
res = curr
return res
def decToBinary(n, size):
binaryNum = [ 0 for i in range (size + 1 )]
i = 0
while (n > 0 ):
binaryNum[i] = n % 2
n = (n >> 1 )
i = i + 1
return binaryNum
for n in range ( 1 , 16 ):
hp2 = highestPowerof2(n + 1 )
howMany = n - hp2 + 1
arr = decToBinary(howMany, floor(log2(hp2 - 1 )))
for i in range (floor(log2(hp2 - 1 )), - 1 , - 1 ):
if (arr[i]):
print ( 4 ,end = "")
else :
print ( 3 ,end = "")
print ()
|
C#
using System;
class GFG
{
static long highestPowerof2( int n)
{
if (n < 1)
return 0;
long res = 1;
for ( int i = 0; i < 32; i++) {
long curr = 1 << i;
if (curr > n)
break ;
res = curr;
}
return res;
}
static long [] decToBinary( long n, int size)
{
long [] binaryNum = new long [size+1];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = (n >> 1);
i = i + 1;
}
return binaryNum;
}
public static void Main( string [] args)
{
for ( int n = 1; n < 16; n++) {
long hp2 = highestPowerof2(n + 1);
long howMany = n - hp2 + 1;
long [] arr = decToBinary(howMany, ( int )Math.Floor(Math.Log(hp2 - 1)/Math.Log(2)));
for ( int i = ( int )Math.Floor(Math.Log(hp2 - 1)/Math.Log(2)); i >= 0; i--) {
if (arr[i] > 0)
Console.Write(4);
else
Console.Write(3);
}
Console.WriteLine();
}
}
}
|
Javascript
function highestPowerof2(n)
{
if (n < 1)
return 0;
let res = 1;
for (let i = 0; i < 32; i++) {
let curr = 1 << i;
if (curr > n)
break ;
res = curr;
}
return res;
}
function decToBinary(n, size)
{
let binaryNum = new Array(size+1);
let i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = (n >> 1);
i = i + 1;
}
return binaryNum;
}
for (let n = 1; n < 16; n++) {
let hp2 = highestPowerof2(n + 1);
let howMany = n - hp2 + 1;
let arr = decToBinary(howMany, Math.floor(Math.log2(hp2 - 1)));
for (let i = Math.floor(Math.log2(hp2 - 1)); i >= 0; i--) {
if (arr[i])
document.write(4);
else
document.write(3);
}
document.write( "\n" );
}
|
Output:
3
4
33
34
43
44
333
334
343
344
433
434
443
444
3333
Time Complexity: O(log(n))
Auxiliary Space: O(Size), where size represents the size of the decimal number in its binary representation.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...