There is a series of numbers that have only digits, 4 and 7, and numbers are arranged in increasing order. The first few numbers of the series are 4, 7, 44, 47, 74, 77, 444, …etc. Given a number N, the task is to find the position of that number in the given series.
Input: N = 4
The first number in the series is 4
Input: N = 777
The 14th number in the series is 777
Approach: This problem can be solved using the below observations:
- Below is the pattern observe in the given series. The numbers can be seen as:
"" / \ 4 7 / \ / \ 44 47 74 77 / \ / \ / \ / \
- As we can observe the pattern is increasing in power of 2. Therefore the idea is to iterate over the digits of the number starting from the least significant digit and update the position of the number as:
- If current digit = 7, then add to the position.
- If current digit = 4, then add to the position.
- Print the final position after the above operations.
Below is the implementation of the above approach:
Time Complexity: O(log10N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find n-th element in a series with only 2 digits (4 and 7) allowed
- Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Sum of the digits of square of the given number which has only 1's as its digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Program to find Nth term in the given Series
- Find the nth term of the given series
- Maximum number formed from array with K number of adjacent swaps allowed
- Find smallest number with given number of digits and sum of digits under given constraints
- Find position of the only set bit
- Count sequences of length K having each term divisible by its preceding term
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.