codeparrot/xlcost-text-to-code huggingface dataset:
Longest subarray forming an Arithmetic Progression ( AP ) | C # Program to implement the above approach ; Function to return the length of longest subarray forming an AP ; Minimum possible length of required subarray is 2 ; Stores the length of the current subarray ; Stores the common difference of the current AP ; Stores the common difference of the previous AP ; If the common differences are found to be equal ; Continue the previous subarray ; Start a new subarray ; Update the length to store maximum length ; Update the length to store maximum length ; Return the length of the longest subarray ; Driver Code
using System ; public class GFG { static int getMaxLength ( int [ ] arr , int N ) { int res = 2 ; int dist = 2 ; int curradj = ( arr [ 1 ] - arr [ 0 ] ) ; int prevadj = ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < N ; i ++ ) { curradj = arr [ i ] - arr [ i - 1 ] ; if ( curradj == prevadj ) { dist ++ ; } else { prevadj = curradj ; res = Math . Max ( res , dist ) ; dist = 2 ; } } res = Math . Max ( res , dist ) ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 7 , 4 , 6 , 8 , 10 , 11 } ; int N = arr . Length ; Console . Write ( getMaxLength ( arr , N ) ) ; } }
Minimize cost to empty a given string by removing characters alphabetically | C # program for the above approach ; Function to find the minimum cost required to remove each character of the string in alphabetical order ; Stores the frequency of characters of the string ; Iterate through the string ; Count the number of characters smaller than the present character ; If no smaller character precedes current character ; Increase the frequency of the current character ; Return the total cost ; Driver Code ; Given string str ; Function call
using System ; class GFG { static int minSteps ( string str , int N ) { int smaller , cost = 0 ; int [ ] f = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = str [ i ] - ' a ' ; smaller = 0 ; for ( int j = 0 ; j <= curr_ele ; j ++ ) { if ( f [ j ] != 0 ) smaller += f [ j ] ; } if ( smaller == 0 ) cost += ( i + 1 ) ; else cost += ( i - smaller + 1 ) ; f [ str [ i ] - ' a ' ] ++ ; } return cost ; } public static void Main ( ) { string str = " abcab " ; int N = str . Length ; Console . Write ( minSteps ( str , N ) ) ; } }
Count of Rectangles with area K made up of only 1 s from given Binary Arrays | C # Program to implement the above approach ; Function to find the subarrays of all possible lengths made up of only 1 s ; Stores the frequency of the subarrays ; Check if the previous value was also 0 ; If the previous value was 1 ; Find the subarrays of each size from 1 to count ; If [ ] A is of the form ... .111 ; Function to find the count of all possible rectangles ; Size of each of the arrays ; Stores the count of subarrays of each size consisting of only 1 s from array [ ] A ; Stores the count of subarrays of each size consisting of only 1 s from array [ ] B ; Iterating over all subarrays consisting of only 1 s in [ ] A ; If i is a factor of K , then there is a subarray of size K / i in [ ] B ; Driver Code
using System ; class GFG { static int [ ] findSubarrays ( int [ ] a ) { int n = a . Length ; int [ ] freq = new int [ n + 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { if ( count == 0 ) continue ; else { int value = count ; for ( int j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } count = 0 ; } } else count ++ ; } if ( count > 0 ) { int value = count ; for ( int j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } } return freq ; } static void countRectangles ( int [ ] a , int [ ] b , int K ) { int n = a . Length ; int m = b . Length ; int [ ] subA = findSubarrays ( a ) ; int [ ] subB = findSubarrays ( b ) ; int total = 0 ; for ( int i = 1 ; i < subA . Length ; i ++ ) { if ( K % i == 0 && ( K / i ) <= m ) { total = total + subA [ i ] * subB [ K / i ] ; } } Console . Write ( total ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 0 , 0 , 1 , 1 } ; int [ ] b = { 1 , 0 , 1 } ; int K = 2 ; countRectangles ( a , b , K ) ; } }
Print Longest Bitonic subsequence ( Space Optimized Approach ) | C # program to implement the above approach ; Function to print the longest bitonic subsequence ; Function to generate the longest bitonic subsequence ; Store the lengths of LIS ending at every index ; Store the lengths of LDS ending at every index ; Compute LIS for all indices ; Compute LDS for all indices ; Find the index having maximum value of lis [ i ] + lds [ i ] - 1 ; Stores the count of elements in increasing order in Bitonic subsequence ; Store the increasing subsequence ; Sort the bitonic subsequence to arrange smaller elements at the beginning ; Stores the count of elements in decreasing order in Bitonic subsequence ; Print the longest bitonic sequence ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void printRes ( List < int > res ) { foreach ( int enu in res ) { Console . Write ( enu + " ▁ " ) ; } } static void printLBS ( int [ ] arr , int N ) { int [ ] lis = new int [ N ] ; int [ ] lds = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { lis [ i ] = lds [ i ] = 1 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { if ( lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } } for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] ) { if ( lds [ i ] < lds [ j ] + 1 ) lds [ i ] = lds [ j ] + 1 ; } } } int MaxVal = arr [ 0 ] , inx = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( MaxVal < lis [ i ] + lds [ i ] - 1 ) { MaxVal = lis [ i ] + lds [ i ] - 1 ; inx = i ; } } int ct1 = lis [ inx ] ; List < int > res = new List < int > ( ) ; for ( int i = inx ; i >= 0 && ct1 > 0 ; i -- ) { if ( lis [ i ] == ct1 ) { res . Add ( arr [ i ] ) ; ct1 -- ; } } res . Reverse ( ) ; int ct2 = lds [ inx ] - 1 ; for ( int i = inx ; i < N && ct2 > 0 ; i ++ ) { if ( lds [ i ] == ct2 ) { res . Add ( arr [ i ] ) ; ct2 -- ; } } printRes ( res ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 80 , 60 , 30 , 40 , 20 , 10 } ; int N = arr . Length ; printLBS ( arr , N ) ; } }
Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible | C # program for the above approach ; Function to check if the array can be partitioned into atmost d subarray with sum atmost T ; Initial partition ; Current sum ; If current sum exceeds T ; Create a new partition ; If count of partitions exceed d ; Function to find the minimum possible value of T ; Stores the maximum and total sum of elements ; Maximum element ; Sum of all elements ; Calculate median T ; If atmost D partitions possible ; Check for smaller T ; Otherwise ; Check for larger T ; Print the minimum T required ; Driver code ; Function call
using System ; class GFG { public static bool possible ( int T , int [ ] arr , int n , int d ) { int partition = 1 ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total = total + arr [ i ] ; if ( total > T ) { partition = partition + 1 ; total = arr [ i ] ; if ( partition > d ) { return false ; } } } return true ; } public static void calcT ( int n , int d , int [ ] arr ) { int mx = - 1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . Max ( mx , arr [ i ] ) ; sum = sum + arr [ i ] ; } int lb = mx ; int ub = sum ; while ( lb < ub ) { int T_mid = lb + ( ub - lb ) / 2 ; if ( possible ( T_mid , arr , n , d ) == true ) { ub = T_mid ; } else { lb = T_mid + 1 ; } } Console . WriteLine ( lb ) ; } public static void Main ( String [ ] args ) { int d = 2 ; int [ ] arr = { 1 , 1 , 1 , 1 , 1 } ; int n = arr . Length ; calcT ( n , d , arr ) ; } }
Minimize splits to generate monotonous Substrings from given String | C # Program to implement the above approach ; Function to return readonly result ; Initialize variable to keep track of ongoing sequence ; If current sequence is neither increasing nor decreasing ; If prev char is greater ; If prev char is same ; Otherwise ; If current sequence is Non - Decreasing ; If prev char is smaller ; If prev char is same ; Update ongoing ; Otherwise ; Update ongoing ; Increase count ; If current sequence is Non - Increasing ; If prev char is greater , then update ongoing with D ; If prev char is equal , then update current with D ; Otherwise , update ongoing with N and increment count ; Return count + 1 ; Driver Code
using System ; class GFG { static int minReqSubstring ( String s , int n ) { char ongoing = ' N ' ; int count = 0 , l = s . Length ; for ( int i = 1 ; i < l ; i ++ ) { if ( ongoing == ' N ' ) { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' D ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' N ' ; } else { ongoing = ' I ' ; } } else if ( ongoing == ' I ' ) { if ( s [ i ] > s [ i - 1 ] ) { ongoing = ' I ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' I ' ; } else { ongoing = ' N ' ; count += 1 ; } } else { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' D ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' D ' ; } else { ongoing = ' N ' ; count += 1 ; } } } return count + 1 ; } public static void Main ( String [ ] args ) { String S = " aeccdhba " ; int n = S . Length ; Console . Write ( minReqSubstring ( S , n ) ) ; } }
Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K | C # program to implement the above approach ; Function to calculate the minimum number of operations required ; Stores the total number of operations ; Iterate over the array ; If the sum of pair of adjacent elements exceed k . ; If current element exceeds k ; Reduce arr [ i ] to k ; Update arr [ i + 1 ] accordingly ; Update answer ; Driver Code
using System ; class GFG { static int minimum_required_operations ( int [ ] arr , int n , int k ) { int answer = 0 ; long mod = 1000000007 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + arr [ i + 1 ] > k ) { if ( arr [ i ] > k ) { answer += ( arr [ i ] - k ) ; arr [ i ] = k ; } answer += ( arr [ i ] + arr [ i + 1 ] ) - k ; arr [ i + 1 ] = ( k - arr [ i ] ) ; answer = ( int ) ( answer % mod ) ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] a = { 9 , 50 , 4 , 14 , 42 , 89 } ; int k = 10 ; int n = a . Length ; Console . Write ( minimum_required_operations ( a , n , k ) ) ; } }
Lexicographic smallest permutation of a String containing the second String as a Substring | C # program to implement the above approach ; Function to print the desired lexicographic smaller String ; Calculate length of the String ; Stores the frequencies of characters of String str1 ; Stores the frequencies of characters of String str2 ; Decrease the frequency of second String from that of of the first String ; To store the resultant String ; To find the index of first character of the String str2 ; Append the characters in lexicographical order ; Append all the current character ( i + ' a ' ) ; If we reach first character of String str2 append str2 ; Return the resultant String ; Driver Code
using System ; class GFG { static String findSmallestString ( String str1 , String str2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; int n1 = str1 . Length ; int n2 = str2 . Length ; for ( int i = 0 ; i < n1 ; ++ i ) { freq1 [ str1 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < n2 ; ++ i ) { freq2 [ str2 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; ++ i ) { freq1 [ i ] -= freq2 [ i ] ; } String res = " " ; int minIndex = str2 [ 0 ] - ' a ' ; for ( int i = 0 ; i < 26 ; ++ i ) { for ( int j = 0 ; j < freq1 [ i ] ; ++ j ) { res += ( char ) ( i + ' a ' ) ; } if ( i == minIndex ) { res += str2 ; } } return res ; } public static void Main ( String [ ] args ) { String str1 = " geeksforgeeksfor " ; String str2 = " for " ; Console . Write ( findSmallestString ( str1 , str2 ) ) ; } }
Determine the winner of a game of deleting Characters from a String | C # program for the above approach ; Function to find the winner of the game when both players play optimally ; Stores the frequency of all digit ; Stores the scores of player1 and player2 respectively ; Iterate to store frequencies ; Turn for the player1 ; Add score of player1 ; Add score of player2 ; Check if its a draw ; If score of player 1 is greater ; Otherwise ; Driver code
using System ; class GFG { static void determineWinner ( String str ) { int [ ] A = new int [ 10 ] ; int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { A [ ( int ) str [ i ] - 48 ] ++ ; } for ( int i = 0 ; i <= 9 ; i ++ ) { if ( i % 2 != 0 ) { sum1 = sum1 + A [ i ] ; } else { sum2 = sum2 + A [ i ] ; } } if ( sum1 == sum2 ) { Console . Write ( " - 1" ) ; } else if ( sum1 > sum2 ) { Console . Write ( " Player ▁ 1" ) ; } else { Console . Write ( " Player ▁ 2" ) ; } } public static void Main ( String [ ] args ) { String str = "78787" ; determineWinner ( str ) ; } }
Generate a String from given Strings P and Q based on the given conditions | C # Program to implement the above approach ; Function to generate a string S from string P and Q according to the given conditions ; Stores the frequencies ; Counts occurrences of each characters ; Reduce the count of the character which is also present in Q ; Stores the resultant string ; Index in [ ] freq to segregate the string ; Add Q to the resulting string ; Driver Code ; Function call
using System ; class GFG { public static void manipulateStrings ( String P , String Q ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < P . Length ; i ++ ) { freq [ P [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < Q . Length ; i ++ ) { freq [ Q [ i ] - ' a ' ] -- ; } String sb = " " ; int pos = Q [ 0 ] - ' a ' ; for ( int i = 0 ; i <= pos ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb += c ; freq [ i ] -- ; } } sb += Q ; for ( int i = pos + 1 ; i < 26 ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb += c ; freq [ i ] -- ; } } Console . WriteLine ( sb ) ; } public static void Main ( String [ ] args ) { String P = " geeksforgeeksfor " ; String Q = " for " ; manipulateStrings ( P , Q ) ; } }
Minimum moves required to type a word in QWERTY based keyboard | C # program for the above approach ; Function that calculates the moves required to print the current String ; row1 has qwertyuiop , row2 has asdfghjkl , and row3 has zxcvbnm Store the row number of each character ; String length ; Initialise move to 1 ; Traverse the String ; If current row number is not equal to previous row then increment the moves ; Return the moves ; Driver Code ; Given String str ; Function Call
using System ; class GFG { static int numberMoves ( String s ) { int [ ] row = { 2 , 3 , 3 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 3 , 3 , 1 , 1 , 1 , 1 , 2 , 1 , 1 , 3 , 1 , 3 , 1 , 3 } ; int n = s . Length ; int move = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( row [ s [ i ] - ' a ' ] != row [ s [ i - 1 ] - ' a ' ] ) { move ++ ; } } return move ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; Console . Write ( numberMoves ( str ) ) ; } }
Queries to calculate the Sum of Array elements in the range [ L , R ] having indices as multiple of K | C # program to implement the above approach ; Structure of a Query ; Function to calculate the sum of array elements at indices from range [ L , R ] which are multiples of K for each query ; Stores Prefix Sum ; prefixSum [ i , j ] : Stores the sum from indices [ 0 , j ] which are multiples of i ; If index j is a multiple of i ; Compute prefix sum ; Otherwise ; Traverse each query ; Sum of all indices upto R which are a multiple of K ; Sum of all indices upto L - 1 which are a multiple of K ; Calculate the difference ; Driver Code
using System ; class GFG { class Node { public int L ; public int R ; public int K ; } ; static void kMultipleSum ( int [ ] arr , Node [ ] Query , int N , int Q ) { int [ , ] prefixSum = new int [ N + 1 , N ] ; for ( int i = 1 ; i <= N ; i ++ ) { prefixSum [ i , 0 ] = arr [ 0 ] ; for ( int j = 0 ; j < N ; j ++ ) { if ( j % i == 0 ) { if ( j != 0 ) prefixSum [ i , j ] = arr [ j ] + prefixSum [ i , j - 1 ] ; } else { prefixSum [ i , j ] = prefixSum [ i , j - 1 ] ; } } } for ( int i = 0 ; i < Q ; i ++ ) { int last = prefixSum [ Query [ i ] . K , Query [ i ] . R ] ; int first ; if ( Query [ i ] . L == 0 ) { first = prefixSum [ Query [ i ] . K , Query [ i ] . L ] ; } else { first = prefixSum [ Query [ i ] . K , Query [ i ] . L - 1 ] ; } Console . Write ( last - first + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . Length ; int Q = 2 ; Node [ ] Query = new Node [ Q ] ; for ( int i = 0 ; i < Q ; i ++ ) Query [ i ] = new Node ( ) ; Query [ 0 ] . L = 2 ; Query [ 0 ] . R = 5 ; Query [ 0 ] . K = 2 ; Query [ 1 ] . L = 3 ; Query [ 1 ] . R = 5 ; Query [ 1 ] . K = 5 ; kMultipleSum ( arr , Query , N , Q ) ; } }
Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array | C # Program to implement the above approach ; Function to count the distinct strings possible after swapping the prefixes between two possible strings of the array ; Stores the count of unique characters for each index ; Store current string ; Stores the total number of distinct strings possible ; Return the answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int mod = 1000000007 ; public static long countS ( String [ ] str , int n , int m ) { Dictionary < int , HashSet < char > > counts = new Dictionary < int , HashSet < char > > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { counts . Add ( i , new HashSet < char > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = str [ i ] ; for ( int j = 0 ; j < m ; j ++ ) { counts [ j ] . Add ( s [ j ] ) ; } } long result = 1 ; foreach ( int index in counts . Keys ) result = ( result * counts [ index ] . Count ) % mod ; return result ; } public static void Main ( String [ ] args ) { String [ ] str = { "112" , "211" } ; int N = 2 , M = 3 ; Console . WriteLine ( countS ( str , N , M ) ) ; } }
Longest Subarrays having each Array element as the maximum | C # Program to implement the above approach ; Function to find the maximum length of Subarrays for each element of the array having it as the maximum ; Initialize the bounds ; Iterate to find greater element on the left ; If greater element is found ; Decrement left pointer ; If boundary is exceeded ; Iterate to find greater element on the right ; If greater element is found ; Increment right pointer ; If boundary is exceeded ; Length of longest subarray where arr [ i ] is the largest ; Print the answer ; Driver Code
using System ; class GFG { static void solve ( int n , int [ ] arr ) { int i , ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { int left = Math . Max ( i - 1 , 0 ) ; int right = Math . Min ( n - 1 , i + 1 ) ; while ( left >= 0 ) { if ( arr [ left ] > arr [ i ] ) { left ++ ; break ; } left -- ; } if ( left < 0 ) left ++ ; while ( right < n ) { if ( arr [ right ] > arr [ i ] ) { right -- ; break ; } right ++ ; } if ( right >= n ) right -- ; ans = 1 + right - left ; Console . Write ( ans + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 1 } ; int n = arr . Length ; solve ( n , arr ) ; } }
Count of Substrings having Sum equal to their Length | C # program to implement the above approach ; Function to count the number of subStrings with sum equal to length ; Stores the count of subStrings ; Add character to sum ; Add count of subStrings to result ; Increase count of subarrays ; Return count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countSubStrings ( String s , int n ) { int count = 0 , sum = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; mp . Add ( 0 , 1 ) ; for ( int i = 0 ; i < n ; ++ i ) { sum += ( s [ i ] - '0' ) ; count += mp . ContainsKey ( sum - ( i + 1 ) ) == true ? mp [ sum - ( i + 1 ) ] : 0 ; if ( ! mp . ContainsKey ( sum - ( i + 1 ) ) ) mp . Add ( sum - ( i + 1 ) , 1 ) ; else mp [ sum - ( i + 1 ) ] = mp [ sum - ( i + 1 ) ] + 1 ; } return count ; } public static void Main ( String [ ] args ) { String str = "112112" ; int n = str . Length ; Console . Write ( countSubStrings ( str , n ) + " STRNEWLINE " ) ; } }
Count of ways to split an Array into three contiguous Subarrays having increasing Sum | C # Program to implement the above approach ; Function to count the number of ways to split array into three contiguous subarrays of the required type ; Stores the prefix sums ; Stores the suffix sums ; Traverse the given array ; Updating curr_subarray_sum until it is less than prefix_sum [ s - 1 ] ; Increase count ; Decrease curr_subarray_sum by arr [ s [ ] ; Return count ; Driver Code
using System ; class GFG { static int findCount ( int [ ] arr , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; int [ ] suffix_sum = new int [ n ] ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; int s = 1 , e = 1 ; int curr_subarray_sum = 0 , count = 0 ; while ( s < n - 1 && e < n - 1 ) { while ( e < n - 1 && curr_subarray_sum < prefix_sum [ s - 1 ] ) { curr_subarray_sum += arr [ e ++ ] ; } if ( curr_subarray_sum <= suffix_sum [ e ] ) { count ++ ; } curr_subarray_sum -= arr [ s ++ ] ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 1 , 7 } ; int n = arr . Length ; Console . WriteLine ( findCount ( arr , n ) ) ; } }
Minimum number of operations required to maximize the Binary String | C # program to implement the above approach ; Function to find the number of operations required ; Count of 1 's ; Count of 0 's upto (cnt1)-th index ; Return the answer ; Driver Code
using System ; class GFG { static int minOperation ( String s , int n ) { int cnt1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) cnt1 ++ ; } int cnt0 = 0 ; for ( int i = 0 ; i < cnt1 ; i ++ ) { if ( s [ i ] == '0' ) cnt0 ++ ; } return cnt0 ; } public static void Main ( String [ ] args ) { int n = 8 ; String s = "01001011" ; int ans = minOperation ( s , n ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }
Minimum number of operations required to maximize the Binary String | C # program to implement the above approach ; Function to find the number of operations required ; Swap 0 ' s ▁ and ▁ 1' s ; Return the answer ; Driver code
using System ; class GFG { static int minOperation ( String s , int n ) { int ans = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( s [ i ] == '0' && s [ j ] == '1' ) { ans ++ ; i ++ ; j -- ; continue ; } if ( s [ i ] == '1' ) { i ++ ; } if ( s [ j ] == '0' ) { j -- ; } } return ans ; } public static void Main ( String [ ] args ) { int n = 8 ; String s = "10100101" ; Console . WriteLine ( minOperation ( s , n ) ) ; } }
Count of N digit Numbers having no pair of equal consecutive Digits | C # Program to implement of the above approach ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Calculate the total count of valid ( i - 1 ) - digit numbers ; Update [ , ] dp table ; Calculate the count of required N - digit numbers ; Driver Code
using System ; class GFG { public static void count ( int N ) { if ( N == 1 ) { Console . WriteLine ( 10 ) ; return ; } int [ , ] dp = new int [ N , 10 ] ; for ( int i = 1 ; i < 10 ; i ++ ) dp [ 0 , i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { int temp = 0 ; for ( int j = 0 ; j < 10 ; j ++ ) temp += dp [ i - 1 , j ] ; for ( int j = 0 ; j < 10 ; j ++ ) dp [ i , j ] = temp - dp [ i - 1 , j ] ; } int ans = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) ans += dp [ N - 1 , i ] ; Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int N = 2 ; count ( N ) ; } }