Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
Last Updated :
08 Jun, 2022
Consider a series of numbers composed of only digits 4 and 7. First few numbers in the series are 4, 7, 44, 47, 74, 77, 444, .. etc. Given a number n, we need to find n-th number in the series.
Examples:
Input : n = 2
Output : 7
Input : n = 3
Output : 44
Input : n = 5
Output : 74
Input : n = 6
Output : 77
We have discussed a O(n) solution in below post.
Find n-th element in a series with only 2 digits (4 and 7) allowed
In this post, a O(log n) solution is discussed which is based on below pattern in numbers. The numbers can be seen
""
/ \
4 7
/ \ / \
44 47 74 77
/ \ / \ / \ / \
The idea is to fill the required number from end. We know can observe that the last digit is 4 if n is odd and last digit is 7 if n is even. After filling last digit, we move to parent node in tree. If n is odd, then parent node corresponds to (n-1/2. Else parent node corresponds to (n-2)/2.
C++
#include<bits/stdc++.h>
using namespace std;
string findNthNo( int n)
{
string res = "" ;
while (n >= 1)
{
if (n & 1)
{
res = res + "4" ;
n = (n-1)/2;
}
else
{
res = res + "7" ;
n = (n-2)/2;
}
}
reverse(res.begin(), res.end());
return res;
}
int main()
{
int n = 13;
cout << findNthNo(n);
return 0;
}
|
Java
public class GFG {
static String findNthNo( int n)
{
String res = "" ;
while (n >= 1 )
{
if ((n & 1 ) == 1 )
{
res = res + "4" ;
n = (n - 1 ) / 2 ;
}
else
{
res = res + "7" ;
n = (n - 2 ) / 2 ;
}
}
StringBuilder sb =
new StringBuilder(res);
sb.reverse();
return new String(sb);
}
public static void main(String args[])
{
int n = 13 ;
System.out.print( findNthNo(n) );
}
}
|
Python3
def reverse(s):
if len (s) = = 0 :
return s
else :
return reverse(s[ 1 :]) + s[ 0 ]
def findNthNo(n):
res = "";
while (n > = 1 ):
if (n & 1 ):
res = res + "4" ;
n = ( int )((n - 1 ) / 2 );
else :
res = res + "7" ;
n = ( int )((n - 2 ) / 2 );
return reverse(res);
n = 13 ;
print (findNthNo(n));
|
C#
using System;
class GFG {
static string findNthNo( int n)
{
string res = "" ;
while (n >= 1)
{
if ((n & 1) == 1)
{
res = res + "4" ;
n = (n - 1) / 2;
}
else
{
res = res + "7" ;
n = (n - 2) / 2;
}
}
char [] arr = res.ToCharArray();
Array.Reverse(arr);
return new string (arr);
}
public static void Main()
{
int n = 13;
Console.Write( findNthNo(n) );
}
}
|
PHP
<?php
function findNthNo( $n )
{
$res = "" ;
while ( $n >= 1)
{
if ( $n & 1)
{
$res = $res . "4" ;
$n = (int)(( $n - 1) / 2);
}
else
{
$res = $res . "7" ;
$n = (int)(( $n - 2) / 2);
}
}
return strrev ( $res );
}
$n = 13;
echo findNthNo( $n );
?>
|
Javascript
<script>
function findNthNo(n)
{
res = "" ;
while (n >= 1)
{
if ((n & 1) == 1)
{
res = res + "4" ;
n = (n - 1) / 2;
}
else
{
res = res + "7" ;
n = parseInt((n - 2) / 2);
}
}
return res.split( "" ).reverse().join( "" );
}
var n = 13;
document.write( findNthNo(n) );
</script>
|
Output:
774
Time Complexity: O(logN), where N represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
In this code the total complexity is O(log n). Because while loop run log (n) times.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...