Nth term of a recurrence relation generated by two given arrays | Java program for the above approach ; Function to calculate Nth term of general recurrence relations ; Stores the generated sequence ; Current term is sum of previous k terms ; Print the nth term ; Driver Code ; Given Array F [ ] and C [ ] ; Given N and K ; Function call
import java . util . * ; import java . lang . * ; class GFG { static double mod = 1e9 + 7 ; static void NthTerm ( int F [ ] , int C [ ] , int K , int n ) { int ans [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < K ; i ++ ) ans [ i ] = F [ i ] ; for ( int i = K ; i <= n ; i ++ ) { for ( int j = i - K ; j < i ; j ++ ) { ans [ i ] += ans [ j ] ; ans [ i ] %= mod ; } } System . out . println ( ans [ n ] ) ; } public static void main ( String [ ] args ) { int F [ ] = { 0 , 1 } ; int C [ ] = { 1 , 1 } ; int K = 2 ; int N = 10 ; NthTerm ( F , C , K , N ) ; } }