codeparrot/xlcost-text-to-code huggingface dataset:
Longest Span with same Sum in two Binary arrays | A Simple C # program to find longest common subarray of two binary arrays with same sum ; Returns length of the longest common sum in arr1 [ ] and arr2 [ ] . Both are of same size n . ; Initialize result ; One by one pick all possible starting points of subarrays ; Initialize sums of current subarrays ; Conider all points for starting with arr [ i ] ; Update sums ; If sums are same and current length is more than maxLen , update maxLen ; Driver Code
using System ; class GFG { static int [ ] arr1 = new int [ ] { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; static int [ ] arr2 = new int [ ] { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; static int longestCommonSum ( int n ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum1 = 0 , sum2 = 0 ; for ( int j = i ; j < n ; j ++ ) { sum1 += arr1 [ j ] ; sum2 += arr2 [ j ] ; if ( sum1 == sum2 ) { int len = j - i + 1 ; if ( len > maxLen ) maxLen = len ; } } } return maxLen ; } public static void Main ( ) { Console . Write ( " Length ▁ of ▁ the ▁ longest ▁ " + " common ▁ span ▁ with ▁ same ▁ sum ▁ is ▁ " ) ; Console . Write ( longestCommonSum ( arr1 . Length ) ) ; } }
Number of Longest Increasing Subsequences | C # program to implement the above approach ; Function to count the number of LIS in the array nums [ ] ; Base Case ; Initialize dp_l array with 1 s ; Initialize dp_c array with 1 s ; If current element is smaller ; Store the maximum element from dp_l ; Stores the count of LIS ; Traverse dp_l and dp_c simultaneously ; Update the count ; Return the count of LIS ; Driver code ; Given array arr [ ] ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int findNumberOfLIS ( int [ ] nums ) { if ( nums . Length == 0 ) return 0 ; int n = nums . Length ; int [ ] dp_l = new int [ n ] ; Array . Fill ( dp_l , 1 ) ; int [ ] dp_c = new int [ n ] ; Array . Fill ( dp_c , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( nums [ i ] <= nums [ j ] ) continue ; if ( dp_l [ j ] + 1 > dp_l [ i ] ) { dp_l [ i ] = dp_l [ j ] + 1 ; dp_c [ i ] = dp_c [ j ] ; } else if ( dp_l [ j ] + 1 == dp_l [ i ] ) dp_c [ i ] += dp_c [ j ] ; } } int max_length = 0 ; foreach ( int i in dp_l ) max_length = Math . Max ( i , max_length ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp_l [ i ] == max_length ) count += dp_c [ i ] ; } return count ; } static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 4 , 7 } ; Console . WriteLine ( findNumberOfLIS ( arr ) ) ; } }
Length of the longest increasing subsequence which does not contain a given sequence as Subarray | C # program for the above approach ; Initialize dp and KMP array ; Length of the given sequence [ ] ; Function to find the max - length subsequence that does not have subarray sequence [ ] ; Stores the subsequence explored so far ; Base Case ; Using memoization to avoid re - computation ; Using KMP array to find corresponding index in arr1 [ ] ; Incrementing k2 as we are including this element in the subsequence ; Possible answer for current state ; Maximum answer for current state ; Memoizing the answer for the corresponding state ; Return the answer for current state ; Function that generate KMP Array ; Previous longest prefix suffix ; KMPArray [ 0 ] is a always 0 ; The loop calculates KMPArray [ i ] for i = 1 to M - 1 ; If current character is same ; Update the KMP array ; Otherwise ; Update the KMP array ; Function to print the maximum possible length ; Length of the given sequence ; Initialize kmp array ; Generate KMP array ; Initialize dp ; Initialize the states to - 1 ; Get answer ; Print answer ; Driver code ; Given array ; Give arr1 ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int [ , , ] dp ; static int [ ] KMPArray ; static int m ; private static int findSubsequence ( int [ ] a , int [ ] sequence , int i , int prev , int k ) { if ( k == m ) return int . MinValue ; if ( i == a . Length ) return 0 ; if ( prev != - 1 && dp [ i , prev , k ] != - 1 ) { return dp [ i , prev , k ] ; } int include = 0 ; if ( prev == - 1 a [ i ] >= a [ prev ] ) { int k2 = k ; while ( k2 > 0 && a [ i ] != sequence [ k2 ] ) k2 = KMPArray [ k2 - 1 ] ; if ( a [ i ] == sequence [ k2 ] ) k2 ++ ; include = 1 + findSubsequence ( a , sequence , i + 1 , i , k2 ) ; } int ans = Math . Max ( include , findSubsequence ( a , sequence , i + 1 , prev , k ) ) ; if ( prev != - 1 ) { dp [ i , prev , k ] = ans ; } return ans ; } private static void fillKMPArray ( int [ ] pattern ) { int j = 0 ; int i = 1 ; KMPArray [ 0 ] = 0 ; while ( i < m ) { if ( pattern [ i ] == pattern [ j ] ) { j ++ ; KMPArray [ i ] = j ; i ++ ; } else { if ( j != 0 ) j = KMPArray [ j - 1 ] ; else { KMPArray [ i ] = j ; i ++ ; } } } } static void printAnswer ( int [ ] a , int [ ] sequence ) { m = sequence . Length ; KMPArray = new int [ m ] ; fillKMPArray ( sequence ) ; dp = new int [ a . Length , a . Length , a . Length ] ; for ( int i = 0 ; i < a . Length ; i ++ ) for ( int j = 0 ; j < a . Length ; j ++ ) for ( int k = 0 ; k < a . Length ; k ++ ) dp [ i , j , k ] = - 1 ; int ans = findSubsequence ( a , sequence , 0 , - 1 , 0 ) ; Console . WriteLine ( ( ans < 0 ) ? 0 : ans ) ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 9 , 3 , 4 , 7 } ; int [ ] arr1 = { 3 , 4 } ; printAnswer ( arr , arr1 ) ; } }
Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels | C # Program for the above approach ; Structure of Tree node ; Function to create new Tree node ; Function to check if given binary tree satisfies the required conditions ; Queue to store nodes of each level ; Stores the current level of the binary tree ; Traverse until the queue is empty ; Stores the number of nodes present in the current level ; Insert left and right child of node into the queue ; If the level is even ; If the nodes in this level are in strictly increasing order or not ; If the level is odd ; If the nodes in this level are in strictly decreasing order or not ; Increment the level count ; Driver Code ; Construct a Binary Tree ; Function Call
using System ; using System . Collections . Generic ; class GFG { public class Node { public int val ; public Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . val = data ; temp . left = null ; temp . right = null ; return temp ; } public static bool checkEvenOddLevel ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; int level = 0 ; while ( q . Count != 0 ) { List < int > list = new List < int > ( ) ; int size = q . Count ; for ( int i = 0 ; i < size ; i ++ ) { Node node = q . Dequeue ( ) ; list . Add ( node . val ) ; if ( node . left != null ) q . Enqueue ( node . left ) ; if ( node . right != null ) q . Enqueue ( node . right ) ; } if ( level % 2 == 0 ) { for ( int i = 0 ; i < list . Count - 1 ; i ++ ) { if ( list [ i + 1 ] > list [ i ] ) continue ; return false ; } } else if ( level % 2 == 1 ) { for ( int i = 0 ; i < list . Count - 1 ; i ++ ) { if ( list [ i + 1 ] < list [ i ] ) continue ; return false ; } } level ++ ; } return true ; } public static void Main ( String [ ] args ) { Node root = null ; root = newNode ( 2 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 7 ) ; root . right . right = newNode ( 11 ) ; root . left . left . left = newNode ( 10 ) ; root . left . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 1 ) ; if ( checkEvenOddLevel ( root ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Longest Span with same Sum in two Binary arrays | C # program to find largest subarray with equal number of 0 ' s ▁ and ▁ 1' s . ; Returns largest common subarray with equal number of 0 s and 1 s ; Find difference between the two ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; To handle sum = 0 at last index ; If this sum is seen before , then update max_len if required ; Else put this sum in hash table ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static int longestCommonSum ( int [ ] arr1 , int [ ] arr2 , int n ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr1 [ i ] - arr2 [ i ] ; Dictionary < int , int > hM = new Dictionary < int , int > ( ) ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) max_len = i + 1 ; if ( hM . ContainsKey ( sum ) ) max_len = Math . Max ( max_len , i - hM [ sum ] ) ; else hM [ sum ] = i ; } return max_len ; } static public void Main ( ) { int [ ] arr1 = { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; int [ ] arr2 = { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; int n = arr1 . Length ; Console . WriteLine ( longestCommonSum ( arr1 , arr2 , n ) ) ; } }
Minimum length of Run Length Encoding possible by removing at most K characters from a given string | C # program to implement the above approach ; Function which solves the desired problem ; Base Case ; If the entire string has been traversed ; If precomputed subproblem occurred ; Minimum run length encoding by removing the current character ; Minimum run length encoding by retaining the current character ; If the current and the previous characters match ; Otherwise ; Function to return minimum run - length encoding for string s by removing atmost k characters ; Driver Code
using System ; class GFG { static int maxN = 20 ; static int [ , , , ] dp = new int [ maxN , maxN , 27 , maxN ] ; public static int solve ( String s , int n , int idx , int k , char last , int count ) { if ( k < 0 ) return n + 1 ; if ( idx == n ) return 0 ; int ans = dp [ idx , k , last - ' a ' , count ] ; if ( ans != - 1 ) return ans ; ans = n + 1 ; ans = Math . Min ( ans , solve ( s , n , idx + 1 , k - 1 , last , count ) ) ; int inc = 0 ; if ( count == 1 count == 9 count == 99 ) inc = 1 ; if ( last == s [ idx ] ) { ans = Math . Min ( ans , inc + solve ( s , n , idx + 1 , k , s [ idx ] , count + 1 ) ) ; } else { ans = Math . Min ( ans , 1 + solve ( s , n , idx + 1 , k , s [ idx ] , 1 ) ) ; } return dp [ idx , k , last - ' a ' , count ] = ans ; } public static int MinRunLengthEncoding ( String s , int n , int k ) { for ( int i = 0 ; i < maxN ; i ++ ) for ( int j = 0 ; j < maxN ; j ++ ) for ( int p = 0 ; p < 27 ; p ++ ) for ( int l = 0 ; l < maxN ; l ++ ) dp [ i , j , p , l ] = - 1 ; return solve ( s , n , 0 , k , ( char ) 123 , 0 ) ; } public static void Main ( String [ ] args ) { String S = " abbbcdcdd " ; int N = 9 , K = 2 ; Console . WriteLine ( MinRunLengthEncoding ( S , N , K ) ) ; } }
Count unique paths is a matrix whose product of elements contains odd number of divisors | C # program for the above approach ; Function to calculate and store all the paths product in vector ; Base Case ; If reaches the bottom right corner and product is a perfect square ; Find square root ; If square root is an integer ; Move towards down in the matrix ; Move towards right in the matrix ; Driver Code ; Given matrix mat [ ] [ ] ; Function call
using System ; class GFG { static int countPaths = 0 ; static void CountUniquePaths ( int [ , ] a , int i , int j , int m , int n , int ans ) { if ( i >= m j >= n ) return ; if ( i == m - 1 && j == n - 1 ) { double sr = Math . Sqrt ( ans * a [ i , j ] ) ; if ( ( sr - Math . Floor ( sr ) ) == 0 ) countPaths ++ ; } CountUniquePaths ( a , i + 1 , j , m , n , ans * a [ i , j ] ) ; CountUniquePaths ( a , i , j + 1 , m , n , ans * a [ i , j ] ) ; } public static void Main ( String [ ] args ) { int M = 3 , N = 2 ; int [ , ] mat = { { 1 , 1 } , { 3 , 1 } , { 3 , 1 } } ; CountUniquePaths ( mat , 0 , 0 , M , N , 1 ) ; Console . Write ( countPaths ) ; } }
Maximize sum by selecting M elements from the start or end of rows of a Matrix | C # program for the above approach ; Function to select m elements having maximum sum ; Base case ; If precomputed subproblem occurred ; Either skip the current row ; Iterate through all the possible segments of current row ; Check if it is possible to select elements from i to j ; Compuete the sum of i to j as calculated ; Store the computed answer and return ; Function to precompute the prefix sum for every row of the matrix ; Preprocessing to calculate sum from i to j ; Utility function to select m elements having maximum sum ; Preprocessing step ; Initialize dp array with - 1 ; Stores maximum sum of M elements ;  ; Driver Code ; Given N ; Given M ; Given matrix ; Function Call
using System ; class GFG { public static int mElementsWithMaxSum ( int [ , ] matrix , int M , int block , int [ , ] dp ) { if ( block == matrix . GetLength ( 0 ) ) return 0 ; if ( dp [ block , M ] != - 1 ) return dp [ block , M ] ; int ans = mElementsWithMaxSum ( matrix , M , block + 1 , dp ) ; for ( int i = 0 ; i < GetRow ( matrix , block ) . Length ; i ++ ) { for ( int j = i ; j < GetRow ( matrix , block ) . Length ; j ++ ) { if ( j - i + 1 <= M ) { ans = Math . Max ( ans , matrix [ block , j ] - ( ( i - 1 ) >= 0 ? matrix [ block , i - 1 ] : 0 ) + mElementsWithMaxSum ( matrix , M - j + i - 1 , block + 1 , dp ) ) ; } } } return dp [ block , M ] = ans ; } public static void preComputing ( int [ , ] matrix , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < GetRow ( matrix , i ) . Length ; j ++ ) { matrix [ i , j ] = ( j > 0 ? matrix [ i , j - 1 ] : 0 ) + matrix [ i , j ] ; } } } public static void mElementsWithMaxSumUtil ( int [ , ] matrix , int M , int N ) { preComputing ( matrix , N ) ; int [ , ] dp = new int [ N + 5 , M + 5 ] ; for ( int i = 0 ; i < N + 5 ; i ++ ) { for ( int j = 0 ; j < M + 5 ; j ++ ) { dp [ i , j ] = - 1 ; } } int sum = mElementsWithMaxSum ( matrix , M , 0 , dp ) ; Console . Write ( sum ) ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int N = 3 ; int M = 4 ; int [ , ] matrix = { { 2 , 3 , 5 } , { - 1 , 7 , 0 } , { 8 , 10 , 0 } } ; mElementsWithMaxSumUtil ( matrix , M , N ) ; } }
Find the last remaining element after repeated removal of odd and even indexed elements alternately | C # program for the above approach ; Function to calculate the last remaining element from the sequence ; If dp [ n ] is already calculated ; Base Case : ; Recursive call ; Return the value of dp [ n ] ; Driver Code ; Given N ; Stores the ; Function call
using System ; using System . Collections . Generic ; class GFG { static int lastRemaining ( int n , Dictionary < int , int > dp ) { if ( dp . ContainsKey ( n ) ) return dp [ n ] ; if ( n == 1 ) return 1 ; else dp . Add ( n , 2 * ( 1 + n / 2 - lastRemaining ( n / 2 , dp ) ) ) ; return dp [ n ] ; } public static void Main ( String [ ] args ) { int N = 5 ; Dictionary < int , int > dp = new Dictionary < int , int > ( ) ; Console . Write ( lastRemaining ( N , dp ) ) ; } }
Maximum subsequence sum possible by multiplying each element by its index | C # program for the above approach ; Initialize dp array ; Function to find the maximum sum of the subsequence formed ; Base Case ; If already calculated state occurs ; Include the current element ; Exclude the current element ; Update the maximum ans ; Function to calculate maximum sum of the subsequence obtained ; Initialise the dp array with - 1 ; Print the maximum sum possible ; Driver Code ; Given array ; Size of the array ; Function Call
using System ; class GFG { static int [ , ] dp = new int [ 1005 , 1005 ] ; static int maximumSumUtil ( int [ ] a , int index , int count , int n ) { if ( index > n count > n + 1 ) { return 0 ; } if ( dp [ index , count ] != - 1 ) return dp [ index , count ] ; int ans1 = maximumSumUtil ( a , index + 1 , count + 1 , n ) + a [ index ] * count ; int ans2 = maximumSumUtil ( a , index + 1 , count , n ) ; return ( dp [ index , count ] = Math . Max ( ans1 , ans2 ) ) ; } static void maximumSum ( int [ ] arr , int N ) { for ( int i = 0 ; i < 1005 ; i ++ ) { for ( int j = 0 ; j < 1005 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . Write ( maximumSumUtil ( arr , 0 , 1 , N - 1 ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 1 , 2 , - 10 , 4 , - 20 } ; int N = arr . Length ; maximumSum ( arr , N ) ; } }
Sort an array according to absolute difference with given value | C # program to sort an array according absolute difference with x . ; Function to sort an array according absolute difference with x . ; Store values in a map with the difference with X as key ; Update the values of array ; Function to print the array ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static void rearrange ( int [ ] arr , int n , int x ) { SortedDictionary < int , List < int > > m = new SortedDictionary < int , List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int diff = Math . Abs ( x - arr [ i ] ) ; if ( m . ContainsKey ( diff ) ) { List < int > al = m ; al . Add ( arr [ i ] ) ; m = al ; } else { List < int > al = new List < int > ( ) ; al . Add ( arr [ i ] ) ; m . Add ( diff , al ) ; } } int index = 0 ; foreach ( int entry in m . Keys ) { List < int > al = m [ entry ] ; for ( int i = 0 ; i < al . Count ; i ++ ) arr [ index ++ ] = al [ i ] ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static public void Main ( ) { int [ ] arr = { 10 , 5 , 3 , 9 , 2 } ; int n = arr . Length ; int x = 7 ; rearrange ( arr , n , x ) ; printArray ( arr , n ) ; } }
Minimum Steps to obtain N from 1 by the given operations | C # program to implement the above approach ; Base Case ; Recursive Call for n - 1 ; Check if n is divisible by 2 ; Check if n is divisible by 3 ; Returns a tuple ( a , b ) , where a : Minimum steps to obtain x from 1 b : Previous number ; Function that find the optimal solution ; Print the length ; Exit condition for loop = - 1 when n has reached 1 ; Return the sequence in reverse order ; Driver Code ; Given N ; Function call
using System ; using System . Collections . Generic ; class GFG { static List < int > find_sequence ( int n ) { List < int > temp = new List < int > ( ) ; temp . Add ( 1 ) ; temp . Add ( - 1 ) ; if ( n == 1 ) return temp ; List < int > arr = find_sequence ( n - 1 ) ; List < int > ans = new List < int > ( n ) ; ans . Add ( arr [ 0 ] + 1 ) ; ans . Add ( n - 1 ) ; if ( n % 2 == 0 ) { List < int > div_by_2 = find_sequence ( n / 2 ) ; if ( div_by_2 [ 0 ] < ans [ 0 ] ) { ans . Clear ( ) ; ans . Add ( div_by_2 [ 0 ] + 1 ) ; ans . Add ( n / 2 ) ; } } if ( n % 3 == 0 ) { List < int > div_by_3 = find_sequence ( n / 3 ) ; if ( div_by_3 [ 0 ] < ans [ 0 ] ) { ans . Clear ( ) ; ans . Add ( div_by_3 [ 0 ] + 1 ) ; ans . Add ( n / 3 ) ; } } return ans ; } static List < int > find_solution ( int n ) { List < int > a = find_sequence ( n ) ; Console . WriteLine ( a [ 0 ] ) ; List < int > sequence = new List < int > ( ) ; sequence . Add ( n ) ; while ( a [ 1 ] != - 1 ) { sequence . Add ( a [ 1 ] ) ; List < int > arr = find_sequence ( a [ 1 ] ) ; a . Insert ( 1 , arr [ 1 ] ) ; } sequence . Reverse ( ) ; return sequence ; } public static void Main ( String [ ] args ) { int n = 5 ; List < int > res = find_solution ( n ) ; for ( int i = 0 ; i < res . Count ; i ++ ) { Console . Write ( res [ i ] + " ▁ " ) ; } } }
Count of vessels completely filled after a given time | C # program to implement the above approach ; Function to find the number of completely filled vessels ; Store the vessels ; Assuming all water is present in the vessel at the first level ; Store the number of vessel that are completely full ; Traverse all the levels ; Number of vessel at each level is j ; Calculate the exceeded amount of water ; If current vessel has less than 1 unit of water then continue ; One more vessel is full ; If left bottom vessel present ; If right bottom vessel present ; Driver Code ; Number of levels ; Number of seconds ; Function call
using System ; class GFG { static int n , t ; public static int FindNoOfFullVessels ( int n , int t ) { double [ , ] Matrix = new double [ n , n ] ; Matrix [ 0 , 0 ] = t * 1.0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { double exceededwater = Matrix [ i , j ] - 1.0 ; if ( exceededwater < 0 ) continue ; ans ++ ; if ( i + 1 < n ) Matrix [ i + 1 , j ] += exceededwater / 2 ; if ( i + 1 < n && j + 1 < n ) Matrix [ i + 1 , j + 1 ] += exceededwater / 2 ; } } return ans ; } public static void Main ( ) { int N = 3 ; int T = 4 ; Console . WriteLine ( FindNoOfFullVessels ( N , T ) ) ; } }
Sort an array in wave form | C # implementation of naive method for sorting an array in wave form . ; A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] . . ; Sort the input array ; Swap adjacent elements ; Driver method
using System ; class SortWave { void swap ( int [ ] arr , int a , int b ) { int temp = arr [ a ] ; arr [ a ] = arr [ b ] ; arr [ b ] = temp ; } void sortInWave ( int [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i += 2 ) swap ( arr , i , i + 1 ) ; } public static void Main ( ) { SortWave ob = new SortWave ( ) ; int [ ] arr = { 10 , 90 , 49 , 2 , 1 , 5 , 23 } ; int n = arr . Length ; ob . sortInWave ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Count of maximum occurring subsequence using only those characters whose indices are in GP | C # program for the above approach ; Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Iterate till the length of the given String ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Driver Code ; Given String s ; Function call
using System ; class GFG { static int findMaxTimes ( String S ) { int [ ] arr = new int [ 26 ] ; int [ , ] dp = new int [ 26 , 26 ] ; for ( int i = 0 ; i < S . Length ; i ++ ) { int now = S [ i ] - ' a ' ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j , now ] += arr [ j ] ; } arr [ now ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = Math . Max ( ans , arr [ i ] ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = Math . Max ( ans , dp [ i , j ] ) ; } } return ans ; } public static void Main ( String [ ] args ) { String S = " ddee " ; Console . Write ( findMaxTimes ( S ) ) ; } }
Sort an array in wave form | A O ( n ) C # program to sort an input array in wave form ; A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] ... . ; Traverse all even elements ; If current even element is smaller than previous ; If current even element is smaller than next ; Driver program to test above function
using System ; class SortWave { void swap ( int [ ] arr , int a , int b ) { int temp = arr [ a ] ; arr [ a ] = arr [ b ] ; arr [ b ] = temp ; } void sortInWave ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i += 2 ) { if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( arr , i - 1 , i ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( arr , i , i + 1 ) ; } } public static void Main ( ) { SortWave ob = new SortWave ( ) ; int [ ] arr = { 10 , 90 , 49 , 2 , 1 , 5 , 23 } ; int n = arr . Length ; ob . sortInWave ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }