codeparrot/xlcost-text-to-code huggingface dataset:
Non | Efficient C # program to print all non - repeating elements . ; Insert all array elements in hash table ; Traverse through map only and using for - each loop for iteration over Map . entrySet ( ) ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void firstNonRepeating ( int [ ] arr , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > x in m ) { if ( x . Value == 1 ) { Console . Write ( x . Key + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . Length ; firstNonRepeating ( arr , n ) ; } }
Minimum substring removals required to make all remaining characters of a string same | C # program for the above approach ; Function to count minimum operations required to make all characters equal by repeatedly removing subString ; Remove consecutive duplicate characters from str ; Stores length of the String ; Stores frequency of each distinct characters of the String str ; Iterate over all the characters of the String str ; Update frequency of str [ i ] ; Decrementing the frequency of the String str [ 0 ] ; Decrementing the frequency of the String str [ N - 1 ] ; Stores the required count ; Iterate over all characters of the String str ; Update ans ; Driver Code ; Given String
using System ; class GFG { static void minOperationNeeded ( char [ ] str ) { str = modstring ( str ) ; int N = str . Length ; int [ ] res = new int [ 256 ] ; for ( int i = 0 ; i < N ; ++ i ) { res [ str [ i ] ] += 1 ; } res [ str [ 0 ] ] -= 1 ; res [ str [ N - 1 ] ] -= 1 ; int ans = Int32 . MaxValue ; for ( int i = 0 ; i < N ; ++ i ) { ans = Math . Min ( ans , res [ str [ i ] ] ) ; } Console . WriteLine ( ( ans + 1 ) + " STRNEWLINE " ) ; } private static char [ ] modstring ( char [ ] str ) { string s = " " ; bool b = true ; for ( int i = 1 ; i < str . Length ; ++ i ) { if ( str [ i - 1 ] != str [ i ] ) b = true ; if ( b ) { s += str [ i - 1 ] ; b = false ; } } return s . ToCharArray ( ) ; } public static void Main ( ) { string str = " ABCDABCDABCDA " ; minOperationNeeded ( str . ToCharArray ( ) ) ; } }
Print all root to leaf paths of an N |  ; Structure of an N ary tree node ; Parameterized Constructor ; Function to print the root to leaf path of the given N - ary Tree ; Print elements in the vector ; Utility function to print all root to leaf paths of an Nary Tree ; If root is null ; Insert current node 's  data into the vector ; If current node is a leaf node ; Print the path ; Pop the leaf node and return ; Recur for all children of the current node ; Recursive Function Call ; Function to print root to leaf path ; If root is null , return ; Stores the root to leaf path ; Utility function call ; Driver Code ; Given N - Ary tree ; Function Call
using System ; using System . Collections . Generic ; public class Node { public int data ; public List < Node > child ; public Node ( int x ) { this . data = x ; this . child = new List < Node > ( ) ; } } public class GFG { static void printPath ( List < int > vec ) { foreach ( int ele in vec ) { Console . Write ( ele + " ▁ " ) ; } Console . WriteLine ( ) ; } static void printAllRootToLeafPaths ( Node root , List < int > vec ) { if ( root == null ) return ; vec . Add ( root . data ) ; if ( root . child . Count == 0 ) { printPath ( vec ) ; vec . RemoveAt ( vec . Count - 1 ) ; return ; } for ( int i = 0 ; i < root . child . Count ; i ++ ) { printAllRootToLeafPaths ( root . child [ i ] , vec ) ; } vec . RemoveAt ( vec . Count - 1 ) ; } static void printAllRootToLeafPaths ( Node root ) { if ( root == null ) return ; List < int > vec = new List < int > ( ) ; printAllRootToLeafPaths ( root , vec ) ; } static public void Main ( ) { Node root = new Node ( 1 ) ; ( root . child ) . Add ( new Node ( 2 ) ) ; ( root . child ) . Add ( new Node ( 3 ) ) ; ( root . child [ 0 ] . child ) . Add ( new Node ( 4 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 5 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 6 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . Add ( new Node ( 7 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . Add ( new Node ( 8 ) ) ; printAllRootToLeafPaths ( root ) ; } }
Minimum removals to make a string concatenation of a substring of 0 s followed by a substring of 1 s | C # program for the above approach ; Function to count minimum removals required to make a given string concatenation of substring of 0 s followed by substring of 1 s ; Stores the length of the string ; Precompute the count of 0 s ; Check for the last character ; Traverse and update zeroCount array ; If current character is 0 , ; Update aCount [ i ] as aCount [ i + 1 ] + 1 ; Update as aCount [ i + 1 ] ; Keeps track of deleted 1 s ; Stores the count of removals ; Traverse the array ; If current character is 1 ; Update ans ; If all 1 s are deleted ; Return the minimum number of deletions ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int minimumDeletions ( String s ) { int n = s . Length ; int [ ] zeroCount = new int [ n ] ; zeroCount [ n - 1 ] = ( s [ n - 1 ] == '0' ) ? 1 : 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) zeroCount [ i ] = ( s [ i ] == '0' ) ? zeroCount [ i + 1 ] + 1 : zeroCount [ i + 1 ] ; int oneCount = 0 ; int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { ans = Math . Min ( ans , oneCount + zeroCount [ i ] ) ; oneCount ++ ; } } ans = Math . Min ( ans , oneCount ) ; return ( ans == int . MaxValue ) ? 0 : ans ; } public static void Main ( String [ ] args ) { String str = "00101101" ; Console . WriteLine ( minimumDeletions ( str ) ) ; } }
Pairs of Positive Negative values in an array | C # program to find pairs of positive and negative values present in an array . ; Print pair with negative and positive value ; For each element of array . ; Try to find the negative value of arr [ i ] from i + 1 to n ; If absolute values are equal print pair . ; If size of vector is 0 , therefore there is no element with positive negative value , print "0" ; Sort the vector ; Print the pair with negative positive value . ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static void printPairs ( int [ ] arr , int n ) { List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . Abs ( arr [ i ] ) == Math . Abs ( arr [ j ] ) ) v . Add ( Math . Abs ( arr [ i ] ) ) ; if ( v . Count == 0 ) return ; v . Sort ( ) ; for ( int i = 0 ; i < v . Count ; i ++ ) Console . Write ( - v [ i ] + " ▁ " + v [ i ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . Length ; printPairs ( arr , n ) ; } }
Check if an array can be divided into pairs whose sum is divisible by k | C # program to check if arr [ 0. . n - 1 ] can be divided in pairs such that every pair is divisible by k . ; Returns true if arr [ 0. . n - 1 ] can be divided into pairs with sum divisible by k . ; An odd length array cannot be divided into pairs ; Create a frequency array to count occurrences of all remainders when divided by k . ; Count occurrences of all remainders ; Traverse input array and use freq [ ] to decide if given array can be divided in pairs ; Remainder of current element ; If remainder with current element divides k into two halves . ; Then there must be even occurrences of such remainder ; If remainder is 0 , then there must be two elements with 0 remainder ; Then there must be even occurrences of such remainder ; Else number of occurrences of remainder must be equal to number of occurrences of k - remainder ; Driver code ; Function call
using System . Collections . Generic ; using System ; class GFG { static bool canPairs ( int [ ] ar , int k ) { if ( ar . Length % 2 == 1 ) return false ; Dictionary < Double , int > hm = new Dictionary < Double , int > ( ) ; for ( int i = 0 ; i < ar . Length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( ! hm . ContainsKey ( rem ) ) { hm [ rem ] = 0 ; } hm [ rem ] ++ ; } for ( int i = 0 ; i < ar . Length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( 2 * rem == k ) { if ( hm [ rem ] % 2 == 1 ) return false ; } else if ( rem == 0 ) { if ( hm [ rem ] % 2 == 1 ) return false ; } else { if ( hm [ k - rem ] != hm [ rem ] ) return false ; } } return true ; } public static void Main ( ) { int [ ] arr = { 92 , 75 , 65 , 48 , 45 , 35 } ; int k = 10 ; bool ans = canPairs ( arr , k ) ; if ( ans ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }
Subarray with no pair sum divisible by K | C # Program to find the subarray with no pair sum divisible by K ; function to find the subarray with no pair sum divisible by k ; hash table to store the remainders obtained on dividing by K ; s : starting index of the current subarray , e : ending index of the current subarray , maxs : starting index of the maximum size subarray so far , maxe : ending index of the maximum size subarray so far ; insert the first element in the set ; Removing starting elements of current subarray while there is an element in set which makes a pair with mod [ i ] such that the pair sum is divisible . ; include the current element in the current subarray the ending index of the current subarray increments by one ; compare the size of the current subarray with the maximum size so far ; Driver Code
using System ; using System . Collections ; class GFG { static void subarrayDivisibleByK ( int [ ] arr , int n , int k ) { int [ ] mp = new int [ 1000 ] ; int s = 0 , e = 0 , maxs = 0 , maxe = 0 ; mp [ arr [ 0 ] % k ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { int mod = arr [ i ] % k ; while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) { mp [ arr [ s ] % k ] -- ; s ++ ; } mp [ mod ] ++ ; e ++ ; if ( ( e - s ) > ( maxe - maxs ) ) { maxe = e ; maxs = s ; } } Console . Write ( " The ▁ maximum ▁ size ▁ is ▁ " + ( maxe - maxs + 1 ) + " ▁ and ▁ the ▁ subarray ▁ is ▁ as ▁ follows STRNEWLINE " ) ; for ( int i = maxs ; i <= maxe ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int k = 3 ; int [ ] arr = { 5 , 10 , 15 , 20 , 25 } ; int n = arr . Length ; subarrayDivisibleByK ( arr , n , k ) ; } }
Find three element from different three arrays such that a + b + c = sum | C # program to find three element from different three arrays such that a + b + c is equal to given sum ; Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Driver Code
using System ; public class GFG { static bool findTriplet ( int [ ] a1 , int [ ] a2 , int [ ] a3 , int n1 , int n2 , int n3 , int sum ) { for ( int i = 0 ; i < n1 ; i ++ ) for ( int j = 0 ; j < n2 ; j ++ ) for ( int k = 0 ; k < n3 ; k ++ ) if ( a1 [ i ] + a2 [ j ] + a3 [ k ] == sum ) return true ; return false ; } static public void Main ( ) { int [ ] a1 = { 1 , 2 , 3 , 4 , 5 } ; int [ ] a2 = { 2 , 3 , 6 , 1 , 2 } ; int [ ] a3 = { 3 , 2 , 4 , 5 , 6 } ; int sum = 9 ; int n1 = a1 . Length ; int n2 = a2 . Length ; int n3 = a3 . Length ; if ( findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find three element from different three arrays such that a + b + c = sum | C # program to find three element from different three arrays such that a + b + c is equal to given sum ; Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Store elements of first array in hash ; sum last two arrays element one by one ; Consider current pair and find if there is an element in a1 [ ] such that these three form a required triplet ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool findTriplet ( int [ ] a1 , int [ ] a2 , int [ ] a3 , int n1 , int n2 , int n3 , int sum ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) { s . Add ( a1 [ i ] ) ; } List < int > al = new List < int > ( s ) ; for ( int i = 0 ; i < n2 ; i ++ ) { for ( int j = 0 ; j < n3 ; j ++ ) { if ( al . Contains ( sum - a2 [ i ] - a3 [ j ] ) & al . IndexOf ( sum - a2 [ i ] - a3 [ j ] ) != al [ al . Count - 1 ] ) { return true ; } } } return false ; } public static void Main ( String [ ] args ) { int [ ] a1 = { 1 , 2 , 3 , 4 , 5 } ; int [ ] a2 = { 2 , 3 , 6 , 1 , 2 } ; int [ ] a3 = { 3 , 2 , 4 , 5 , 6 } ; int sum = 9 ; int n1 = a1 . Length ; int n2 = a2 . Length ; int n3 = a3 . Length ; if ( findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Count quadruplets with sum K from given array | C # program for the above approach ; Function to return the number of quadruplets having given sum ; Initialize variables ; Initialize answer ; All possible first elements ; All possible second element ; Use map to find the fourth element ; All possible third elements ; Calculate number of valid 4 th elements ; Update the twice_count ; Unordered pairs ; Return answer ; Driver Code ; Given array [ ] arr ; Given sum S ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int countSum ( int [ ] a , int n , int sum ) { int i , j , k ; int l ; int count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { int req = sum - a [ i ] - a [ j ] ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( k = j + 1 ; k < n ; k ++ ) if ( m . ContainsKey ( a [ k ] ) ) { m [ a [ k ] ] ++ ; } else { m . Add ( a [ k ] , 1 ) ; } int twice_count = 0 ; for ( k = j + 1 ; k < n ; k ++ ) { if ( m . ContainsKey ( req - a [ k ] ) ) twice_count += m [ req - a [ k ] ] ; if ( req - a [ k ] == a [ k ] ) twice_count -- ; } count += twice_count / 2 ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = arr . Length ; Console . Write ( countSum ( arr , N , S ) ) ; } }
Find four elements a , b , c and d in an array such that a + b = c + d | C # Program to find four different elements a , b , c and d of array such that a + b = c + d ; Class to represent a pair ; function to find a , b , c , d such that ( a + b ) = ( c + d ) ; Create an empty Hash to store mapping from sum to pair indexes ; Traverse through all possible pairs of arr [ ] ; If sum of current pair is not in hash , then store it and continue to next pair ; Testing program
using System ; using System . Collections . Generic ; public class ArrayElements { public class pair { private readonly ArrayElements outerInstance ; public int first , second ; public pair ( ArrayElements outerInstance , int f , int s ) { this . outerInstance = outerInstance ; first = f ; second = s ; } } public virtual bool findPairs ( int [ ] arr ) { Dictionary < int , pair > map = new Dictionary < int , pair > ( ) ; int n = arr . Length ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( ! map . ContainsKey ( sum ) ) { map [ sum ] = new pair ( this , i , j ) ; } else { pair p = map [ sum ] ; Console . WriteLine ( " ( " + arr [ p . first ] + " , ▁ " + arr [ p . second ] + " ) ▁ and ▁ ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; return true ; } } } return false ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 3 , 4 , 7 , 1 , 2 , 9 , 8 } ; ArrayElements a = new ArrayElements ( ) ; a . findPairs ( arr ) ; } }
Check if all subarrays contains at least one unique element | C # program for the above approach ; Function to check if all subarrays have at least one unique element ; Generate all subarray ; Store frequency of subarray 's elements ; Traverse the array over the range [ i , N ] ; Update frequency of current subarray in map ; Increment count ; Decrement count ; If all subarrays have at least 1 unique element ; Driver Code ; Given array [ ] arr ; Function Call
using System ; using System . Collections . Generic ; class GFG { static String check ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; int count = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( hm . ContainsKey ( ( arr [ j ] ) ) ) hm [ arr [ j ] ] ++ ; else hm . Add ( arr [ j ] , 1 ) ; if ( hm [ arr [ j ] ] == 1 ) count ++ ; if ( hm [ arr [ j ] ] == 2 ) count -- ; if ( count == 0 ) return " No " ; } } return " Yes " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 } ; int N = arr . Length ; Console . WriteLine ( check ( arr , N ) ) ; } }
Count all disjoint pairs having absolute difference at least K from a given array | C # program for the above approach ; Function to count distinct pairs with absolute difference atleast K ; Track the element that have been paired ; Stores count of distinct pairs ; Pick all elements one by one ; If already visited ; If already visited ; If difference is at least K ; Mark element as visited and increment the count ; Print the final count ; Driver Code ; Given arr [ ] ; Size of array ; Given difference K ; Function Call
using System ; class GFG { static void countPairsWithDiffK ( int [ ] arr , int N , int K ) { int [ ] vis = new int [ N ] ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( vis [ i ] == 1 ) continue ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( vis [ j ] == 1 ) continue ; if ( Math . Abs ( arr [ i ] - arr [ j ] ) >= K ) { count ++ ; vis [ i ] = 1 ; vis [ j ] = 1 ; break ; } } } Console . Write ( count ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 3 , 5 } ; int N = arr . Length ; int K = 2 ; countPairsWithDiffK ( arr , N , K ) ; } }
Check if two trees are Mirror | c # program to see if two trees are mirror of each other ; A binary tree node ; Given two trees , return true if they are mirror of each other ; Base case : Both empty ; If only one is empty ; Both non - empty , compare them recursively Note that in recursive calls , we pass left of one tree and right of other tree ; Driver code to test above methods
using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } public class BinaryTree { public Node a , b ; public virtual bool areMirror ( Node a , Node b ) { if ( a == null && b == null ) { return true ; } if ( a == null b == null ) { return false ; } return a . data == b . data && areMirror ( a . left , b . right ) && areMirror ( a . right , b . left ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node a = new Node ( 1 ) ; Node b = new Node ( 1 ) ; a . left = new Node ( 2 ) ; a . right = new Node ( 3 ) ; a . left . left = new Node ( 4 ) ; a . left . right = new Node ( 5 ) ; b . left = new Node ( 3 ) ; b . right = new Node ( 2 ) ; b . right . left = new Node ( 5 ) ; b . right . right = new Node ( 4 ) ; if ( tree . areMirror ( a , b ) == true ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Find the length of largest subarray with 0 sum | C # program to find maximum length subarray with 0 sum ; Returns length of the maximum length subarray with 0 sum ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; Look this sum in hash table ; Else put this sum in hash table ; Driver code
using System ; using System . Collections . Generic ; public class MaxLenZeroSumSub { static int maxLen ( int [ ] arr ) { Dictionary < int , int > hM = new Dictionary < int , int > ( ) ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < arr . GetLength ( 0 ) ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ; if ( sum == 0 ) max_len = i + 1 ; int prev_i = 0 ; if ( hM . ContainsKey ( sum ) ) { prev_i = hM [ sum ] ; } if ( hM . ContainsKey ( sum ) ) max_len = Math . Max ( max_len , i - prev_i ) ; else { if ( hM . ContainsKey ( sum ) ) hM . Remove ( sum ) ; hM . Add ( sum , i ) ; } } return max_len ; } public static void Main ( ) { int [ ] arr = { 15 , - 2 , 2 , - 8 , 1 , 7 , 10 , 23 } ; Console . WriteLine ( " Length ▁ of ▁ the ▁ longest ▁ 0 ▁ sum ▁ subarray ▁ is ▁ " + maxLen ( arr ) ) ; } }
Print alternate elements of an array | C # program to implement the above approach ; Function to print Alternate elements of the given array ; Print elements at odd positions ; Print elements of array ; Driver Code
using System ; class GFG { static void printAlter ( int [ ] arr , int N ) { for ( int currIndex = 0 ; currIndex < N ; currIndex += 2 ) { Console . Write ( arr [ currIndex ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; printAlter ( arr , N ) ; } }
Longest Increasing consecutive subsequence | C # program to find length of the longest increasing subsequence whose adjacent element differ by 1 ; function that returns the length of the longest increasing subsequence whose adjacent element differ by 1 ; Create hashmap to save latest consequent number as " key " and its length as " value " ; Put first element as " key " and its length as " value " ; iterate for all element ; Check if last consequent of arr [ i ] exist or not ; put the updated consequent number and increment its value ( length ) ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int longIncrConseqSubseq ( int [ ] arr , int n ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; map . Add ( arr [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( map . ContainsKey ( arr [ i ] - 1 ) ) { map . Add ( arr [ i ] , map [ arr [ i ] - 1 ] + 1 ) ; map . Remove ( arr [ i ] - 1 ) ; } else { if ( ! map . ContainsKey ( arr [ i ] ) ) map . Add ( arr [ i ] , 1 ) ; } } int max = int . MinValue ; foreach ( KeyValuePair < int , int > entry in map ) { if ( entry . Value > max ) { max = entry . Value ; } } return max ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 } ; int n = arr . Length ; Console . WriteLine ( longIncrConseqSubseq ( arr , n ) ) ; } }
Longest subsequence such that difference between adjacents is one | Set 2 | C # implementation to find longest subsequence such that difference between adjacents is one ; function to find longest subsequence such that difference between adjacents is one ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last element of that subsequence ; to store the longest length subsequence ; traverse the array elements ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' um ' and its length of subsequence is greater than ' len ' ; if ' arr [ i ] + 1' is in ' um ' and its length of subsequence is greater than ' len ' ; update arr [ i ] subsequence length in ' um ' ; update longest length ; required longest length subsequence ; Driver program to test above
using System ; using System . Collections . Generic ; class GFG { static int longLenSub ( int [ ] arr , int n ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( um . ContainsKey ( arr [ i ] - 1 ) && len < um [ arr [ i ] - 1 ] ) len = um [ arr [ i ] - 1 ] ; if ( um . ContainsKey ( arr [ i ] + 1 ) && len < um [ arr [ i ] + 1 ] ) len = um [ arr [ i ] + 1 ] ; um [ arr [ i ] ] = len + 1 ; if ( longLen < um [ arr [ i ] ] ) longLen = um [ arr [ i ] ] ; } return longLen ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . Length ; Console . Write ( " Longest ▁ length ▁ subsequence ▁ = ▁ " + longLenSub ( arr , n ) ) ; } }