codeparrot/xlcost-text-to-code huggingface dataset:
Count of N digit Numbers having no pair of equal consecutive Digits | C # program to implement of the above approach ; Iterative Function to calculate ( x ^ y ) % mod in O ( log y ) ; Initialize result ; Update x if x >= mod ; If x is divisible by mod ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Driver Code
using System ; class GFG { static int power ( int x , int y , int mod ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } public static void count ( int N ) { if ( N == 1 ) { Console . WriteLine ( 10 ) ; return ; } Console . WriteLine ( power ( 9 , N , 1000000007 ) ) ; } public static void Main ( String [ ] args ) { int N = 3 ; count ( N ) ; } }
Smallest String consisting of a String S exactly K times as a Substring | C # program to implement the above approach ; KMP algorithm ; Function to return the required string ; Finding the longest proper prefix which is also suffix ; ans string ; Update ans appending the substring K - 1 times ; Append the original string ; Returning min length string which contain exactly k substring of given string ; Driver code
using System ; class GFG { static int [ ] kmp ( string s ) { int n = s . Length ; int [ ] lps = new int [ n ] ; lps [ 0 ] = 0 ; int i = 1 , len = 0 ; while ( i < n ) { if ( s [ i ] == s [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } return lps ; } static string findString ( string s , int k ) { int n = s . Length ; int [ ] lps = kmp ( s ) ; string ans = " " ; string suff = s . Substring ( 0 , n - lps [ n - 1 ] ) ; for ( int i = 0 ; i < k - 1 ; ++ i ) { ans += suff ; } ans += s ; return ans ; } public static void Main ( string [ ] args ) { int k = 3 ; string s = " geeksforgeeks " ; Console . Write ( findString ( s , k ) ) ; } }
Check if an Array can be Sorted by picking only the corner Array elements | C # program to implement the above approach ; Function to check if an array can be sorted using given operations ; If sequence becomes increasing after an already non - decreasing to non - increasing pattern ; If a decreasing pattern is observed ; Driver Code
using System ; class GFG { static bool check ( int [ ] arr , int n ) { int i , g ; g = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 0 && g == 1 ) return false ; if ( arr [ i ] - arr [ i - 1 ] < 0 ) g = 1 ; } return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 10 , 4 , 3 , 1 } ; int n = arr . Length ; if ( check ( arr , n ) == true ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Queries to find the count of connected Non | C # program to implement the above approach ; Count of connected cells ; Function to return the representative of the Set to which x belongs ; If x is parent of itself ; x is representative of the Set ; Otherwise ; Path Compression ; Unites the set that includes x and the set that includes y ; Find the representatives ( or the root nodes ) for x an y ; If both are in the same set ; Decrement count ; If x ' s ▁ rank ▁ is ▁ less ▁ than ▁ y ' s rank ; Otherwise ; Then move x under y ( doesn 't matter which one goes where) ; And increment the result tree 's  rank by 1 ; Function to count the number of connected cells in the matrix ; Store result for queries ; Store representative of each element ; Initially , all elements are in their own set ; Stores the rank ( depth ) of each node ; If the grid [ x * m + y ] is already set , store the result ; Set grid [ x * m + y ] to 1 ; Increment count . ; Check for all adjacent cells to do a Union with neighbour 's  set if neighbour is also 1 ; Store result . ; Driver Code
using System ; class GFG { static int ctr = 0 ; static int find ( int [ ] parent , int x ) { if ( parent [ x ] == x ) return x ; parent [ x ] = find ( parent , parent [ x ] ) ; return parent [ x ] ; } static void setUnion ( int [ ] parent , int [ ] rank , int x , int y ) { int parentx = find ( parent , x ) ; int parenty = find ( parent , y ) ; if ( parenty == parentx ) return ; ctr -- ; if ( rank [ parentx ] < rank [ parenty ] ) { parent [ parentx ] = parenty ; } else if ( rank [ parentx ] > rank [ parenty ] ) { parent [ parenty ] = parentx ; } else { parent [ parentx ] = parenty ; rank [ parenty ] ++ ; } } static int [ ] solve ( int n , int m , int [ , ] query ) { int [ ] result = new int [ query . Length ] ; int [ ] parent = new int [ n * m ] ; for ( int i = 0 ; i < n * m ; i ++ ) parent [ i ] = i ; int [ ] rank = new int [ n * m ] ; for ( int i = 0 ; i < rank . Length ; i ++ ) rank [ i ] = 1 ; bool [ ] grid = new bool [ n * m ] ; for ( int i = 0 ; i < query . GetLength ( 0 ) ; i ++ ) { int x = query [ i , 0 ] ; int y = query [ i , 1 ] ; if ( grid [ m * x + y ] == true ) { result [ i ] = ctr ; continue ; } grid [ m * x + y ] = true ; ctr ++ ; if ( x > 0 && grid [ m * ( x - 1 ) + y ] == true ) setUnion ( parent , rank , m * x + y , m * ( x - 1 ) + y ) ; if ( y > 0 && grid [ m * ( x ) + y - 1 ] == true ) setUnion ( parent , rank , m * x + y , m * ( x ) + y - 1 ) ; if ( x < n - 1 && grid [ m * ( x + 1 ) + y ] == true ) setUnion ( parent , rank , m * x + y , m * ( x + 1 ) + y ) ; if ( y < m - 1 && grid [ m * ( x ) + y + 1 ] == true ) setUnion ( parent , rank , m * x + y , m * ( x ) + y + 1 ) ; result [ i ] = ctr ; } return result ; } public static void Main ( String [ ] args ) { int N = 3 , M = 3 , K = 4 ; int [ , ] query = { { 0 , 0 } , { 1 , 1 } , { 1 , 0 } , { 1 , 2 } } ; int [ ] result = solve ( N , M , query ) ; for ( int i = 0 ; i < K ; i ++ ) Console . Write ( result [ i ] + " ▁ " ) ; } }
Minimum Sum of a pair at least K distance apart from an Array | C # program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Find the suffix array ; Iterate in the array ; Update minimum sum ; Print the answer ; Driver Code
using System ; class GFG { public static void findMinSum ( int [ ] A , int K ) { int n = A . Length ; int [ ] suffix_min = new int [ n ] ; suffix_min [ n - 1 ] = A [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_min [ i ] = Math . Min ( suffix_min [ i + 1 ] , A [ i ] ) ; int min_sum = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + K < n ) min_sum = Math . Min ( min_sum , A [ i ] + suffix_min [ i + K ] ) ; } Console . WriteLine ( min_sum ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 2 ; findMinSum ( A , K ) ; } }
Maximize difference between the Sum of the two halves of the Array after removal of N elements | C # program to implement the above approach ; Function to print the maximum difference possible between the two halves of the array ; Stores n maximum values from the start ; Insert first n elements ; Update sum of largest n elements from left ; For the remaining elements ; Obtain minimum value in the set ; Insert only if it is greater than minimum value ; Update sum from left ; Remove the minimum ; Insert the current element ; Clear the set ; Store n minimum elements from the end ; Insert the last n elements ; Update sum of smallest n elements from right ; For the remaining elements ; Obtain the minimum ; Insert only if it is smaller than maximum value ; Update sum from right ; Remove the minimum ; Insert the new element ; Compare the difference and store the maximum ; Return the maximum possible difference ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static long FindMaxDif ( List < long > a , int m ) { int n = m / 3 ; long [ ] l = new long [ m + 5 ] ; long [ ] r = new long [ m + 5 ] ; List < long > s = new List < long > ( ) ; for ( int i = 1 ; i <= m ; i ++ ) { if ( i <= n ) { l [ i ] = a [ i - 1 ] + l [ i - 1 ] ; s . Add ( a [ i - 1 ] ) ; } else { l [ i ] = l [ i - 1 ] ; s . Sort ( ) ; long d = s [ 0 ] ; if ( a [ i - 1 ] > d ) { l [ i ] -= d ; l [ i ] += a [ i - 1 ] ; s . Remove ( d ) ; s . Add ( a [ i - 1 ] ) ; } } } s . Clear ( ) ; for ( int i = m ; i >= 1 ; i -- ) { if ( i >= m - n + 1 ) { r [ i ] = a [ i - 1 ] + r [ i + 1 ] ; s . Add ( a [ i - 1 ] ) ; } else { r [ i ] = r [ i + 1 ] ; s . Sort ( ) ; long d = s [ s . Count - 1 ] ; if ( a [ i - 1 ] < d ) { r [ i ] -= d ; r [ i ] += a [ i - 1 ] ; s . Remove ( d ) ; s . Add ( a [ i - 1 ] ) ; } } } long ans = ( long ) ( - 9e18 ) ; for ( int i = n ; i <= m - n ; i ++ ) { ans = Math . Max ( ans , l [ i ] - r [ i + 1 ] ) ; } return ans ; } static void Main ( ) { List < long > vtr = new List < long > ( new long [ ] { 3 , 1 , 4 , 1 , 5 , 9 } ) ; int n = vtr . Count ; Console . Write ( FindMaxDif ( vtr , n ) ) ; } }
Check if each element of an Array is the Sum of any two elements of another Array | C # program to implement the above approach ; Function to check if each element of [ ] B can be formed by adding two elements of array [ ] A ; Store each element of [ ] B ; Traverse all possible pairs of array ; If A [ i ] + A [ j ] is present in the set ; Remove A [ i ] + A [ j ] from the set ; If set is empty ; Otherwise ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static String checkPossible ( int [ ] A , int [ ] B , int n ) { HashSet values = new HashSet ( ) ; for ( int i = 0 ; i < n ; i ++ ) { values . Add ( B [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( values . Contains ( A [ i ] + A [ j ] ) ) { values . Remove ( A [ i ] + A [ j ] ) ; if ( values . Count == 0 ) break ; } } } if ( values . Count == 0 ) return " Yes " ; else return " No " ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] A = { 3 , 5 , 1 , 4 , 2 } ; int [ ] B = { 3 , 4 , 5 , 6 , 7 } ; Console . Write ( checkPossible ( A , B , N ) ) ; } }
Count of Array elements greater than or equal to twice the Median of K trailing Array elements | C # Program to implement the above approach ; Function to find the count of array elements >= twice the median of K trailing array elements ; Stores frequencies ; Stores the array elements ; Count the frequencies of the array elements ; Iterating from d to n - 1 index means ( d + 1 ) th element to nth element ; To check the median ; Iterate over the frequencies of the elements ; Add the frequencies ; Check if the low_median value is obtained or not , if yes then do not change as it will be minimum ; Check if the high_median value is obtained or not , if yes then do not change it as it will be maximum ; Store 2 * median of K trailing elements ; If the current >= 2 * median ; Decrease the frequency for ( k - 1 ) - th element ; Increase the frequency of the current element ; Print the count ; Driver Code
using System ; class GFG { static int N = ( int ) 2e5 ; static int V = 500 ; static void solve ( int n , int d , int [ ] input ) { int [ ] a = new int [ N ] ; int [ ] cnt = new int [ V + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) a [ i ] = input [ i ] ; int answer = 0 ; for ( int i = 0 ; i < d ; ++ i ) cnt [ a [ i ] ] ++ ; for ( int i = d ; i <= n - 1 ; ++ i ) { int acc = 0 ; int low_median = - 1 , high_median = - 1 ; for ( int v = 0 ; v <= V ; ++ v ) { acc += cnt [ v ] ; if ( low_median == - 1 && acc >= ( int ) ( Math . Floor ( ( d + 1 ) / 2.0 ) ) ) low_median = v ; if ( high_median == - 1 && acc >= ( int ) ( Math . Ceiling ( ( d + 1 ) / 2.0 ) ) ) high_median = v ; } int double_median = low_median + high_median ; if ( a [ i ] >= double_median ) answer ++ ; cnt [ a [ i - d ] ] -- ; cnt [ a [ i ] ] ++ ; } Console . Write ( answer + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] input = { 1 , 2 , 2 , 4 , 5 } ; int n = input . Length ; int k = 3 ; solve ( n , k , input ) ; } }
Smallest positive integer X satisfying the given equation | C # program to implement the above approach ; Function to find out the smallest positive integer for the equation ; Stores the minimum ; Iterate till K ; Check if n is divisible by i ; Return the answer ; Driver Code
using System ; class GFG { static int findMinSoln ( int n , int k ) { int minSoln = int . MaxValue ; for ( int i = 1 ; i < k ; i ++ ) { if ( n % i == 0 ) minSoln = Math . Min ( minSoln , ( n / i ) * k + i ) ; } return minSoln ; } public static void Main ( String [ ] args ) { int n = 4 , k = 6 ; Console . WriteLine ( findMinSoln ( n , k ) ) ; } }
Queries to find the Lower Bound of K from Prefix Sum Array with updates using Fenwick Tree | C # program to implement the above approach ; Function to calculate and return the sum of arr [ 0. . index ] ; Traverse ancestors of BITree [ index ] ; Update the sum of current element of BIT to ans ; Update index to that of the parent node in getSum ( ) view by subtracting LSB ( Least Significant Bit ) ; Function to update the Binary Index Tree by replacing all ancestors of index by their respective sum with val ; Traverse all ancestors and sum with ' val ' . ; Add ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Function to construct the Binary Indexed Tree for the given array ; Initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Function to obtain and return the index of lower_bound of k ; Store the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx ; Driver Code
using System ; class GFG { static int getSum ( int [ ] BITree , int index ) { int ans = 0 ; index += 1 ; while ( index > 0 ) { ans += BITree [ index ] ; index -= index & ( - index ) ; } return ans ; } static void updateBIT ( int [ ] BITree , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } static int [ ] constructBITree ( int [ ] arr , int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } static int getLowerBound ( int [ ] BITree , int [ ] arr , int n , int k ) { int lb = - 1 ; int l = 0 , r = n - 1 ; while ( l <= r ) { int mid = l + ( r - l ) / 2 ; if ( getSum ( BITree , mid ) >= k ) { r = mid - 1 ; lb = mid ; } else l = mid + 1 ; } return lb ; } static void performQueries ( int [ ] A , int n , int [ , ] q ) { int [ ] BITree = constructBITree ( A , n ) ; for ( int i = 0 ; i < q . GetLength ( 0 ) ; i ++ ) { int id = q [ i , 0 ] ; if ( id == 1 ) { int idx = q [ i , 1 ] ; int val = q [ i , 2 ] ; A [ idx ] += val ; updateBIT ( BITree , n , idx , val ) ; } else { int k = q [ i , 1 ] ; int lb = getLowerBound ( BITree , A , n , k ) ; Console . WriteLine ( lb ) ; } } } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 5 , 8 } ; int n = A . Length ; int [ , ] q = { { 1 , 0 , 2 } , { 2 , 5 , 0 } , { 1 , 3 , 5 } } ; performQueries ( A , n , q ) ; } }
Minimum number of leaves required to be removed from a Tree to satisfy the given condition | C # Program to find the minimum number of leaves to be deleted ; Stores the count of safe nodes ; Function to perform DFS on the Tree to obtain the count of vertices that are not required to be deleted ; Update cost to reach the vertex ; If the vertex does not satisfy the condition ; Otherwise ; Traverse its subtree ; Driver Code ; Stores the Tree ; Perform DFS ; Print the number of nodes to be deleted
using System ; using System . Collections . Generic ; class GFG { static int cnt = 0 ; static void dfs ( int [ ] val , int [ ] cost , List < int > [ ] tr , int u , int s ) { s = s + cost [ u ] ; if ( s < 0 ) s = 0 ; if ( s > val [ u ] ) return ; cnt ++ ; for ( int i = 0 ; i < tr [ u ] . Count ; i ++ ) { dfs ( val , cost , tr , tr [ u ] [ i ] , s ) ; } } public static void Main ( String [ ] args ) { int n = 9 ; int [ ] val = { 88 , 22 , 83 , 14 , 95 , 91 , 98 , 53 , 11 } ; int [ ] cost = { - 1 , 24 , - 8 , 67 , 64 , 65 , 12 , - 80 , 8 } ; List < int > [ ] tr = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < tr . Length ; i ++ ) tr [ i ] = new List < int > ( ) ; tr [ 0 ] . Add ( 3 ) ; tr [ 0 ] . Add ( 4 ) ; tr [ 4 ] . Add ( 6 ) ; tr [ 6 ] . Add ( 2 ) ; tr [ 2 ] . Add ( 1 ) ; tr [ 2 ] . Add ( 8 ) ; tr [ 8 ] . Add ( 5 ) ; tr [ 5 ] . Add ( 7 ) ; dfs ( val , cost , tr , 0 , 0 ) ; Console . Write ( n - cnt ) ; } }
Check if an Array is made up of Subarrays of continuous repetitions of every distinct element | C # program to implement the above approach ; Function to check if the array is made up of subarrays of repetitions ; Base Case ; Stores the size of current subarray ; If a different element is encountered ; If the previous subarray was a single element ; Reset to new subarray ; Increase size of subarray ; If last element differed from the second last element ; Driver code
using System ; class GFG { static Boolean ContinuousElements ( int [ ] a , int n ) { if ( n == 1 ) return false ; int curr = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { if ( curr == 1 ) return false ; else curr = 0 ; } curr ++ ; } if ( curr == 1 ) return false ; return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 1 , 2 , 2 , 1 , 3 , 3 } ; int n = a . Length ; if ( ContinuousElements ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Size of all connected non | C # program to implement the above approach ; Function to find size of all the islands from the given matrix ; Initialize a queue for the BFS traversal ; Iterate until the queue is empty ; Top element of queue ; Pop the element ; Check for boundaries ; Check if current element is 0 ; Check if current element is 1 ; Mark the cell visited ; Incrementing the size ; Traverse all neighbors ; Return the answer ; Function to print size of each connections ; Stores the size of each connected non - empty ; Check if the cell is non - empty ; Function call ; Print the answer ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int BFS ( int [ , ] mat , int row , int col ) { int area = 0 ; Queue < pair > Q = new Queue < pair > ( ) ; Q . Enqueue ( new pair ( row , col ) ) ; while ( Q . Count != 0 ) { pair it = Q . Peek ( ) ; Q . Dequeue ( ) ; int r = it . first , c = it . second ; if ( r < 0 c < 0 r > 4 c > 4 ) continue ; if ( mat [ r , c ] == 0 ) continue ; if ( mat [ r , c ] == 1 ) { mat [ r , c ] = 0 ; area ++ ; } Q . Enqueue ( new pair ( r + 1 , c ) ) ; Q . Enqueue ( new pair ( r - 1 , c ) ) ; Q . Enqueue ( new pair ( r , c + 1 ) ) ; Q . Enqueue ( new pair ( r , c - 1 ) ) ; } return area ; } static void sizeOfConnections ( int [ , ] mat ) { ArrayList result = new ArrayList ( ) ; for ( int row = 0 ; row < 5 ; ++ row ) { for ( int col = 0 ; col < 5 ; ++ col ) { if ( mat [ row , col ] == 1 ) { int area = BFS ( mat , row , col ) ; result . Add ( area ) ; } } } foreach ( int val in result ) Console . Write ( val + " ▁ " ) ; } public static void Main ( string [ ] args ) { int [ , ] mat = { { 1 , 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 , 1 } , { 1 , 0 , 0 , 1 , 1 } , { 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 1 , 1 } } ; sizeOfConnections ( mat ) ; } }
Find a pair in Array with second largest product | C # program for the above approach ; Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Sort the array ; Initialize smallest element of the array ; Initialize largest element of the array ; Print second largest product pair ; Driver Code ; Given array ; Function Call
using System ; class GFG { static void maxProduct ( int [ ] arr , int N ) { if ( N < 3 ) { return ; } Array . Sort ( arr ) ; int smallest1 = arr [ 0 ] ; int smallest3 = arr [ 2 ] ; int largest1 = arr [ N - 1 ] ; int largest3 = arr [ N - 3 ] ; if ( smallest1 * smallest3 >= largest1 * largest3 ) { Console . Write ( smallest1 + " ▁ " + smallest3 ) ; } else { Console . Write ( largest1 + " ▁ " + largest3 ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = arr . Length ; maxProduct ( arr , N ) ; } }
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | C # program to implement the above approach ; Function to find the maximum subarray length of ones ; Stores the length , starting index and ending index of the subarrays ; S : starting index of the sub - array ; Traverse only continuous 1 s ; Calculate length of the sub - array ; v [ i , 0 ] : Length of subarray v [ i , 1 ] : Starting Index of subarray v [ i , 2 ] : Ending Index of subarray ; If no such sub - array exists ; Traversing through the subarrays ; Update maximum length ; v [ i + 1 , 1 ] - v [ i , 2 ] - 1 : Count of zeros between the two sub - arrays ; Update length of both subarrays to the maximum ; Update length of both subarrays - 1 to the maximum ; Check if the last subarray has the maximum length ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maxLen ( int [ ] A , int N ) { List < List < int > > v = new List < List < int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 1 ) { int s = i , len ; while ( i < N && A [ i ] == 1 ) { i ++ ; } len = i - s ; List < int > l = new List < int > { len , s , i - 1 } ; v . Add ( l ) ; } } if ( v . Count == 0 ) { return - 1 ; } int ans = 0 ; for ( int i = 0 ; i < v . Count - 1 ; i ++ ) { ans = Math . Max ( ans , v [ i ] [ 0 ] ) ; if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 2 ) { ans = Math . Max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] ) ; } if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 1 ) { ans = Math . Max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] - 1 ) ; } } ans = Math . Max ( v [ v . Count - 1 ] [ 0 ] , ans ) ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 1 , 0 , 0 , 1 } ; int N = arr . Length ; Console . WriteLine ( maxLen ( arr , N ) ) ; } }
Detect cycle in Directed Graph using Topological Sort | C # program to implement the above approach ; Stack to store the visited vertices in the Topological Sort ; Store Topological Order ; Adjacency list to store edges ; To ensure visited vertex ; Function to perform DFS ; Set the vertex as visited ; Visit connected vertices ; Push into the stack on complete visit of vertex ; Function to check and return if a cycle exists or not ; Stores the position of vertex in topological order ; Pop all elements from stack ; Push element to get Topological Order ; Pop from the stack ; If parent vertex does not appear first ; Cycle exists ; Return false if cycle does not exist ; Function to add edges from u to v ; Driver code ; Insert edges ; If cycle exist
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int n ; static Stack < int > s ; static ArrayList tsort ; static ArrayList adj ; static int [ ] visited = new int [ 100001 ] ; static void dfs ( int u ) { visited [ u ] = 1 ; foreach ( int it in ( ArrayList ) adj [ u ] ) { if ( visited [ it ] == 0 ) dfs ( it ) ; } s . Push ( u ) ; } static bool check_cycle ( ) { Dictionary < int , int > pos = new Dictionary < int , int > ( ) ; int ind = 0 ; while ( s . Count != 0 ) { pos . Add ( s . Peek ( ) , ind ) ; tsort . Add ( s . Peek ( ) ) ; ind += 1 ; s . Pop ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { foreach ( int it in ( ArrayList ) adj [ i ] ) { if ( pos [ i ] > pos [ it ] ) { return true ; } } } return false ; } static void addEdge ( int u , int v ) { ( ( ArrayList ) adj [ u ] ) . Add ( v ) ; } public static void Main ( string [ ] args ) { n = 4 ; s = new Stack < int > ( ) ; adj = new ArrayList ( ) ; tsort = new ArrayList ( ) ; for ( int i = 0 ; i < 4 ; i ++ ) adj . Add ( new ArrayList ( ) ) ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 0 ) ; addEdge ( 2 , 3 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ i ] == 0 ) { dfs ( i ) ; } } if ( check_cycle ( ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }