Find the nth term of the given series
Given the first two terms of the series as 1 and 6 and all the elements of the series are 2 less than the mean of the number preceding and succeeding it. The task is to print the nth term of the series.
First few terms of the series are:
1, 6, 15, 28, 45, 66, 91, …
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: N = 3
Input: N = 1
Approach: The given series represents odd positioned numbers in the triangular number series. Since the nth triangular number can easily be found by (n * (n + 1) / 2), so for finding the odd numbers we can replace n by (2 * n) – 1 as (2 * n) – 1 will always result in odd numbers i.e. the nth number of the given series will be ((2 * n) – 1) * n.
Below is the implementation of the above approach: