codeparrot/xlcost-text-to-code huggingface dataset:
Return maximum occurring character in an input string | C ++ program to output the maximum occurring character in a string ; Create array to keep the count of individual characters and initialize the array as 0 ; Construct character count array from the input string . ; Initialize max count ; Initialize result ; Traversing through the string and maintaining the count of each character ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE #define ASCII_SIZE  256 NEW_LINE using namespace std ; char getMaxOccuringChar ( char * str ) { int count [ ASCII_SIZE ] = { 0 } ; int len = strlen ( str ) ; int max = 0 ; char result ; for ( int i = 0 ; i < len ; i ++ ) { count [ str [ i ] ] ++ ; if ( max < count [ str [ i ] ] ) { max = count [ str [ i ] ] ; result = str [ i ] ; } } return result ; } int main ( ) { char str [ ] = " sample ▁ string " ; cout << " Max ▁ occurring ▁ character ▁ is ▁ " << getMaxOccuringChar ( str ) ; }
Group words with same set of characters | C ++ program to print all words that have the same unique character set ; Generates a key from given string . The key contains all unique characters of given string in sorted order consisting of only distinct elements . ; store all unique characters of current word in key ; Print all words together with same character sets . ; Stores indexes of all words that have same set of unique characters . ; Traverse all words ; print all words that have the same unique character set ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_CHAR  26 NEW_LINE string getKey ( string & str ) { bool visited [ MAX_CHAR ] = { false } ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) visited [ str [ j ] - ' a ' ] = true ; string key = " " ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) if ( visited [ j ] ) key = key + ( char ) ( ' a ' + j ) ; return key ; } void wordsWithSameCharSet ( string words [ ] , int n ) { unordered_map < string , vector < int > > Hash ; for ( int i = 0 ; i < n ; i ++ ) { string key = getKey ( words [ i ] ) ; Hash [ key ] . push_back ( i ) ; } for ( auto it = Hash . begin ( ) ; it != Hash . end ( ) ; it ++ ) { for ( auto v = ( * it ) . second . begin ( ) ; v != ( * it ) . second . end ( ) ; v ++ ) cout << words [ * v ] << " , ▁ " ; cout << endl ; } } int main ( ) { string words [ ] = { " may " , " student " , " students " , " dog " , " studentssess " , " god " , " cat " , " act " , " tab " , " bat " , " flow " , " wolf " , " lambs " , " amy " , " yam " , " balms " , " looped " , " poodle " } ; int n = sizeof ( words ) / sizeof ( words [ 0 ] ) ; wordsWithSameCharSet ( words , n ) ; return 0 ; }
Second most repeated word in a sequence | C ++ program to find out the second most repeated word ; Function to find the word ; Store all the words with its occurrence ; find the second largest occurrence ; Return string with occurrence equals to sec_max ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string secMostRepeated ( vector < string > seq ) { unordered_map < string , int > occ ; for ( int i = 0 ; i < seq . size ( ) ; i ++ ) occ [ seq [ i ] ] ++ ; int first_max = INT_MIN , sec_max = INT_MIN ; for ( auto it = occ . begin ( ) ; it != occ . end ( ) ; it ++ ) { if ( it -> second > first_max ) { sec_max = first_max ; first_max = it -> second ; } else if ( it -> second > sec_max && it -> second != first_max ) sec_max = it -> second ; } for ( auto it = occ . begin ( ) ; it != occ . end ( ) ; it ++ ) if ( it -> second == sec_max ) return it -> first ; } int main ( ) { vector < string > seq = { " ccc " , " aaa " , " ccc " , " ddd " , " aaa " , " aaa " } ; cout << secMostRepeated ( seq ) ; return 0 ; }
Check if a Binary Tree ( not BST ) has duplicate values | C ++ Program to check duplicates in Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; If tree is empty , there are no duplicates . ; If current node 's data is already present. ; Insert current node ; Recursively check in left and right subtrees . ; To check if tree has duplicates ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } bool checkDupUtil ( Node * root , unordered_set < int > & s ) { if ( root == NULL ) return false ; if ( s . find ( root -> data ) != s . end ( ) ) return true ; s . insert ( root -> data ) ; return checkDupUtil ( root -> left , s ) || checkDupUtil ( root -> right , s ) ; } bool checkDup ( struct Node * root ) { unordered_set < int > s ; return checkDupUtil ( root , s ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 2 ) ; root -> left -> left = newNode ( 3 ) ; if ( checkDup ( root ) ) printf ( " Yes " ) ; else printf ( " No " ) ; return 0 ; }
Expression Tree | C ++ program for expression tree ; An expression tree node ; A utility function to check if ' c ' is an operator ; Utility function to do inorder traversal ; A utility function to create a new node ; Returns root of constructed tree for given postfix expression ; Traverse through every character of input expression ; If operand , simply push into stack ; operator ; Pop two top nodes Store top ; make them children ; Add this subexpression to stack ; only element will be root of expression tree ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct et { char value ; et * left , * right ; } ; bool isOperator ( char c ) { if ( c == ' + ' c == ' - ' c == ' * ' c == ' / ' c == ' ^ ' ) return true ; return false ; } void inorder ( et * t ) { if ( t ) { inorder ( t -> left ) ; printf ( " % c ▁ " , t -> value ) ; inorder ( t -> right ) ; } } et * newNode ( char v ) { et * temp = new et ; temp -> left = temp -> right = NULL ; temp -> value = v ; return temp ; } ; et * constructTree ( char postfix [ ] ) { stack < et * > st ; et * t , * t1 , * t2 ; for ( int i = 0 ; i < strlen ( postfix ) ; i ++ ) { if ( ! isOperator ( postfix [ i ] ) ) { t = newNode ( postfix [ i ] ) ; st . push ( t ) ; } else { t = newNode ( postfix [ i ] ) ; t1 = st . top ( ) ; st . pop ( ) ; t2 = st . top ( ) ; st . pop ( ) ; t -> right = t1 ; t -> left = t2 ; st . push ( t ) ; } } t = st . top ( ) ; st . pop ( ) ; return t ; } int main ( ) { char postfix [ ] = " ab + ef * g * - " ; et * r = constructTree ( postfix ) ; printf ( " infix ▁ expression ▁ is ▁ STRNEWLINE " ) ; inorder ( r ) ; return 0 ; }
Queries to count sum of rows and columns of a Matrix present in given ranges | C ++ program for the above approach ; Function to search for the leftmost index of given number ; Initialize low , high and ans ; Stores mid ; If A [ mid ] >= num ; Function to search for the rightmost index of given number ; Initialise low , high and ans ; Stores mid ; If A [ mid ] <= num ; Update ans ; Update mid ; Update high ; Function to preprocess the matrix to execute the queries ; Stores the sum of each row ; Stores the sum of each col ; Traverse the matrix and calculate sum of each row and column ; Insert all row sums in sum_list ; Insert all column sums in sum_list ; Sort the array in ascending order ; Traverse the array queries [ ] [ ] ; Search the leftmost index of L ; Search the rightmost index of R ; Driver Code ; Given dimensions of matrix ; Given matrix ; Given number of queries ; Given queries ; Function call to count the number row - sums and column - sums present in the given ranges
#include <bits/stdc++.h> NEW_LINE using namespace std ; int left_search ( vector < int > A , int num ) { int low = 0 , high = A . size ( ) - 1 ; int ans = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( A [ mid ] >= num ) { ans = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } return ans ; } int right_search ( vector < int > A , int num ) { int low = 0 , high = A . size ( ) - 1 ; int ans = high ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( A [ mid ] <= num ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } void totalCount ( vector < vector < int > > A , int N , int M , vector < vector < int > > queries , int Q ) { vector < int > row_sum ( N ) ; vector < int > col_sum ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { row_sum [ i ] += A [ i ] [ j ] ; col_sum [ j ] += A [ i ] [ j ] ; } } vector < int > sum_list ; for ( int i = 0 ; i < N ; i ++ ) sum_list . push_back ( row_sum [ i ] ) ; for ( int i = 0 ; i < M ; i ++ ) sum_list . push_back ( col_sum [ i ] ) ; sort ( sum_list . begin ( ) , sum_list . end ( ) ) ; for ( int i = 0 ; i < Q ; i ++ ) { int L = queries [ i ] [ 0 ] ; int R = queries [ i ] [ 1 ] ; int l = left_search ( sum_list , L ) ; int r = right_search ( sum_list , R ) ; cout << r - l + 1 << " ▁ " ; } } int main ( ) { int N = 3 , M = 2 ; vector < vector < int > > A = { { 13 , 3 } , { 9 , 4 } , { 6 , 10 } } ; int Q = 2 ; vector < vector < int > > queries = { { 10 , 20 } , { 25 , 35 } } ; totalCount ( A , N , M , queries , Q ) ; }
Smallest element repeated exactly ‘ k ’ times ( not limited to small range ) | C ++ program to find the smallest element with frequency exactly k . ; Map is used to store the count of elements present in the array ; Traverse the map and find minimum element with frequency k . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestKFreq ( int a [ ] , int n , int k ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ a [ i ] ] ++ ; int res = INT_MAX ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; ++ it ) if ( it -> second == k ) res = min ( res , it -> first ) ; return ( res != INT_MAX ) ? res : -1 ; } int main ( ) { int arr [ ] = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = sizeof ( arr ) / ( sizeof ( arr [ 0 ] ) ) ; cout << smallestKFreq ( arr , n , k ) ; return 0 ; }
Maximize profit that can be earned by selling an item among N buyers |  ; Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Increment count ; Update the maximum profit ; Return the maximum possible price ; Driver code
#include <iostream> NEW_LINE #include <climits> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; int maximumProfit ( int arr [ ] , int n ) { int ans = INT_MIN ; int price = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) { count ++ ; } } if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } int main ( ) { int arr [ ] = { 22 , 87 , 9 , 50 , 56 , 43 } ; cout << maximumProfit ( arr , 6 ) ; return 0 ; }
Lexicographically smallest permutation of the array possible by at most one swap | C ++ implementation of the above approach ; Function to print the elements of the array arr [ ] ; Traverse the array arr [ ] ; Function to convert given array to lexicographically smallest permutation possible by swapping at most one pair ; Stores the index of the first element which is not at its correct position ; Checks if any such array element exists or not ; Traverse the given array ; If element is found at i ; If the first array is not in correct position ; Store the index of the first elements ; Store the index of the first element ; Swap the pairs ; Print the array ; Driver code ; Given array ; Store the size of the array
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void print ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } void makeLexicographically ( int arr [ ] , int N ) { int index = 0 ; int temp = 0 ; int check = 0 ; int condition = 0 ; int element = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( element == arr [ i ] ) { check = i ; break ; } else if ( arr [ i ] != i + 1 && check == 0 ) { index = i ; check = 1 ; condition = -1 ; element = i + 1 ; } } if ( condition == -1 ) { temp = arr [ index ] ; arr [ index ] = arr [ check ] ; arr [ check ] = temp ; } print ( arr , N ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; makeLexicographically ( arr , N ) ; return 0 ; }
Check if uppercase characters in a string are used correctly or not | C ++ program for the above approach ; Function to check if the character c is in lowercase or not ; Function to check if the character c is in uppercase or not ; Utility function to check if uppercase characters are used correctly or not ; Length of string ; If the first character is in lowercase ; Otherwise ; Check if all characters are in uppercase or not ; If all characters are in uppercase ; Check if all characters except the first are in lowercase ; Function to check if uppercase characters are used correctly or not ; Stores whether the use of uppercase characters are correct or not ; If correct ; Otherwise ; Driver Code ; Given string ; Function call to check if use of uppercase characters is correct or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isLower ( char c ) { return c >= ' a ' and c < = ' z ' ; } bool isUpper ( char c ) { return c >= ' A ' and c < = ' Z ' ; } bool detectUppercaseUseUtil ( string S ) { int N = S . size ( ) ; int i ; if ( isLower ( S [ 0 ] ) ) { i = 1 ; while ( S [ i ] && isLower ( S [ i ] ) ) ++ i ; return i == N ? true : false ; } else { i = 1 ; while ( S [ i ] && isUpper ( S [ i ] ) ) ++ i ; if ( i == N ) return true ; else if ( i > 1 ) return false ; while ( S [ i ] && isLower ( S [ i ] ) ) ++ i ; return i == N ? true : false ; } } void detectUppercaseUse ( string S ) { bool check = detectUppercaseUseUtil ( S ) ; if ( check ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string S = " GeeKs " ; detectUppercaseUse ( S ) ; return 0 ; }
Queries to count array elements from a given range having a single set bit | C ++ implementation for above approach ; Check if N has only one set bit ; Function to build segment tree ; If se is leaf node ; Update the node ; Stores mid value of segment [ ss , se ] ; Recursive call for left Subtree ; Recursive call for right Subtree ; Update the Current Node ; Function to update a value at Xth index ; If ss is equal to X ; Update the Xth node ; Update the tree ; Stores the mid of segment [ ss , se ] ; Update current node ; Count of numbers having one set bit ; If L > se or R < ss Invalid Range ; If [ ss , se ] lies inside the [ L , R ] ; Stores the mid of segment [ ss , se ] ; Return the sum after recursively traversing left and right subtree ; Function to solve queries ; Initialise Segment tree ; Build segment tree ; Perform queries ; Driver Code ; Input ; Function call to solve queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int N ) { if ( N == 0 ) return 0 ; return ! ( N & ( N - 1 ) ) ; } void build_seg_tree ( int ss , int se , int si , int tree [ ] , int arr [ ] ) { if ( ss == se ) { tree [ si ] = check ( arr [ ss ] ) ; return ; } int mid = ( ss + se ) / 2 ; build_seg_tree ( ss , mid , 2 * si + 1 , tree , arr ) ; build_seg_tree ( mid + 1 , se , 2 * si + 2 , tree , arr ) ; tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] ; } void update ( int ss , int se , int si , int X , int V , int tree [ ] , int arr [ ] ) { if ( ss == se ) { if ( ss == X ) { arr [ X ] = V ; tree [ si ] = check ( V ) ; } return ; } int mid = ( ss + se ) / 2 ; if ( X <= mid ) update ( ss , mid , 2 * si + 1 , X , V , tree , arr ) ; else update ( mid + 1 , se , 2 * si + 2 , X , V , tree , arr ) ; tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] ; } int query ( int l , int r , int ss , int se , int si , int tree [ ] ) { if ( r < ss l > se ) return 0 ; if ( l <= ss && r >= se ) return tree [ si ] ; int mid = ( ss + se ) / 2 ; return query ( l , r , ss , mid , 2 * si + 1 , tree ) + query ( l , r , mid + 1 , se , 2 * si + 2 , tree ) ; } void Query ( int arr [ ] , int N , vector < vector < int > > Q ) { int tree [ 4 * N ] = { 0 } ; build_seg_tree ( 0 , N - 1 , 0 , tree , arr ) ; for ( int i = 0 ; i < ( int ) Q . size ( ) ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) cout << query ( Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 , N - 1 , 0 , tree ) << " ▁ " ; else update ( 0 , N - 1 , 0 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , tree , arr ) ; } } int main ( ) { int arr [ ] = { 12 , 11 , 16 , 2 , 32 } ; vector < vector < int > > Q { { 1 , 0 , 2 } , { 2 , 4 , 24 } , { 1 , 1 , 4 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Query ( arr , N , Q ) ; return 0 ; }
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X |  ; Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Binary Search ; Checks if sum of first ' mid ' natural numbers is greater than equal to X ; Update res ; Update high ; Update low ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isGreaterEqual ( int N , int X ) { return ( N * 1LL * ( N + 1 ) / 2 ) >= X ; } int minimumPossible ( int X ) { int low = 1 , high = X , res = -1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( isGreaterEqual ( mid , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } return res ; } int main ( ) { int X = 14 ; cout << minimumPossible ( X ) ; return 0 ; }
Minimum moves required to come out of a grid safely | C ++ program to implement the above approach ; Stores size of the grid ; Function to check valid cells of the grid ; Checks for the border sides ; Function to find shortest distance between two cells of the grid ; Rows of the grid ; Column of the grid ; Stores possible move of the person ; Store possible cells visited by the person ; Store possible cells which are burning ; Traverse the grid ; If current cell is burning ; If person is in the current cell ; Stores shortest distance between two cells ; Check if a cell is visited by the person or not ; While pQ is not empty ; Update depth ; Popped all the cells from pQ and mark all adjacent cells of as visited ; Front element of the queue pQ ; Remove front element of the queue pQ ; If current cell is burning ; Find all adjacent cells ; Stores row number of adjacent cell ; Stores column number of adjacent cell ; Checks if current cell is valid ; Mark the cell as visited ; Enqueue the cell ; Checks the escape condition ; Burn all the adjacent cells of burning cells ; Front element of the queue fQ ; Delete front element of the queue fQ ; Find adjacent cells of burning cell ; Stores row number of adjacent cell ; Stores column number of adjacent cell ; Checks if current cell is valid ; Burn all the adjacent cells of current cell ; Driver Code ; Given grid
#include <bits/stdc++.h> NEW_LINE using namespace std ; int m , n ; bool valid ( int x , int y ) { return ( x >= 0 && x < m && y >= 0 && y < n ) ; } bool border ( int x , int y ) { return ( x == 0 x == m - 1 y == 0 y == n - 1 ) ; } int minStep ( vector < vector < int > > mat ) { m = mat . size ( ) ; n = mat [ 0 ] . size ( ) ; int dx [ ] = { 1 , -1 , 0 , 0 } ; int dy [ ] = { 0 , 0 , 1 , -1 } ; queue < pair < int , int > > pQ ; queue < pair < int , int > > fQ ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] == 2 ) fQ . push ( { i , j } ) ; else if ( mat [ i ] [ j ] == 1 ) { if ( border ( i , j ) ) return 0 ; pQ . push ( { i , j } ) ; } } } int depth = 0 ; vector < vector < int > > visited ( n , vector < int > ( m , 0 ) ) ; while ( pQ . size ( ) > 0 ) { depth ++ ; for ( int i = pQ . size ( ) ; i > 0 ; i -- ) { pair < int , int > pos = pQ . front ( ) ; pQ . pop ( ) ; if ( mat [ pos . first ] [ pos . second ] == 2 ) continue ; for ( int j = 0 ; j < 4 ; j ++ ) { int x = pos . first + dx [ j ] ; int y = pos . second + dy [ j ] ; if ( valid ( x , y ) && mat [ x ] [ y ] != 2 && ! visited [ x ] [ y ] ) { visited [ x ] [ y ] = 1 ; pQ . push ( pair < int , int > ( x , y ) ) ; if ( border ( x , y ) ) return depth ; } } } for ( int i = fQ . size ( ) ; i > 0 ; i -- ) { pair < int , int > pos = fQ . front ( ) ; fQ . pop ( ) ; for ( int j = 0 ; j < 4 ; j ++ ) { int x = pos . first + dx [ j ] ; int y = pos . second + dy [ j ] ; if ( valid ( x , y ) && mat [ x ] [ y ] != 2 ) { mat [ x ] [ y ] = 2 ; fQ . push ( pair < int , int > ( x , y ) ) ; } } } } return -1 ; } int main ( ) { vector < vector < int > > grid = { { 0 , 0 , 0 , 0 } , { 2 , 0 , 0 , 0 } , { 2 , 1 , 0 , 0 } , { 2 , 2 , 0 , 0 } } ; cout << minStep ( grid ) ; }
Find sum of non | C ++ Find the sum of all non - repeated elements in an array ; Find the sum of all non - repeated elements in an array ; sort all elements of array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) sum = sum + arr [ i ] ; } return sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findSum ( arr , n ) ; return 0 ; }
Length of the longest subsequence such that XOR of adjacent elements is equal to K | C ++ program for the above approach ; Function to find maximum length of subsequence having XOR of adjacent elements equal to K ; Store maximum length of subsequence ; Stores the dp - states ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Iterate over the range [ 0 , i - 1 ] ; If arr [ i ] ^ arr [ j ] = = K ; Update the dp [ i ] ; Update the maximum subsequence length ; If length of longest subsequence is less than 2 then return 0 ; Driver Code ; Input ; Print the length of longest subsequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorSubsequence ( int a [ ] , int n , int k ) { int ans = 0 ; int dp [ n ] = { 0 } ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( ( a [ i ] ^ a [ j ] ) == k ) dp [ i ] = max ( dp [ i ] , dp [ j ] + 1 ) ; } ans = max ( ans , dp [ i ] ) ; dp [ i ] = max ( 1 , dp [ i ] ) ; } return ans >= 2 ? ans : 0 ; } int main ( ) { int arr [ ] = { 3 , 2 , 4 , 3 , 5 } ; int K = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << xorSubsequence ( arr , N , K ) ; return 0 ; }