Count the occurrence of Nth term in first N terms of Van Eck 's sequence | Java program to count the occurrence of nth term in first n terms of Van Eck 's sequence ; Utility function to compute Van Eck 's sequence ; Initialize sequence array ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Get nth term of the sequence ; Count the occurrence of nth term in first n terms of the sequence ; Return count ; Driver code ; Pre - compute Van Eck 's sequence ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
class GFG { static int MAX = 100000 ; static int sequence [ ] = new int [ MAX + 1 ] ; static void vanEckSequence ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { sequence [ i ] = 0 ; } for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( sequence [ j ] == sequence [ i ] ) { sequence [ i + 1 ] = i - j ; break ; } } } } static int getCount ( int n ) { int nthTerm = sequence [ n - 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( sequence [ i ] == nthTerm ) count ++ ; } return count ; } public static void main ( String [ ] args ) { vanEckSequence ( ) ; int n = 5 ; System . out . println ( getCount ( n ) ) ; n = 11 ; System . out . println ( getCount ( n ) ) ; } }