- Related Questions & Answers
- Program to find N-th term of series 0, 2,1, 3, 1, 5, 2, 7, 3...in C++
- C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- C++ program to find n-th term in the series 7, 15, 32, …
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Find n-th element from Stern’s Diatomic Series in C++
- C++ Programe to find n-th term in series 1 2 2 3 3 3 4
- Minimum number with digits as and 7 only and given sum in C++
- Find n’th number in a number system with only 3 and 4 in C++
- Program to find N-th term of series 1, 2, 11, 12, 21… in C++
- Program to find N-th term of series 2, 4, 3, 4, 15… in C++
- C++ program to find n-th term of series 2, 10, 30, 68, 130 …
- Program to find N-th term of series 0, 7, 8, 33, 51, 75, 102, 133...in C++
- Program to find N-th term of series 7, 21, 49, 91, 147, 217, …… in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given an integer N, denoting a series of numbers consisting of 4 and 7 only.

The series is 4, 7, 44, 47, 74, 77, …

The task is to find the n-th element in a series with only 2 digits (and 7) allowed.

**Let’s take an example to understand the problem,**

N = 4,

47

The series is: 4, 7, 44, 47, ….

A simple solution to the problem is creating the series till Nth number. It’s simple, if the current number’s last digit is 7. Then the last digit of previous and next numbers is 4.

So, we will start from 1st and 2nd numbers and then progress to the next element.

For this, we will create an array series[n+1].

For index series[1] put 4 For index series[2] put 7

Then for successive values till N, find the values for the given index i,

If i is odd, series[i] = series[i/2]*10 + 4 If i is even, series[i] = series[i/2]*10 + 7

After n iterations, return value at series[n].

**Program to illustrate the working of our solution,**

#include <iostream> using namespace std; int findNthSeriesElement(int N) { int series[N+1]; series[1] = 4; series[2] = 7; for (int i=3; i<=N; i++) { if (i%2 != 0) series[i] = series[i/2]*10 + 4; else series[i] = series[(i/2)-1]*10 + 7; } return series[N]; } int main() { int N = 9; cout<<"The "<<N<<"th element of the array is "<<findNthSeriesElement(N); return 0; }

The 9th element of the array is 474

Advertisements