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
Nth term of given recurrence relation having each term equal to the product of previous K terms
Recurrence Relation In mathematics, recurrence relation refers to an equation where the nth term of the sequence is equal to some combination of the previous terms.
For a recurrence relation where each term equals the product of previous K terms, let's define N and K along with an array arr[] containing the first K terms of the relation.
Thus, the nth term is given by
$$\mathrm{F_N= F_{N1} ? F_{N2} ? F_{N3} ? . . .? F_{NK}}$$
Problem Statement
Given two positive integers N and K and an array of integers containing K positive integers. Find the Nth term of the recurrence relation.
Sample Example 1
Input: N = 5, K = 3, arr[] = {2,1,4}
Output: 1024
Explanation
F0 = 2, F1 = 1, F2 = 4 F3 = 2*1*4 = 8 F4 = 1*4*8 = 32 F5 = 4*8*32 = 1024
Sample Example 2
Input: N = 10, K = 4, arr[] = {0, 1, 3, 2}
Output: 0
Explanation
F0 = 0, F1 = 0, F2 = 0 F3 = 0, F4 = 0, F5 = 0, F6 = 0, F7 = 0, F8 = 0, F9 = 0, F10 = 0
Approach 1: Brute Force Solution
Brute force approach for the problem is to calculate al the terms in the recurrence relation and print the nth term term required.
Pseudocode
function NthTerm(arr[], K, N)
Create an array ans of size N + 1 and initialize all elements to 0.
// Initialize first K terms
for i from 0 to K - 1
ans[i] = F[i]
// Find all terms from Kth term to the Nth term
for i from K to N
ans[i] = 1
for j from i - K to i - 1
// Current term is the product of previous K terms
ans[i] = (ans[i] * ans[j]) % MOD // If MOD is needed
// Print the Nth term
print ans[N]
end function
Example: C++ Implementation
In the following code, we are finding all the terms in the recurrence relation.
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007; // The modulus value if needed.
// Function to find the Nth term
void NthTerm(int arr[], int K, int N){
// Stores the terms of recurrence relation
vector<int> ans(N + 1, 0);
// Initialize first K terms
for (int i = 0; i < K; i++)
ans[i] = arr[i];
// Find all terms from Kth term to the Nth term
for (int i = K; i <= N; i++) {
ans[i] = 1;
for (int j = i - K; j < i; j++) {
// Current term is the product of previous K terms
ans[i] = (1LL * ans[i] * ans[j]) % MOD;
}
}
// Print the Nth term
cout << ans[N] << endl;
}
int main(){
// Given N, K, and F[]
int arr[] = { 1, 2 };
int K = 2;
int N = 5;
// Function Call
NthTerm(arr, K, N);
return 0;
}
Output
32
Approach 2: Optimized Solution
An optimised solution will use a fast exponentiation technique for efficient modular exponentiation. The deque data structure can be used to store the terms of the sequence.
Pseudocode
// Function to calculate (x ^ y) % p using fast exponentiation
function power(x, y, p):
res = 1
x = x % p
while y > 0:
if y is odd:
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
// Function to calculate modular inverse using Fermat's Little Theorem
function modInverse(n, p):
return power(n, p - 2, p)
// Function to find Nth term of the given recurrence relation
function NthTerm(arr[], K, N):
Create an empty deque named q
product = 1
// Initialize the product of the first K terms and the deque
for i from 0 to K - 1:
product = (product * arr[i]) % MOD
q.push_back(arr[i])
// Push (K + 1)th term to the deque
q.push_back(product)
for i from K + 1 to N:
f = q.front()
e = q.back()
// Calculate the (i + 1)th term
next_term = ((e % MOD * e % MOD) % MOD * modInverse(f, MOD)) % MOD
// Add the current term to the end of the deque
q.push_back(next_term)
// Remove the first number from the deque
q.pop_front()
// Print the Nth term
print q.back()
end function
Example: C++ Implementation
The above approach can be implemented as.
#include <iostream>
#include <deque>
using namespace std;
const int MOD = 1000000007; // The modulus value if needed.
// Function to calculate (x ^ y) % p using fast exponentiation ( O(log y) )
int power(int x, int y, int p){
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// Function to find mod inverse using Fermat Little Theorem
int modInverse(int n, int p){
return power(n, p - 2, p);
}
// Function to find the Nth term of the given recurrence relation
void NthTerm(int arr[], int K, int N){
deque<int> q;
int product = 1;
// Initialize the product of the first K terms and the deque
for (int i = 0; i < K; i++) {
product = (product * arr[i]) % MOD;
q.push_back(arr[i]);
}
// Push (K + 1)th term to the deque
q.push_back(product);
for (int i = K + 1; i <= N; i++) {
int f = *q.begin();
int e = *q.rbegin();
// Calculate the ith term
int next_term = ((e % MOD * e % MOD) % MOD * (modInverse(f, MOD))) % MOD;
// Add the current term to the end of the deque
q.push_back(next_term);
// Remove the first number from the deque
q.pop_front();
}
// Print the Nth term
cout << *q.rbegin() << endl;
}
int main(){
// Given N, K, and arr[]
int arr[] = { 1, 2 };
int K = 2;
int N = 5;
// Function Call
NthTerm(arr, K, N);
return 0;
}
Output
0
Conclusion
In conclusion, the Nth term of given recurrence relation having each term equal to the product of previous K terms can be found using the above two approaches where the first one is the brute force approach followed by a more optimised one.