Connell Sequence | CPP code to generate first ' n ' terms of Connell Sequence ; Function to generate a fixed number of even or odd terms . The size of r decides whether numbers to be generated even or odd . ; Generating the first ' n ' terms of Connell Sequence ; A dummy 0 is inserted at the beginning for consistency ; Calling function gen ( ) to generate ' k ' number of terms ; Checking if ' n ' terms are already generated ; Removing the previously inserted dummy 0 ; Driver Method
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < long long int > gen ( long long int n , vector < long long int > r ) { long long int a = r [ r . size ( ) - 1 ] ; a ++ ; for ( int i = 1 ; i <= n ; a += 2 , i ++ ) r . push_back ( a ) ; return r ; } vector < long long int > conell ( long long int n ) { vector < long long int > res ; long long int k = 1 ; res . push_back ( 0 ) ; while ( 1 ) { res = gen ( k , res ) ; k ++ ; int j = res . size ( ) - 1 ; while ( j != n && j + k > n ) k -- ; if ( j >= n ) break ; } res . erase ( res . begin ( ) ) ; return res ; } int main ( ) { long long int n = 10 ; cout << " The ▁ first ▁ " << n << " ▁ terms ▁ are " << endl ; vector < long long int > res = conell ( n ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] << " ▁ " ; cout << endl ; return 0 ; }