Nth term of given recurrence relation having each term equal to the product of previous K terms
Last Updated :
29 Jul, 2025
You are given an array arr[] containing the first k positive integers of a recurrence sequence. The task is to calculate the n-th term of the sequence using the below relation.
The recurrence relation defines the n-th term (Fn) as the product of the previous k terms:
Fn- 1 * Fn - 2 * Fn - 3 *.......* Fn - k
The array arr[] will contain the first k terms of the sequence (i.e., F1 to Fk), and we must use this to compute any Fn where n > k.
Examples:
Input: n = 5, arr[] = [1, 2]
Output: 32
Explanation: The sequence is: 1, 2, 2, 4, 8, 32...
From the 3rd term onward: F3 = 2×1 = 2, F4 = 2×2 = 4, F5 = 4×2 = 8, F6 = 8×4 = 32
Input: n = 6, arr[] = [1, 1, 1]
Output: 1
Explanation: The sequence is: 1, 1, 1, 1, 1, 1...
Every term is the product of the previous 3, which are all 1.
Input: n = 7, arr[] = [2, 3]
Output: 408146688
Explanation: The sequence is: 2, 3, 6, 18, 108, 1944, 209952...
Each term is the product of the last two terms. F7 = 1944 × 209952 = 408146688.
[Naive Approach] Generate all the Terms - O(n * k) Time and O(n) Space
The idea is to build the sequence iteratively by storing all terms up to the n-th in a result array. The thought process is that each term from index k onward is simply the product of the previous k terms. We recompute this product each time using a nested loop, which makes it straightforward but less efficient.
C++
// C++ code to compute the n-th term of a recurrence
// relation using Naive Approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the n-th term of the recurrence
int findNthTerm(int n, vector<int>& arr) {
int k = arr.size();
vector<int> res(n + 1, 0);
// Initialize first k terms
for (int i = 0; i < k; i++) {
res[i] = arr[i];
}
// Compute terms from k to n
for (int i = k; i <= n; i++) {
res[i] = 1;
// Multiply previous k terms
for (int j = i - k; j < i; j++) {
res[i] *= res[j];
}
}
return res[n];
}
int main() {
int n = 5;
vector<int> arr = {1, 2};
cout << findNthTerm(n, arr) << endl;
return 0;
}
Java
// Java code to compute the n-th term of a recurrence
// relation using Naive Approach
import java.util.*;
class GfG {
// Function to find the n-th term of the recurrence
static int findNthTerm(int n, int[] arr) {
int k = arr.length;
int[] res = new int[n + 1];
// Initialize first k terms
for (int i = 0; i < k; i++) {
res[i] = arr[i];
}
// Compute terms from k to n
for (int i = k; i <= n; i++) {
res[i] = 1;
// Multiply previous k terms
for (int j = i - k; j < i; j++) {
res[i] *= res[j];
}
}
return res[n];
}
public static void main(String[] args) {
int n = 5;
int[] arr = {1, 2};
System.out.println(findNthTerm(n, arr));
}
}
Python
# Python code to compute the n-th term of a recurrence
# relation using Naive Approach
def findNthTerm(n, arr):
k = len(arr)
res = [0] * (n + 1)
# Initialize first k terms
for i in range(k):
res[i] = arr[i]
# Compute terms from k to n
for i in range(k, n + 1):
res[i] = 1
# Multiply previous k terms
for j in range(i - k, i):
res[i] *= res[j]
return res[n]
if __name__ == "__main__":
n = 5
arr = [1, 2]
print(findNthTerm(n, arr))
C#
// C# code to compute the n-th term of a recurrence
// relation using Naive Approach
using System;
class GfG {
// Function to find the n-th term of the recurrence
public static int findNthTerm(int n, int[] arr) {
int k = arr.Length;
int[] res = new int[n + 1];
// Initialize first k terms
for (int i = 0; i < k; i++) {
res[i] = arr[i];
}
// Compute terms from k to n
for (int i = k; i <= n; i++) {
res[i] = 1;
// Multiply previous k terms
for (int j = i - k; j < i; j++) {
res[i] *= res[j];
}
}
return res[n];
}
public static void Main() {
int n = 5;
int[] arr = {1, 2};
Console.WriteLine(findNthTerm(n, arr));
}
}
JavaScript
// JavaScript code to compute the n-th term of a recurrence
// relation using Naive Approach
function findNthTerm(n, arr) {
let k = arr.length;
let res = new Array(n + 1).fill(0);
// Initialize first k terms
for (let i = 0; i < k; i++) {
res[i] = arr[i];
}
// Compute terms from k to n
for (let i = k; i <= n; i++) {
res[i] = 1;
// Multiply previous k terms
for (let j = i - k; j < i; j++) {
res[i] *= res[j];
}
}
return res[n];
}
// Driver Code
let n = 5;
let arr = [1, 2];
console.log(findNthTerm(n, arr));
[Expected Approach] Using Deque - O(n) Time and O(k) Space
The idea is to maintain a rolling product of the last k terms using a deque to efficiently compute the n-th term of the sequence. The thought process is that every new term (from index k+1 onward) is simply the product of the previous k elements. We avoid recomputing the entire product each time by dividing out the oldest term and multiplying in the newest one.
Steps to implement the above idea:
- Initialize k as the length of the input array and return the n-th value directly if n <= k.
- Use a deque to maintain the last k terms and a variable product to store their cumulative product.
- Fill the deque with the initial k terms, updating the product as you insert each value.
- Loop from index k to n, generating one new term per iteration using the current product.
- Append the product as the new term into the deque to extend the sequence.
- Remove the oldest term from the deque and update product by dividing it out.
- Multiply the newly added term to product and repeat until the n-th term is generated.
C++
// C++ code to compute the n-th term of a recurrence
// relation using Deque
#include <bits/stdc++.h>
using namespace std;
int findNthTerm(int n, vector<int> &arr) {
int k = arr.size();
if (n <= k) {
return arr[n - 1];
}
deque<int> dq;
int product = 1;
// Initialize deque and product
for (int i = 0; i < k; i++) {
dq.push_back(arr[i]);
product *= arr[i];
}
// Generate terms from k to n
for (int i = k; i <= n; i++) {
dq.push_back(product);
// Remove oldest term and update product
product = product / dq.front();
dq.pop_front();
product *= dq.back();
}
return dq.back();
}
int main() {
int n = 5;
vector<int> arr = {1, 2};
cout << findNthTerm(n, arr) << endl;
return 0;
}
Java
// Java code to compute the n-th term of a recurrence
// relation using Deque
import java.util.*;
class GfG {
public static int findNthTerm(int n, int[] arr) {
int k = arr.length;
if (n <= k) {
return arr[n - 1];
}
Deque<Integer> dq = new ArrayDeque<>();
int product = 1;
// Initialize deque and product
for (int i = 0; i < k; i++) {
dq.addLast(arr[i]);
product *= arr[i];
}
// Generate terms from k to n
for (int i = k; i <= n; i++) {
dq.addLast(product);
// Remove oldest term and update product
product = product / dq.peekFirst();
dq.pollFirst();
product *= dq.peekLast();
}
return dq.peekLast();
}
public static void main(String[] args) {
int n = 5;
int[] arr = {1, 2};
System.out.println(findNthTerm(n, arr));
}
}
Python
# Python code to compute the n-th term of a recurrence
# relation using Deque
from collections import deque
def findNthTerm(n, arr):
k = len(arr)
if n <= k:
return arr[n - 1]
dq = deque()
product = 1
# Initialize deque and product
for i in range(k):
dq.append(arr[i])
product *= arr[i]
# Generate terms from k to n
for i in range(k, n + 1):
dq.append(product)
# Remove oldest term and update product
product = product // dq[0]
dq.popleft()
product *= dq[-1]
return dq[-1]
if __name__ == "__main__":
n = 5
arr = [1, 2]
print(findNthTerm(n, arr))
C#
// C# code to compute the n-th term of a recurrence
// relation using Deque
using System;
using System.Collections.Generic;
class GfG {
public static int findNthTerm(int n, int[] arr) {
int k = arr.Length;
if (n <= k) {
return arr[n - 1];
}
Queue<int> dq = new Queue<int>();
int product = 1;
// Initialize deque and product
for (int i = 0; i < k; i++) {
dq.Enqueue(arr[i]);
product *= arr[i];
}
// Generate terms from k to n
for (int i = k; i <= n; i++) {
dq.Enqueue(product);
// Remove oldest term and update product
product = product / dq.Peek();
dq.Dequeue();
product *= dq.ToArray()[dq.Count - 1];
}
return dq.ToArray()[dq.Count - 1];
}
public static void Main() {
int n = 5;
int[] arr = {1, 2};
Console.WriteLine(findNthTerm(n, arr));
}
}
JavaScript
// JavaScript code to compute the n-th term of a recurrence
// relation using Deque
function findNthTerm(n, arr) {
let k = arr.length;
if (n <= k) {
return arr[n - 1];
}
let dq = [];
let product = 1;
// Initialize deque and product
for (let i = 0; i < k; i++) {
dq.push(arr[i]);
product *= arr[i];
}
// Generate terms from k to n
for (let i = k; i <= n; i++) {
dq.push(product);
// Remove oldest term and update product
product = product / dq[0];
dq.shift();
product *= dq[dq.length - 1];
}
return dq[dq.length - 1];
}
// Driver Code
let n = 5;
let arr = [1, 2];
console.log(findNthTerm(n, arr));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem