text: Gijswijt 's Sequence | C ++ program to demonstrate Gijswijt 's sequence ; if the sequence is a ( 1 ) a ( 2 ) a ( 3 ) . . a ( n - 1 ) check if the sequence can be represented as x * ( y ^ k ) find the largest value of k ; count ; pattern of elements of size i from the end of sequence ; count ; extract the pattern in a reverse order ; check how many times the pattern is repeated ; if the element dosent match ; if the end of pattern is reached set value of k = 0 and increase the count ; return the max count ; print first n terms of Gijswijt 's sequence ; set the count ; stoes the element ; print the first n terms of the sequence ; push the element ; find the count for next number ; Driver code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int find_count ( vector < int > ele ) { int count = 0 ; for ( int i = 0 ; i < ele . size ( ) ; i ++ ) { vector < int > p ; int c = 0 ; for ( int j = ele . size ( ) - 1 ; j >= ( ele . size ( ) - 1 - i ) && j >= 0 ; j -- ) p . push_back ( ele [ j ] ) ; int j = ele . size ( ) - 1 , k = 0 ; while ( j >= 0 ) { if ( ele [ j ] != p [ k ] ) break ; j -- ; k ++ ; if ( k == p . size ( ) ) { c ++ ; k = 0 ; } } count = max ( count , c ) ; } return count ; } void solve ( int n ) { int count = 1 ; vector < int > ele ; for ( int i = 0 ; i < n ; i ++ ) { cout << count << " , ▁ " ; ele . push_back ( count ) ; count = find_count ( ele ) ; } } int main ( ) { int n = 10 ; solve ( n ) ; return 0 ; }