codeparrot/xlcost-text-to-code huggingface dataset:
Schedule jobs so that each server gets equal load | C # program to schedule jobs so that each server gets equal load . ; Function to find new array a ; find sum S of both arrays a and b . ; Single element case . ; This checks whether sum s can be divided equally between all array elements . i . e . whether all elements can take equal value or not . ; Compute possible value of new array elements . ; Possibility 1 ; ensuring that all elements of array b are used . ; If a ( i ) already updated to x move to next element in array a . ; Possibility 2 ; Possibility 3 ; Possibility 4 ; If a ( i ) can not be made equal to x even after adding all possible elements from b ( i ) then print - 1. ; check whether all elements of b are used . ; Return the new array element value . ; Driver code
using System ; class GFG { static int solve ( int [ ] a , int [ ] b , int n ) { int i ; int s = 0 ; for ( i = 0 ; i < n ; i ++ ) s += ( a [ i ] + b [ i ] ) ; if ( n == 1 ) return a [ 0 ] + b [ 0 ] ; if ( s % n != 0 ) return - 1 ; int x = s / n ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) return - 1 ; if ( i > 0 ) { a [ i ] += b [ i - 1 ] ; b [ i - 1 ] = 0 ; } if ( a [ i ] == x ) continue ; int y = a [ i ] + b [ i ] ; if ( i + 1 < n ) y += b [ i + 1 ] ; if ( y == x ) { a [ i ] = y ; b [ i ] = 0 ; continue ; } if ( a [ i ] + b [ i ] == x ) { a [ i ] += b [ i ] ; b [ i ] = 0 ; continue ; } if ( i + 1 < n && a [ i ] + b [ i + 1 ] == x ) { a [ i ] += b [ i + 1 ] ; b [ i + 1 ] = 0 ; continue ; } return - 1 ; } for ( i = 0 ; i < n ; i ++ ) if ( b [ i ] != 0 ) return - 1 ; return x ; } public static void Main ( String [ ] args ) { int [ ] a = { 6 , 14 , 21 , 1 } ; int [ ] b = { 15 , 7 , 10 , 10 } ; int n = a . Length ; Console . WriteLine ( solve ( a , b , n ) ) ; } }
Check if it is possible to survive on Island | C # program to find the minimum days on which you need to buy food from the shop so that you can survive the next S days ; function to find the minimum days ; If we can not buy at least a week supply of food during the first week OR We can not buy a day supply of food on the first day then we can 't survive. ; If we can survive then we can buy ceil ( A / N ) times where A is total units of food required . ; Driver code
using System ; class GFG { static void survival ( int S , int N , int M ) { if ( ( ( N * 6 ) < ( M * 7 ) && S > 6 ) M > N ) Console . Write ( " No " ) ; else { int days = ( M * S ) / N ; if ( ( ( M * S ) % N ) != 0 ) days ++ ; Console . WriteLine ( " Yes ▁ " + days ) ; } } public static void Main ( ) { int S = 10 , N = 16 , M = 2 ; survival ( S , N , M ) ; } }
Lexicographically largest subsequence such that every character occurs at least k times | C # program to find lexicographically largest subsequence where every character appears at least k times . ; Find lexicographically largest subsequence of s [ 0. . n - 1 ] such that every character appears at least k times . The result is filled in t [ ] ; Starting from largest character ' z ' to ' a ' ; Counting the frequency of the character ; If frequency is greater than k ; From the last point we leave ; check if string contain ch ; If yes , append to output string ; Update the last point . ; Driver code
using System ; class GFG { static void subsequence ( char [ ] s , char [ ] t , int n , int k ) { int last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( char ch = ' z ' ; ch >= ' a ' ; ch -- ) { cnt = 0 ; for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) cnt ++ ; } if ( cnt >= k ) { for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) { t [ size ++ ] = ch ; new_last = i ; } } last = new_last ; } } t [ size ] = ' \0' ; } public static void Main ( ) { char [ ] s = { ' b ' , ' a ' , ' n ' , ' a ' , ' n ' , ' a ' } ; int n = s . Length ; int k = 2 ; char [ ] t = new char [ n ] ; subsequence ( s , t , n - 1 , k ) ; for ( int i = 0 ; i < t . Length ; i ++ ) if ( t [ i ] != 0 ) Console . Write ( t [ i ] ) ; } }
Program for Least Recently Used ( LRU ) Page Replacement algorithm | C # implementation of above algorithm ; Method to find page faults using indexes ; To represent set of current pages . We use an unordered_set so that we quickly check if a page is present in set or not ; To store least recently used indexes of pages . ; Start from initial page ; Check if the set can hold more pages ; Insert it into set if not present already which represents page fault ; increment page fault ; Store the recently used index of each page ; If the set is full then need to perform lru i . e . remove the least recently used page and insert the current page ; Check if current page is not already present in the set ; Find the least recently used pages that is present in the set ; Remove the indexes page ; remove lru from hashmap ; insert the current page ; Increment page faults ; Update the current page index ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int pageFaults ( int [ ] pages , int n , int capacity ) { HashSet < int > s = new HashSet < int > ( capacity ) ; Dictionary < int , int > indexes = new Dictionary < int , int > ( ) ; int page_faults = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . Count < capacity ) { if ( ! s . Contains ( pages [ i ] ) ) { s . Add ( pages [ i ] ) ; page_faults ++ ; } if ( indexes . ContainsKey ( pages [ i ] ) ) indexes [ pages [ i ] ] = i ; else indexes . Add ( pages [ i ] , i ) ; } else { if ( ! s . Contains ( pages [ i ] ) ) { int lru = int . MaxValue , val = int . MinValue ; foreach ( int itr in s ) { int temp = itr ; if ( indexes [ temp ] < lru ) { lru = indexes [ temp ] ; val = temp ; } } s . Remove ( val ) ; indexes . Remove ( val ) ; s . Add ( pages [ i ] ) ; page_faults ++ ; } if ( indexes . ContainsKey ( pages [ i ] ) ) indexes [ pages [ i ] ] = i ; else indexes . Add ( pages [ i ] , i ) ; } } return page_faults ; } public static void Main ( String [ ] args ) { int [ ] pages = { 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 } ; int capacity = 4 ; Console . WriteLine ( pageFaults ( pages , pages . Length , capacity ) ) ; } }
Largest permutation after at most k swaps | C # Program to find the largest permutation after at most k swaps using unordered_map . ; Function to find the largest permutation after k swaps ; Storing the elements and their index in map ; If number of swaps allowed are equal to number of elements then the resulting permutation will be descending order of given permutation . ; if j is not at it 's best index ; Change the index of the element which was at position 0. Swap the element basically . ; decrement number of swaps ; Driver code ; K is the number of swaps ; n is the size of the array ; Function calling
using System ; using System . Collections . Generic ; class GFG { static void bestpermutation ( List < int > arr , int k , int n ) { Dictionary < int , int > h = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { h . Add ( arr [ i ] , i ) ; } if ( n <= k ) { arr . Sort ( ) ; arr . Reverse ( ) ; } else { for ( int j = n ; j >= 1 ; j -- ) { if ( k > 0 ) { int initial_index = h [ j ] ; int best_index = n - j ; if ( initial_index != best_index ) { h [ j ] = best_index ; int element = arr [ best_index ] ; h [ element ] = initial_index ; int temp = arr [ best_index ] ; arr [ best_index ] = arr [ initial_index ] ; arr [ initial_index ] = temp ; k -- ; } } } } } static void Main ( ) { List < int > arr = new List < int > ( new int [ ] { 3 , 1 , 4 , 2 , 5 } ) ; int k = 10 ; int n = arr . Count ; bestpermutation ( arr , k , n ) ; Console . Write ( " Largest ▁ possible ▁ permutation ▁ after ▁ " + k + " ▁ swaps ▁ is ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Program for Best Fit algorithm in Memory Management | C # implementation of Best - Fit algorithm ; Method to allocate memory to blocks as per Best fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Find the best fit block for current process ; If we could find a block for current process ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver Method
using System ; public class GFG { static void bestFit ( int [ ] blockSize , int m , int [ ] processSize , int n ) { int [ ] allocation = new int [ n ] ; for ( int i = 0 ; i < allocation . Length ; i ++ ) allocation [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int bestIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( bestIdx == - 1 ) bestIdx = j ; else if ( blockSize [ bestIdx ] > blockSize [ j ] ) bestIdx = j ; } } if ( bestIdx != - 1 ) { allocation [ i ] = bestIdx ; blockSize [ bestIdx ] -= processSize [ i ] ; } } Console . WriteLine ( " STRNEWLINE Process ▁ No . TABSYMBOL Process " + " ▁ Size TABSYMBOL Block ▁ no . " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( " ▁ " + ( i + 1 ) + " TABSYMBOL TABSYMBOL " + processSize [ i ] + " TABSYMBOL TABSYMBOL " ) ; if ( allocation [ i ] != - 1 ) Console . Write ( allocation [ i ] + 1 ) ; else Console . Write ( " Not ▁ Allocated " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int [ ] blockSize = { 100 , 500 , 200 , 300 , 600 } ; int [ ] processSize = { 212 , 417 , 112 , 426 } ; int m = blockSize . Length ; int n = processSize . Length ; bestFit ( blockSize , m , processSize , n ) ; } }
Bin Packing Problem ( Minimize number of used Bins ) | C # program to find number of bins required using Best fit algorithm . ; Returns number of bins required using best fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that can accommodate weight [ i ] ; Initialize minimum space left and index of best bin ; If no bin could accommodate weight [ i ] , create a new bin ; Assign the item to best bin ; Driver code
using System ; class GFG { static int bestFit ( int [ ] weight , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int min = c + 1 , bi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] < min ) { bi = j ; min = bin_rem [ j ] - weight [ i ] ; } } if ( min == c + 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } else bin_rem [ bi ] -= weight [ i ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] weight = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . Length ; Console . Write ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Best ▁ Fit ▁ : ▁ " + bestFit ( weight , n , c ) ) ; } }
Bin Packing Problem ( Minimize number of used Bins ) | C # program to find number of bins required using Worst fit algorithm . ; Returns number of bins required using worst fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that ca \ n accommodate weight [ i ] ; Initialize maximum space left and index of worst bin ; If no bin could accommodate weight [ i ] , create a new bin ; else Assign the item to best bin ; Driver program
using System ; class GFG { static int worstFit ( int [ ] weight , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int mx = - 1 , wi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] > mx ) { wi = j ; mx = bin_rem [ j ] - weight [ i ] ; } } if ( mx == - 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ wi ] -= weight [ i ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] weight = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . Length ; Console . Write ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Worst ▁ Fit ▁ : ▁ " + worstFit ( weight , n , c ) ) ; } }
Find minimum time to finish all jobs with given constraints | C # program to find minimum time to finish all jobs with given number of assignees ; Utility function to get maximum element in job [ 0. . n - 1 ] ; Returns true if it is possible to finish jobs [ ] within given time ' time ' ; cnt is count of current assignees required for jobs ; time assigned to current assignee ; If time assigned to current assignee exceeds max , increment count of assignees . ; Else add time of job to current time and move to next job . ; Returns true if count is smaller than k ; Returns minimum time required to finish given array of jobs k -- > number of assignees T -- > Time required by every assignee to finish 1 unit m -- > Number of jobs ; Set start and end for binary search end provides an upper limit on time ; Initialize answer ; Find the job that takes maximum time ; Do binary search for minimum feasible time ; If it is possible to finish jobs in mid time ; Update answer ; Driver program
using System ; class GFG { static int getMax ( int [ ] arr , int n ) { int result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > result ) result = arr [ i ] ; return result ; } static bool isPossible ( int time , int K , int [ ] job , int n ) { int cnt = 1 ; int curr_time = 0 ; for ( int i = 0 ; i < n ; ) { if ( curr_time + job [ i ] > time ) { curr_time = 0 ; cnt ++ ; } else { curr_time += job [ i ] ; i ++ ; } } return ( cnt <= K ) ; } static int findMinTime ( int K , int T , int [ ] job , int n ) { int end = 0 , start = 0 ; for ( int i = 0 ; i < n ; ++ i ) end += job [ i ] ; int ans = end ; int job_max = getMax ( job , n ) ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid >= job_max && isPossible ( mid , K , job , n ) ) { ans = Math . Min ( ans , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return ( ans * T ) ; } public static void Main ( ) { int [ ] job = { 10 , 7 , 8 , 12 , 6 , 8 } ; int n = job . Length ; int k = 4 , T = 5 ; Console . WriteLine ( findMinTime ( k , T , job , n ) ) ; } }
Longest subarray with all even or all odd elements | C # program for the above approach ; Function to calculate longest substring with odd or even elements ; Initializing dp [ ] ; Initializing dp [ 0 ] with 1 ; ans will store the final answer ; Traversing the array from index 1 to N - 1 ; Checking both current and previous element is even or odd ; Updating dp [ i ] with dp [ i - 1 ] + 1 ; Storing max element to ans ; Returning the final answer ; Driver Code ; Input ; Function call
using System ; class GFG { static int LongestOddEvenSubarray ( int [ ] A , int N ) { int [ ] dp = new int [ N ] ; dp [ 0 ] = 1 ; int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 == 0 && A [ i - 1 ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ i - 1 ] % 2 != 0 ) ) { dp [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = 1 ; } for ( int i = 0 ; i < N ; i ++ ) ans = Math . Max ( ans , dp [ i ] ) ; return ans ; } public static void Main ( ) { int [ ] A = { 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 } ; int N = A . Length ; Console . Write ( LongestOddEvenSubarray ( A , N ) ) ; } }
Count of subarrays with maximum value as K | C # program for the above approach ; Function to count the subarrays with maximum not greater than K ; If arr [ i ] > k then arr [ i ] cannot be a part of any subarray . ; Count the number of elements where arr [ i ] is not greater than k . ; Summation of all possible subarrays in the variable ans . ; Function to count the subarrays with maximum value is equal to K ; Stores count of subarrays with max <= k - 1. ; Stores count of subarrays with max >= k + 1. ; Stores count of subarrays with max = k . ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int totalSubarrays ( int [ ] arr , int n , int k ) { int ans = 0 , i = 0 ; while ( i < n ) { if ( arr [ i ] > k ) { i ++ ; continue ; } int count = 0 ; while ( i < n && arr [ i ] <= k ) { i ++ ; count ++ ; } ans += ( ( count * ( count + 1 ) ) / 2 ) ; } return ans ; } static int countSubarrays ( int [ ] arr , int n , int k ) { int count1 = totalSubarrays ( arr , n , k - 1 ) ; int count2 = totalSubarrays ( arr , n , k ) ; int ans = count2 - count1 ; return ans ; } static void Main ( ) { int n = 4 , k = 3 ; int [ ] arr = { 2 , 1 , 3 , 4 } ; Console . WriteLine ( countSubarrays ( arr , n , k ) ) ; } }
Maximum subsequence sum such that no three are consecutive in O ( 1 ) space | C # program for the above approach ; Function to calculate the maximum subsequence sum such that no three elements are consecutive ; when N is 1 , answer would be the only element present ; when N is 2 , answer would be sum of elements ; variable to store sum up to i - 3 ; variable to store sum up to i - 2 ; variable to store sum up to i - 1 ; variable to store the final sum of the subsequence ; find the maximum subsequence sum up to index i ; update first , second and third ; return ans ; ; Driver Code ; Input ; Function call
using System ; class GFG { public static int maxSumWO3Consec ( int [ ] A , int N ) { if ( N == 1 ) return A [ 0 ] ; if ( N == 2 ) return A [ 0 ] + A [ 1 ] ; int third = A [ 0 ] ; int second = third + A [ 1 ] ; int first = Math . Max ( second , A [ 1 ] + A [ 2 ] ) ; int sum = Math . Max ( Math . Max ( third , second ) , first ) ; for ( int i = 3 ; i < N ; i ++ ) { sum = Math . Max ( Math . Max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] ) ; third = second ; second = first ; first = sum ; } return sum ; } public static void Main ( ) { int [ ] A = { 3000 , 2000 , 1000 , 3 , 10 } ; int N = A . Length ; int res = maxSumWO3Consec ( A , N ) ; Console . Write ( res ) ; } }
Minimum number of given operations required to reduce a number to 2 | C # program for the above approach ; Function to find the minimum number of operations required to reduce n to 2 ; Initialize a dp array ; Handle the base case ; Iterate in the range [ 2 , n ] ; Check if i * 5 <= n ; Check if i + 3 <= n ; Return the result ; Driver code ; Given Input ; Function Call ; Print the result
using System ; using System . Collections . Generic ; class GFG { static int findMinOperations ( int n ) { int i ; int [ ] dp = new int [ n + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = 999999 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i < n + 1 ; i ++ ) { if ( i * 5 <= n ) { dp [ i * 5 ] = Math . Min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= n ) { dp [ i + 3 ] = Math . Min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } return dp [ n ] ; } public static void Main ( ) { int n = 28 ; int m = findMinOperations ( n ) ; if ( m != 9999 ) Console . Write ( m ) ; else Console . Write ( - 1 ) ; } }
Number of M | C # program for the above approach ; Function to find the number of M - length sorted arrays possible using numbers from the range [ 1 , N ] ; Create an array of size M + 1 ; Base cases ; Fill the dp table ; dp [ j ] will be equal to maximum number of sorted array of size j when elements are taken from 1 to i ; Here dp [ m ] will be equal to the maximum number of sorted arrays when element are taken from 1 to i ; Return the result ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int countSortedArrays ( int n , int m ) { int [ ] dp = new int [ ( m + 1 ) ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ j ] = dp [ j - 1 ] + dp [ j ] ; } } return dp [ m ] ; } public static void Main ( ) { int n = 2 , m = 3 ; Console . WriteLine ( countSortedArrays ( n , m ) ) ; } }
Split array into subarrays such that sum of difference between their maximums and minimums is maximum | C # program for the above approach ; Function to find maximum sum of difference between maximums and minimums in the splitted subarrays ; Base Case ; Traverse the array ; Stores the maximum and minimum elements upto the i - th index ; Traverse the range [ 0 , i ] ; Update the minimum ; Update the maximum ; Update dp [ i ] ; Return the maximum sum of difference ; Driver Code
using System ; class GFG { static int getValue ( int [ ] arr , int N ) { int [ ] dp = new int [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { int min = arr [ i ] ; int max = arr [ i ] ; for ( int j = i ; j >= 0 ; j -- ) { min = Math . Min ( arr [ j ] , min ) ; max = Math . Max ( arr [ j ] , max ) ; dp [ i ] = Math . Max ( dp [ i ] , max - min + ( ( j >= 1 ) ? dp [ j - 1 ] : 0 ) ) ; } } return dp [ N - 1 ] ; } static public void Main ( ) { int [ ] arr = { 8 , 1 , 7 , 9 , 2 } ; int N = arr . Length ; Console . Write ( getValue ( arr , N ) ) ; } }
Length of longest subset consisting of A 0 s and B 1 s from an array of strings | C # program for the above approach ; Function to count 0 's in a string ; Stores count of 0 s ; Iterate over characters of string ; If current character is '0' ; Recursive function to find the length of longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; If idx is equal to N or A + B is equal to 0 ; Stores the count of 0 's in arr[idx] ; Stores the count of 1 's in arr[idx] ; Stores the length of the subset if arr [ i ] is included ; If zero is less than or equal to A and one is less than or equal to B ; Stores the length of the subset if arr [ i ] is excluded ; Returns max of inc and exc ; Function to find the length of the longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; Return ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int count0 ( string s ) { int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '0' ) { count ++ ; } } return count ; } static int solve ( List < string > vec , int A , int B , int idx ) { if ( idx == vec . Count A + B == 0 ) { return 0 ; } int zero = count0 ( vec [ idx ] ) ; int one = vec [ idx ] . Length - zero ; int inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 ) ; } int exc = solve ( vec , A , B , idx + 1 ) ; return Math . Max ( inc , exc ) ; } static int MaxSubsetlength ( List < string > arr , int A , int B ) { return solve ( arr , A , B , 0 ) ; } public static void Main ( ) { List < string > arr = new List < string > { "1" , "0" , "10" } ; int A = 1 , B = 1 ; Console . WriteLine ( MaxSubsetlength ( arr , A , B ) ) ; } }
Maximum score possible by removing substrings made up of single distinct character | C # program for the above approach ; Function to check if the string s consists of a single distinct character or not ; Function to calculate the maximum score possible by removing substrings ; If string is empty ; If length of string is 1 ; Store the maximum result ; Try to remove all substrings that satisfy the condition and check for resultant string after removal ; Store the substring { s [ i ] , . . , s [ j ] } ; Check if the substring contains only a single distinct character ; Return the maximum score ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isUnique ( string s ) { HashSet < char > set = new HashSet < char > ( ) ; foreach ( char c in s ) set . Add ( c ) ; return set . Count == 1 ; } static int maxScore ( string s , int [ ] a ) { int n = s . Length ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int mx = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { string sub = s . Substring ( i , j + 1 - i ) ; if ( isUnique ( sub ) ) mx = Math . Max ( mx , a [ sub . Length - 1 ] + maxScore ( s . Substring ( 0 , i ) + s . Substring ( j + 1 ) , a ) ) ; } } return mx ; } static void Main ( ) { string s = "011" ; int [ ] a = { 1 , 3 , 1 } ; Console . Write ( maxScore ( s , a ) ) ; } }