Consider a series of numbers composed of only digits 4 and 7. First few numbers in the series are 4, 7, 44, 47, 74, 44744,.. etc. Given a number n, we need to find n-th number in the series.
Input : n = 2 Output : 7 Input : n = 3 Output : 44 Input : n = 5 Output : 74 Input : n = 6 Output : 77
The idea is based on the fact that the value of last digit alternates in series. For example, if last digit of i-th number is 4, then last digit of (i-1)-th and (i+1)-th numbers must be 7.
We create an array of size (n+1) and push 4 and 7 (These two are always first two elements of series) to it. For more elements we check
1) If i is odd,
arr[i] = arr[i/2]*10 + 4;
2) If it is even,
arr[i] = arr[(i/2)-1]*10 + 7;
At last return arr[n].
This article is contributed by Roshni Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Nth number made up of odd digits only
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Number of digits in the nth number made of given four digits
- Find the Nth element of the modified Fibonacci series
- Sum of the digits of square of the given number which has only 1's as its digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Find Nth even length palindromic number formed using digits X and Y
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Program to find Nth term in the given Series
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,...
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
Improved By : jit_t