codeparrot/xlcost-text-to-code huggingface dataset:
Reverse Level Order Traversal | A recursive C # program to print reverse level order traversal using stack and queue ; A binary tree node has data , pointer to left and right children ; Given a binary tree , print its nodes in reverse level order ; Do something like normal level order traversal order . Following are the differences with normal level order traversal 1 ) Instead of printing a node , we push the node to stack 2 ) Right subtree is visited before left subtree ; Dequeue node and make it root ; Enqueue right child ; NOTE : RIGHT CHILD IS ENQUEUED BEFORE LEFT ; Enqueue left child ; Now pop all items from stack one by one and print them ; Driver code ; Let us create trees shown in above diagram
using System . Collections . Generic ; using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right ; } } public class BinaryTree { Node root ; void reverseLevelOrder ( Node node ) { Stack < Node > S = new Stack < Node > ( ) ; Queue < Node > Q = new Queue < Node > ( ) ; Q . Enqueue ( node ) ; while ( Q . Count > 0 ) { node = Q . Peek ( ) ; Q . Dequeue ( ) ; S . Push ( node ) ; if ( node . right != null ) Q . Enqueue ( node . right ) ; if ( node . left != null ) Q . Enqueue ( node . left ) ; } while ( S . Count > 0 ) { node = S . Peek ( ) ; Console . Write ( node . data + " ▁ " ) ; S . Pop ( ) ; } } public static void Main ( ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; Console . WriteLine ( " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ : " ) ; tree . reverseLevelOrder ( tree . root ) ; } }
Modify array of strings by replacing characters repeating in the same or remaining strings | C # program for the above approach ; Function to remove duplicate characters across the strings ; Stores distinct characters ; Size of the array ; Stores the list of modified strings ; Traverse the array ; Stores the modified string ; Iterate over the characters of the modified string ; If character is already present ; Insert character into the Set ; Print the list of modified strings ; Print each string ; Driver Code ; Given array of strings ; Function Call to modify the given array of strings
using System ; using System . Collections . Generic ; class GFG { static void removeDuplicateCharacters ( string [ ] arr ) { HashSet < int > cset = new HashSet < int > ( ) ; int n = arr . Length ; List < string > Out = new List < string > ( ) ; foreach ( string str in arr ) { string out_curr = " " ; foreach ( char ch in str . ToCharArray ( ) ) { if ( cset . Contains ( ch ) ) continue ; out_curr += ch ; cset . Add ( ch ) ; } if ( out_curr . Length != 0 ) Out . Add ( out_curr ) ; } for ( int i = 0 ; i < Out . Count ; i ++ ) { Console . Write ( Out [ i ] + " ▁ " ) ; } } static public void Main ( ) { string [ ] arr = { " Geeks " , " For " , " Geeks " , " Post " } ; removeDuplicateCharacters ( arr ) ; } }
Sum of array elements which are prime factors of a given number | C # program for the above approach ; Function to check if a number is prime or not ; Corner cases ; Check if n is a multiple of 2 or 3 ; Above condition allows to check only for every 6 th number , starting from 5 ; If n is a multiple of i and i + 2 ; Function to find the sum of array elements which are prime factors of K ; Stores the required sum ; Traverse the given array ; If current element is a prime factor of k , add it to the sum ; Print the result ; Driver code ; Given arr [ ] ; Store the size of the array
using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void primeFactorSum ( int [ ] arr , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 && isPrime ( arr [ i ] ) ) { sum = sum + arr [ i ] ; } } Console . Write ( sum ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 5 , 6 , 7 , 15 } ; int N = arr . Length ; int K = 35 ; primeFactorSum ( arr , N , K ) ; } }
Minimum number of array elements from either ends required to be subtracted from X to reduce X to 0 | C # Program to implement the above approach ; Function to count the minimum number of operations required to reduce x to 0 ; If sum of the array is less than x ; Stores the count of operations ; Two pointers to traverse the array ; Reduce x by the sum of the entire array ; Iterate until l reaches the front of the array ; If sum of elements from the front exceeds x ; Shift towards left ; If sum exceeds 0 ; Reduce x by elements from the right ; If x is reduced to 0 ; Update the minimum count of operations required ; Driver Code
using System ; class GFG { static int minOperations ( int [ ] nums , int x ) { int sum = 0 ; for ( int i = 0 ; i < x ; i ++ ) sum += nums [ i ] ; if ( sum < x ) return - 1 ; int ans = Int32 . MaxValue ; int l = nums . Length - 1 , r = nums . Length ; x -= sum ; while ( l >= 0 ) { if ( x <= 0 ) { x += nums [ l ] ; l -= 1 ; } if ( x > 0 ) { r -= 1 ; x -= nums [ r ] ; } if ( x == 0 ) { ans = Math . Min ( ans , ( l + 1 ) + ( nums . Length - r ) ) ; } } if ( ans < Int32 . MaxValue ) return ans ; else return - 1 ; } public static void Main ( ) { int [ ] nums = { 1 , 1 , 4 , 2 , 3 } ; int x = 5 ; Console . Write ( minOperations ( nums , x ) ) ; } }
Number of subarrays having even product | C # program for the above approach ; Function to count subarrays with even product ; Total number of subarrays ; Counter variables ; Traverse the array ; If current element is odd ; Update count of subarrays with odd product up to index i ; Print count of subarrays with even product ; Driver Code ; Input ; Length of an array ; Function call to count even product subarrays
using System ; class GFG { static void evenproduct ( int [ ] arr , int length ) { int total_subarray = length * ( length + 1 ) / 2 ; int total_odd = 0 ; int count_odd = 0 ; for ( int i = 0 ; i < arr . Length ; ++ i ) { if ( arr [ i ] % 2 == 0 ) { count_odd = 0 ; } else { ++ count_odd ; total_odd += count_odd ; } } Console . WriteLine ( total_subarray - total_odd ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 5 , 4 , 9 } ; int length = arr . Length ; evenproduct ( arr , length ) ; } }
Check if all rows of a Binary Matrix have all ones placed adjacently or not | C # program for the above approach ; Function to check if all 1 s are placed adjacently in an array or not ; Base Case ; Stores the sum of XOR of all pair of adjacent elements ; Calculate sum of XOR of all pair of adjacent elements ; Check for corner cases ; Return true ; Function to check if all the rows have all 1 s grouped together or not ; Traverse each row ; Check if all 1 s are placed together in the ith row or not ; Function to check if all 1 s in a row are grouped together in a matrix or not ; Print the result ; Driver code ; Given matrix ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool checkGroup ( List < int > arr ) { if ( arr . Count <= 2 ) return true ; int corner = arr [ 0 ] + arr [ arr . Count - 1 ] ; int xorSum = 0 ; for ( int i = 0 ; i < arr . Count - 1 ; i ++ ) xorSum += ( arr [ i ] ^ arr [ i + 1 ] ) ; if ( corner == 0 ) if ( xorSum > 2 ) return false ; else if ( corner == 1 ) if ( xorSum > 1 ) return false ; else if ( xorSum > 0 ) return false ; return true ; } static bool isInGroupUtil ( int [ , ] mat ) { for ( int i = 0 ; i < mat . GetLength ( 1 ) ; i ++ ) { List < int > arr = new List < int > ( ) ; for ( int j = 0 ; j < mat . GetLength ( 0 ) ; j ++ ) { arr . Add ( mat [ i , j ] ) ; } if ( ! checkGroup ( arr ) ) return false ; } return true ; } static void isInGroup ( int [ , ] mat ) { bool ans = isInGroupUtil ( mat ) ; if ( ans ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static void Main ( ) { int [ , ] mat = { { 0 , 1 , 1 , 0 } , { 1 , 1 , 0 , 0 } , { 0 , 0 , 0 , 1 } , { 1 , 1 , 1 , 0 } } ; isInGroup ( mat ) ; } }
Print all numbers up to N having product of digits equal to K | C # program for the above approach ; Function to find the product of digits of a number ; Stores the product of digits of a number ; Return the product ; Function to print all numbers upto N having product of digits equal to K ; Stores whether any number satisfying the given conditions exists or not ; Iterate over the range [ 1 , N ] ; If product of digits of arr [ i ] is equal to K or not ; Print that number ; If no numbers are found ; Driver Code ; Given value of N & K ; Function call to print all numbers from [ 1 , N ] with product of digits K
using System ; class GFG { static int productOfDigits ( int N ) { int product = 1 ; while ( N != 0 ) { product = product * ( N % 10 ) ; N = N / 10 ; } return product ; } static void productOfDigitsK ( int N , int K ) { int flag = 0 ; for ( int i = 1 ; i <= N ; ++ i ) { if ( K == productOfDigits ( i ) ) { Console . Write ( i + " ▁ " ) ; flag = 1 ; } } if ( flag == 0 ) Console . WriteLine ( - 1 ) ; } static public void Main ( ) { int N = 500 , K = 10 ; productOfDigitsK ( N , K ) ; } }
Minimize difference between maximum and minimum array elements by removing a K | C # program for the above approach ; Function to minimize difference between maximum and minimum array elements by removing a K - length subarray ; Size of array ; Stores the maximum and minimum in the suffix subarray [ i . . N - 1 ] ; Traverse the array ; Stores the maximum and minimum in the prefix subarray [ 0 . . i - 1 ] ; Store the minimum difference ; Traverse the array ; If the suffix doesn 't exceed  the end of the array ; Store the maximum element in array after removing subarray of size K ; Stores the maximum element in array after removing subarray of size K ; Update minimum difference ; Updating the maxPrefix and minPrefix with current element ; Print the minimum difference ; Driver Code
using System ; using System . Collections . Generic ; class GFg { static void minimiseDifference ( List < int > arr , int K ) { int N = arr . Count ; int [ ] maxSuffix = new int [ N + 1 ] ; int [ ] minSuffix = new int [ N + 1 ] ; maxSuffix [ N ] = - 1000000000 ; minSuffix [ N ] = 1000000000 ; maxSuffix [ N - 1 ] = arr [ N - 1 ] ; minSuffix [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; -- i ) { maxSuffix [ i ] = Math . Max ( maxSuffix [ i + 1 ] , arr [ i ] ) ; minSuffix [ i ] = Math . Min ( minSuffix [ i + 1 ] , arr [ i ] ) ; } int maxPrefix = arr [ 0 ] ; int minPrefix = arr [ 0 ] ; int minDiff = maxSuffix [ K ] - minSuffix [ K ] ; for ( int i = 1 ; i < N ; ++ i ) { if ( i + K <= N ) { int maximum = Math . Max ( maxSuffix [ i + K ] , maxPrefix ) ; int minimum = Math . Min ( minSuffix [ i + K ] , minPrefix ) ; minDiff = Math . Min ( minDiff , maximum - minimum ) ; } maxPrefix = Math . Max ( maxPrefix , arr [ i ] ) ; minPrefix = Math . Min ( minPrefix , arr [ i ] ) ; } Console . WriteLine ( minDiff ) ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 4 , 5 , 8 , 9 , 1 , 2 } ; int K = 2 ; minimiseDifference ( arr , K ) ; } }
Longest substring where all the characters appear at least K times | Set 3 | C # program to implement the above approach ; Function to find the length of the longest substring ; Store the required answer ; Create a frequency map of the characters of the string ; Store the length of the string ; Traverse the string , s ; Increment the frequency of the current character by 1 ; Stores count of unique characters ; Find the number of unique characters in string ; Iterate in range [ 1 , unique ] ; Initialize frequency of all characters as 0 ; Stores the start and the end of the window ; Stores the current number of unique characters and characters occuring atleast K times ; New unique character ; New character which occurs atleast k times ; Expand window by incrementing end by 1 ; Check if this character is present atleast k times ; Check if this character is unique ; Shrink the window by incrementing start by 1 ; If there are curr_unique characters and each character is atleast k times ; Update the overall maximum length ; Print the answer ; Driver Code
using System ; class GFG { static void longestSubstring ( string s , int k ) { int ans = 0 ; int [ ] freq = new int [ 26 ] ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) freq [ s [ i ] - ' a ' ] ++ ; int unique = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) if ( freq [ i ] != 0 ) unique ++ ; for ( int curr_unique = 1 ; curr_unique <= unique ; curr_unique ++ ) { for ( int i = 0 ; i < freq . Length ; i ++ ) { freq [ i ] = 0 ; } int start = 0 , end = 0 ; int cnt = 0 , count_k = 0 ; while ( end < n ) { if ( cnt <= curr_unique ) { int ind = s [ end ] - ' a ' ; if ( freq [ ind ] == 0 ) cnt ++ ; freq [ ind ] ++ ; if ( freq [ ind ] == k ) count_k ++ ; end ++ ; } else { int ind = s [ start ] - ' a ' ; if ( freq [ ind ] == k ) count_k -- ; freq [ ind ] -- ; if ( freq [ ind ] == 0 ) cnt -- ; start ++ ; } if ( cnt == curr_unique && count_k == curr_unique ) ans = Math . Max ( ans , end - start ) ; } } Console . Write ( ans ) ; } public static void Main ( ) { string S = " aabbba " ; int K = 3 ; longestSubstring ( S , K ) ; } }
Print matrix elements using DFS traversal | C # program to implement the above approach ; Direction vectors ; Function to check if current position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited or not ; Utility function to print matrix elements using DFS Traversal ; Mark the current cell visited ; Print the element at the cell ; Traverse all four adjacent cells of the current element ; Check if x and y is valid index or not ; Function to print the matrix elements ; Initialize a visiting matrix ; Function call to print matrix elements by DFS traversal ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix
using System ; public class GFG { static int [ ] dRow = { - 1 , 0 , 1 , 0 } ; static int [ ] dCol = { 0 , 1 , 0 , - 1 } ; static bool isValid ( bool [ , ] vis , int row , int col , int COL , int ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row , col ] == true ) return false ; return true ; } static void DFSUtil ( int row , int col , int [ , ] grid , bool [ , ] vis , int M , int N ) { vis [ row , col ] = true ; Console . Write ( grid [ row , col ] + " ▁ " ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = row + dRow [ i ] ; int y = col + dCol [ i ] ; if ( isValid ( vis , x , y , M , N ) ) DFSUtil ( x , y , grid , vis , M , N ) ; } } static void DFS ( int row , int col , int [ , ] grid , int M , int N ) { bool [ , ] vis = new bool [ M + 1 , N + 1 ] ; for ( int i = 0 ; i < M + 1 ; i ++ ) { for ( int j = 0 ; j < N + 1 ; j ++ ) { vis [ i , j ] = false ; } } DFSUtil ( 0 , 0 , grid , vis , M , N ) ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int M = grid . GetLength ( 0 ) ; int N = grid . GetLength ( 1 ) ; DFS ( 0 , 0 , grid , M , N ) ; } }
Print matrix elements using DFS traversal | C # program for the above approach ; Direction vectors ; Function to check if curruent position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited ; Function to print the matrix elements ; Stores if a position in the matrix been visited or not ; Initialize stack to implement DFS ; Push the first position of grid [ ] [ ] in the stack ; Mark the cell ( 0 , 0 ) visited ; Stores top element of stack ; Delete the top ( ) element of stack ; Print element at the cell ; Traverse in all four adjacent sides of current positions ; Check if x and y is valid position and then push the position of current cell in the stack ; Push the current cell ; Mark current cell visited ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix
using System ; using System . Collections . Generic ; class GFG { static int [ ] dRow = { - 1 , 0 , 1 , 0 } ; static int [ ] dCol = { 0 , 1 , 0 , - 1 } ; static List < List < bool > > vis ; static bool isValid ( int row , int col , int COL , int ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row ] [ col ] == true ) return false ; return true ; } static void DFS_iterative ( int [ , ] grid , int M , int N ) { vis = new List < List < bool > > ( ) ; for ( int i = 0 ; i < M + 5 ; i ++ ) { vis . Add ( new List < bool > ( ) ) ; for ( int j = 0 ; j < N + 5 ; j ++ ) { vis [ i ] . Add ( false ) ; } } List < Tuple < int , int > > st = new List < Tuple < int , int > > ( ) ; st . Add ( new Tuple < int , int > ( 0 , 0 ) ) ; vis [ 0 ] [ 0 ] = true ; while ( st . Count > 0 ) { Tuple < int , int > p = st [ st . Count - 1 ] ; st . RemoveAt ( st . Count - 1 ) ; int row = p . Item1 ; int col = p . Item2 ; Console . Write ( grid [ row , col ] + " ▁ " ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = row + dRow [ i ] ; int y = col + dCol [ i ] ; if ( isValid ( x , y , M , N ) ) { st . Add ( new Tuple < int , int > ( x , y ) ) ; vis [ x ] [ y ] = true ; } } } } static void Main ( ) { int [ , ] grid = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int M = 4 ; int N = 4 ; DFS_iterative ( grid , M , N ) ; } }
Segregate 1 s and 0 s in separate halves of a Binary String | C # program for the above approach ; Function to count the minimum number of operations required to segregate all 1 s and 0 s in a binary string ; Stores the count of unequal pair of adjacent characters ; If an unequal pair of adjacent characters occurs ; For odd count ; For even count ; Driver Code ; Given string ; Length of the string ; Prints the minimum count of operations required
using System ; public class GFG { static void minOps ( String s , int N ) { int ans = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( s [ i ] != s [ i - 1 ] ) { ans ++ ; } } if ( ans % 2 == 1 ) { Console . Write ( ( ans - 1 ) / 2 ) ; return ; } Console . Write ( ans / 2 ) ; } public static void Main ( String [ ] args ) { String str = "01011100" ; int N = str . Length ; minOps ( str , N ) ; } }
Count prime triplets upto N having sum of first two elements equal to the third number | C # Program to implement the above approach ; Boolean array to mark prime numbers ; To count the prime triplets having the sum of the first two numbers equal to the third element ; Function to count prime triplets having sum of the first two elements equal to the third element ; Sieve of Eratosthenes ; Checks for the prime numbers having difference equal to2 ; Update count of triplets ; Print the answer ; Driver Code
using System ; class GFG { static int MAX = 1000001 ; static bool [ ] prime = new bool [ MAX ] ; static int [ ] cntTriplet = new int [ MAX ] ; static void primeTriplet ( int N ) { for ( int i = 0 ; i <= N ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j <= N ; j += i ) { prime [ j ] = false ; } } } for ( int i = 3 ; i <= N ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) { cntTriplet [ i ] = cntTriplet [ i - 1 ] + 1 ; } else { cntTriplet [ i ] = cntTriplet [ i - 1 ] ; } } Console . WriteLine ( cntTriplet [ N ] ) ; } public static void Main ( String [ ] args ) { int N = 7 ; primeTriplet ( N ) ; } }
Find index of the element differing in parity with all other array elements | C # program for the above approach ; Function to print the array element which differs in parity with the remaining array elements ; Multimaps to store even and odd numbers along with their indices ; Traverse the array ; If array element is even ; Otherwise ; If only one even element is present in the array ; If only one odd element is present in the array ; Driver Code ; Given array ; Size of the array
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void OddOneOut ( int [ ] arr , int N ) { Dictionary < int , int > e = new Dictionary < int , int > ( ) ; Dictionary < int , int > o = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { e [ arr [ i ] ] = i ; } else { o [ arr [ i ] ] = i ; } } if ( e . Count == 1 ) { Console . Write ( e . First ( ) . Value ) ; } else { Console . Write ( o . First ( ) . Value ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 7 , 8 , 10 } ; int N = arr . Length ; OddOneOut ( arr , N ) ; } }
Count indices where the maximum in the prefix array is less than that in the suffix array | C # program for the above approach ; Function to print the count of indices in which the maximum in prefix arrays is less than that in the suffix array ; If size of array is 1 ; pre [ ] : Prefix array suf [ ] : Suffix array ; Stores the required count ; Find the maximum in prefix array ; Find the maximum in suffix array ; Traverse the array ; If maximum in prefix array is less than maximum in the suffix array ; Print the answer ; Driver code ; Function Call
using System ; class GFG { static void count ( int [ ] a , int n ) { if ( n == 1 ) { Console . Write ( 0 ) ; return ; } int [ ] pre = new int [ n - 1 ] ; int [ ] suf = new int [ n - 1 ] ; int max = a [ 0 ] ; int ans = 0 , i ; pre [ 0 ] = a [ 0 ] ; for ( i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; pre [ i ] = max ; } max = a [ n - 1 ] ; suf [ n - 2 ] = a [ n - 1 ] ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( a [ i ] > max ) max = a [ i ] ; suf [ i - 1 ] = max ; } for ( i = 0 ; i < n - 1 ; i ++ ) { if ( pre [ i ] < suf [ i ] ) ans ++ ; } Console . Write ( ans ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 8 , 1 , 4 } ; int N = arr . Length ; count ( arr , N ) ; } }
Length of the longest subsequence such that XOR of adjacent elements is equal to K | C # program for above approach ; Function to find maximum length of subsequence ; Stores maximum length of subsequence ; Dictionary to store the longest length of subsequence ending at an integer , say X ; Stores the maximum length of subsequence ending at index i ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Retrieve the longest length of subsequence ending at integer [ ] a ^ K ; If dpj is not NULL ; Update dp [ i ] ; Update ans ; Update the maximum length of subsequence ending at element is a [ i ] in Dictionary ; Return the ans if ans >= 2. Otherwise , return 0 ; Driver Code ; Input ; Print the length of the longest subsequence
using System ; using System . Collections . Generic ; public class GFG { public static int xorSubsequence ( int [ ] a , int n , int k ) { int ans = 0 ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int [ ] dp = new int [ n ] ; map . Add ( a [ 0 ] , 1 ) ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int dpj = map . ContainsKey ( a [ i ] ^ k ) ? map [ a [ i ] ^ k ] : - 1 ; if ( dpj != 0 ) dp [ i ] = Math . Max ( dp [ i ] , dpj + 1 ) ; ans = Math . Max ( ans , dp [ i ] ) ; if ( map . ContainsKey ( a [ i ] ) ) { map [ a [ i ] ] = Math . Max ( map [ a [ i ] ] + 1 , dp [ i ] ) ; ; } else { map . Add ( a [ i ] , Math . Max ( 1 , dp [ i ] ) ) ; } } return ans >= 2 ? ans : 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 4 , 3 , 5 } ; int N = arr . Length ; int K = 1 ; Console . WriteLine ( xorSubsequence ( arr , N , K ) ) ; } }