Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find N-th term of series a, b, b, c, c, c…in C++
In this problem, we are given a number N. Our task is to create a Program to find N-th term of series a, b, b, c, c, c…in C++.
Problem Description
To find the Nth term of the series −
a, b, b, c, c, c, d, d, d, d,....Nterms
We need to find the general term of the series.
Let’s take an example to understand the problem,
Input
N = 7
Output
d
Solution Approach
To find the general term of the series, we need to closely observe the series. The series has 1 a, 2 b’s, 3 c’s, 4 d’s,... This seems to be an AP. And the Nth term is the sum of AP which a and d both 1.
Sum of AP = Nth Term = (n/2)(a+(n-1)d).
The n specifies which character is the Nth term.
Now, lets derive the value of n,
Nth Term = (n/2)*(1 + (n-1)*1) (n/2)*(1 + n - 1) (n/2)*n
$\sqrt{2\square^2}$
Example
#include <iostream>
#include <math.h>
using namespace std;
char findNTerm(int N) {
int n = sqrt(2*N);
return ((char)('a' + n));
}
int main() {
int N = 54;
cout<<N<<"th term of the series is "<<findNTerm(N);
return 0;
}
Output
54th term of the series is k
Advertisements