Given three integers X, Y and N, the task is to find the Nth term of the series f[i] = f[i – 1] – f[i – 2], i > 1 where f[0] = X and f[1] = Y.
Examples:
Input: X = 2, Y = 3, N = 3
Output: -2
The series will be 2 3 1 -2 -3 -1 2 and f[3] = -2Input: X = 3, Y = 7, N = 8
Output: 4
Approach: An important observation here is that there will be atmost 6 distinct terms, before the sequence starts repeating itself. So, find the first 6 terms of the series and then the Nth term would be same as the (N % 6)th term.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the Nth term // of the given series int findNthTerm(int x, int y, int n) { int f[6]; // First and second term of the series f[0] = x; f[1] = y; // Find first 6 terms for (int i = 2; i <= 5; i++) f[i] = f[i - 1] - f[i - 2]; // Return the Nth term return f[n % 6]; } // Driver code int main() { int x = 2, y = 3, n = 3; cout << findNthTerm(x, y, n); return 0; } |
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to find the nth term of series static int findNthTerm(int x, int y, int n) { int[] f = new int[6]; f[0] = x; f[1] = y; // Loop to add numbers for (int i = 2; i <= 5; i++) f[i] = f[i - 1] - f[i - 2]; return f[n % 6]; } // Driver code public static void main(String args[]) { int x = 2, y = 3, n = 3; System.out.println(findNthTerm(x, y, n)); } } // This code is contributed by mohit kumar 29 |
Python3
# Python3 implementation of the approach # Function to return the Nth term # of the given series def findNthTerm(x, y, n): f = [0] * 6 # First and second term of # the series f[0] = x f[1] = y # Find first 6 terms for i in range(2, 6): f[i] = f[i - 1] - f[i - 2] # Return the Nth term return f[n % 6] # Driver code if __name__ == "__main__": x, y, n = 2, 3, 3 print(findNthTerm(x, y, n)) # This code is contributed by # Rituraj Jain |
C#
// C# implementation of the approach using System; class GFG { // Function to find the nth term of series static int findNthTerm(int x, int y, int n) { int[] f = new int[6]; f[0] = x; f[1] = y; // Loop to add numbers for (int i = 2; i <= 5; i++) f[i] = f[i - 1] - f[i - 2]; return f[n % 6]; } // Driver code public static void Main() { int x = 2, y = 3, n = 3; Console.WriteLine(findNthTerm(x, y, n)); } } // This code is contributed by Ryuga |
PHP
<?php //PHP implementation of the approach // Function to find the nth term of series function findNthTerm($x, $y, $n) { $f = array(6); $f[0] = $x; $f[1] = $y; // Loop to add numbers for ($i = 2; $i <= 5; $i++) $f[$i] = $f[$i - 1] - $f[$i - 2]; return $f[$n % 6]; } // Driver code $x = 2; $y = 3; $n = 3; echo(findNthTerm($x, $y, $n)); // This code is contributed by Code_Mech. ?> |
-2
Time Complexity: O(1)
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.
Recommended Posts:
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- 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,…
- Find Nth term of the series 5, 13, 25, 41, 61...
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Find Nth term of the series 3, 14, 39, 84...
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Find Nth term of the series 1, 8, 54, 384...
- Find Nth term of the series 1, 1, 2, 6, 24...
- Find Nth term of the series 0, 6, 0, 12, 0, 90...
- Program to find Nth term of the series 3, 6, 18, 24, ...
- Program to find Nth term of the series 2, 4, 3, 4, 15...
- Find the Nth term of the series 2 + 6 + 13 + 23 + . . .
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

