Find N-th term in the series 0, 4, 18, 48, 100 …
Given a series 0, 4, 18, 48, 100 . . . and an integer N, the task is to find the N-th term of the series.
Input: N = 4
Explanation: As given in the sequence we can see that 4th term is 48
Input: N = 6
Approach: Consider the below observation:
For N = 2, the 2nd term is 4, which can be represented as 8 – 4, i.e. 23 – 22
For N = 3, the 3rd term is 18, which can be represented as 27 – 9, i.e. 33 – 32
For N = 4, the 4th term is 18, which can be represented as 27 – 9, i.e. 43 – 42
Similarly, The N-th term of this series can be represented as N3 – N2
So for any N, find the square of that N and subtract it from the cube of the number.
Below is the implementation of the above approach:
Time complexity: O(1)
Auxiliary space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.