codeparrot/xlcost-text-to-code huggingface dataset:
Minimize increment | C # program for the above approach ; Function to calculate the minimum number of operations to convert array A to array B by incrementing and decrementing adjacent elements ; Stores the final count ; Stores the sum of array A and B respectivelly ; Check of the sums are unequall ; Pointer to iterate through array ; Case 1 where A [ i ] > B [ i ] ; Stores the extra values for the current index ; Iterate the array from [ i - 1 , 0 ] ; Stores the count of values being transfered from A [ i ] to A [ j ] ; Add operation count ; Iterate the array in right direction id A [ i ] - B [ i ] > 0 ; Iterate the array from [ i + 1 , n - 1 ] ; Stores the count of values being transfered from A [ i ] to A [ j ] ; Add operation count ; Return Answer ; Driver Code ; Function Call
using System ; public class GFG { static int minimumMoves ( int [ ] A , int [ ] B , int N ) { int ans = 0 ; int sum_A = 0 , sum_B = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum_A += A [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { sum_B += B [ i ] ; } if ( sum_A != sum_B ) { return - 1 ; } int k = 0 ; while ( k < N ) { if ( A [ k ] > B [ k ] ) { int temp = A [ k ] - B [ k ] ; int j = k - 1 ; while ( j >= 0 && temp > 0 ) { if ( B [ j ] > A [ j ] ) { int cnt = Math . Min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * Math . Abs ( j - k ) ) ; } j -- ; } if ( temp > 0 ) { j = k + 1 ; while ( j < N && temp > 0 ) { if ( B [ j ] > A [ j ] ) { int cnt = Math . Min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * Math . Abs ( j - k ) ) ; } j ++ ; } } } k ++ ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] A = { 1 , 5 , 7 } ; int [ ] B = { 13 , 0 , 0 } ; int N = A . Length ; Console . WriteLine ( minimumMoves ( A , B , N ) ) ; } }
Check if X can be reduced to 0 in exactly T moves by substracting D or 1 from it | C # program for the above approach ; Function to check the above problem condition ; Check for base cases ; Check if D - 1 is a divisor of X - T ; Driver Code
using System ; public class GFG { static int possibleReachingSequence ( int X , int D , int T ) { if ( X < T ) { Console . WriteLine ( " NO " ) ; return 0 ; } if ( T * D < X ) { Console . WriteLine ( " NO " ) ; return 0 ; } if ( ( X - T ) % ( D - 1 ) == 0 ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } return 0 ; } public static void Main ( string [ ] args ) { int X = 10 , D = 3 , T = 6 ; possibleReachingSequence ( X , D , T ) ; } }
Maximum number of times Array can be reduced in half when its all elements are even | C # code implementation for the above approach ; Function to return the number of operations possible ; counter to store the number of times the current element is divisible by 2 ; variable to store the final answer ; Initialize the counter to zero for each element ; update the counter till the number is divisible by 2 ; update the answer as the minimum of all the counts ; Driver code
using System ; class GFG { static int arrayDivisionByTwo ( int [ ] arr , int n ) { int cnt = 0 ; int ans = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { cnt = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = arr [ i ] / 2 ; cnt ++ ; } ans = Math . Min ( ans , cnt ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 12 , 40 } ; int n = arr . Length ; Console . Write ( arrayDivisionByTwo ( arr , n ) ) ; } }
Find smallest number with given digits and sum of digits | C # program for the above approach ; Function to print the minimum integer having only digits P and Q and the sum of digits as N ; If Q is greater that P then swap the values of P and Q ; If P and Q are both zero or if Q is zero and N is not divisible by P then there is no possible integer which satisfies the given conditions ; Loop to find the maximum value of count_P that also satisfy P * count_P + Q * count_Q = N ; If N is 0 , their is a valid integer possible that satisfies all the requires conditions ; Print Answer ; Driver code ; Function Call
using System ; public class GFG { static void printMinint ( int P , int Q , int N ) { if ( Q > P ) { int temp ; temp = P ; P = Q ; Q = temp ; } if ( Q == 0 && ( P == 0 N % P != 0 ) ) { Console . WriteLine ( " Not ▁ Possible " ) ; return ; } int count_P = 0 , count_Q = 0 ; while ( N > 0 ) { if ( N % P == 0 ) { count_P += N / P ; N = 0 ; } else { N = N - Q ; count_Q ++ ; } } if ( N == 0 ) { for ( int i = 0 ; i < count_Q ; i ++ ) Console . Write ( Q ) ; for ( int i = 0 ; i < count_P ; i ++ ) Console . Write ( P ) ; } else { Console . WriteLine ( " Not ▁ Possible " ) ; } } public static void Main ( String [ ] args ) { int N = 32 ; int P = 7 ; int Q = 4 ; printMinint ( P , Q , N ) ; } }
Minimum number of sum and modulo operations using given numbers to reach target | C # implementation of the above approach ; Function to find the minimum moves to reach K from N ; Initialization of dp vector ; dp [ i ] = minimum pushes required to reach i ; Traversing through the buttons ; Iterating through all the positions ; If not visited ; Next status of lock ; Advance to next state ; Return the readonly dp [ target ] ; Driver function ; Given Input ; Function Call
using System ; public class GFG { static int minPushes ( int N , int K , int [ ] arr ) { int [ ] dp = new int [ 100000 ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = - 1 ; dp [ N ] = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { for ( int xx = 0 ; xx < 100000 ; xx ++ ) { int x = xx ; if ( dp [ x ] == - 1 ) continue ; int next = ( x + arr [ i ] ) % 100000 ; while ( dp [ next ] == - 1 dp [ next ] > dp [ x ] + 1 ) { dp [ next ] = dp [ x ] + 1 ; x = next ; next = ( next + arr [ i ] ) % 100000 ; } } } return dp [ K ] ; } public static void Main ( String [ ] args ) { int N = 99880 , K = 89 ; int [ ] arr = { 100 , 3 } ; Console . Write ( minPushes ( N , K , arr ) ) ; } }
Minimum number of Apples to be collected from trees to guarantee M red apples | C # program for the above approach ; Function to minimum no . of apples ; If we get all required apple from South ; If we required trees at East and West ; If we doesn 't have enough  red apples ; Driver code ; No . of red apple for gift ; No . of red apple in each tree ; No . of tree in North ; No . of tree in South ; No . of tree in West ; No . of tree in East ; Function Call
using System ; class GFG { static int minApples ( int M , int K , int N , int S , int W , int E ) { if ( M <= S * K ) return M ; else if ( M <= S * K + E + W ) return S * K + ( M - S * K ) * K ; else return - 1 ; } public static void Main ( String [ ] args ) { int M = 10 ; int K = 15 ; int N = 0 ; int S = 1 ; int W = 0 ; int E = 0 ; int ans = minApples ( M , K , N , S , W , E ) ; Console . Write ( ans ) ; } }
Minimum increments or decrements required to signs of prefix sum array elements alternating | C # code for above approach ; Function to find the minimum number of increments / decrements of array elements by 1 to make signs of prefix sum array elements alternating ; Case 1. neg - pos - neg ; Stores the current sign of the prefix sum of array ; Stores minimum number of operations for Case 1 ; Traverse the array ; Checking both conditions ; Update the current prefix1 to currentPrefixSum ; Case 2. pos - neg - pos ; Stores the prefix sum of array ; Stores minimum number of operations for Case 1 ; Checking both conditions ; Update the current prefix2 to currentPrefixSum ; Driver Code
using System ; public class GFG { static int minimumOperations ( int [ ] A , int N ) { int cur_prefix_1 = 0 ; int parity = - 1 ; int minOperationsCase1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_prefix_1 += A [ i ] ; if ( cur_prefix_1 == 0 parity * cur_prefix_1 < 0 ) { minOperationsCase1 += Math . Abs ( parity - cur_prefix_1 ) ; cur_prefix_1 = parity ; } parity *= - 1 ; } int cur_prefix_2 = 0 ; parity = 1 ; int minOperationsCase2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_prefix_2 += A [ i ] ; if ( cur_prefix_2 == 0 parity * cur_prefix_2 < 0 ) { minOperationsCase2 += Math . Abs ( parity - cur_prefix_2 ) ; cur_prefix_2 = parity ; } parity *= - 1 ; } return Math . Min ( minOperationsCase1 , minOperationsCase2 ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , - 3 , 1 , 0 } ; int N = A . Length ; Console . Write ( minimumOperations ( A , N ) ) ; } }
Generate an array of maximum sum such that each element exceeds all elements present either on its left or right | C # code for the above approach ; Function to construct the array having maximum sum satisfying the given criteria ; Declaration of the array arrA [ ] and ans [ ] ; Stores the maximum sum of the resultant array ; Initialize the array arrA [ ] ; Traversing the array arrA [ ] ; Initialize the array arrB [ ] ; Assign the maximum element to the current element ; Form the first increasing till every index i ; Make the current element as the maximum element ; Forming decreasing from the index i + 1 to the index N ; Initialize the sum ; Find the total sum ; Check if the total sum is at least the sum found then make ans as ansB ; Print the final array formed ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maximumSumArray ( int [ ] arr , int N ) { int [ ] arrA = new int [ N ] ; int [ ] ans = new int [ N ] ; int maxSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) arrA [ i ] = arr [ i ] ; for ( int i = 0 ; i < N ; i ++ ) { int [ ] arrB = new int [ N ] ; int maximum = arrA [ i ] ; arrB [ i ] = maximum ; for ( int j = i - 1 ; j >= 0 ; j -- ) { arrB [ j ] = Math . Min ( maximum , arrA [ j ] ) ; maximum = arrB [ j ] ; } maximum = arrA [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { arrB [ j ] = Math . Min ( maximum , arrA [ j ] ) ; maximum = arrB [ j ] ; } int sum = 0 ; for ( int j = 0 ; j < N ; j ++ ) sum += arrB [ j ] ; if ( sum > maxSum ) { maxSum = sum ; ans = arrB ; } } foreach ( int val in ans ) { Console . Write ( val + " ▁ " ) ; } } public static void Main ( ) { int [ ] A = { 10 , 6 , 8 } ; int N = A . Length ; maximumSumArray ( A , N ) ; } }
Minimize product of two scores possible by at most M reductions | C # program for the above approach ; Utility function to find the minimum product of R1 and R2 possible ; Reaching to its limit ; If M is remaining ; Function to find the minimum product of R1 and R2 ; Case 1 - R1 reduces first ; Case 2 - R2 reduces first ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int minProductUtil ( int R1 , int B1 , int R2 , int B2 , int M ) { int x = Math . Min ( R1 - B1 , M ) ; M -= x ; R1 -= x ; if ( M > 0 ) { int y = Math . Min ( R2 - B2 , M ) ; M -= y ; R2 -= y ; } return R1 * R2 ; } static int minProduct ( int R1 , int B1 , int R2 , int B2 , int M ) { int res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) ; int res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) ; return Math . Min ( res1 , res2 ) ; } public static void Main ( String [ ] args ) { int R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ; Console . Write ( ( minProduct ( R1 , B1 , R2 , B2 , M ) ) ) ; } }
Maximize the profit after selling the tickets | Set 2 ( For elements in range [ 1 , 10 ^ 6 ] ) | C # program for the above approach ; Function to find maximum profit after selling K tickets ; Frequency array to store freq of every element of the array ; Modify the arr [ ] so that the array is sorted in O ( N ) ; Variable to store answer ; Traverse the array while K > 0 and j >= 0 ; If arr [ i ] > arr [ j ] then ticket can be brought from counter [ j + 1 , N ] ; If arr [ j ] = = arr [ i ] decrement j until arr [ j ] != arr [ i ] ; Sell tickets from counter [ j + 1 , N ] ; All elements of array are equal Send tickets from each counter 1 time until K > 0. ; Converting answer from long long to int ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int maxAmount ( int n , int k , int [ ] arr ) { int i ; int [ ] A = new int [ 1000001 ] ; Array . Clear ( A , 0 , 1000001 ) ; for ( i = 0 ; i < n ; i ++ ) { A [ arr [ i ] ] ++ ; } int j = 0 ; for ( i = 0 ; i < 1000001 ; i ++ ) { while ( A [ i ] != 0 ) { arr [ j ++ ] = i ; A [ i ] -- ; } } int ans = 0 ; int mod = 1000000007 ; i = n - 1 ; j = n - 2 ; while ( k > 0 && j >= 0 ) { if ( arr [ i ] > arr [ j ] ) { ans = ans + Math . Min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } else { while ( j >= 0 && arr [ j ] == arr [ i ] ) j -- ; if ( j < 0 ) break ; ans = ans + Math . Min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } } while ( k > 0 && arr [ i ] != 0 ) { ans = ans + Math . Min ( n , k ) * arr [ i ] ; k -= n ; arr [ i ] -- ; } ans = ans % mod ; int x = ans ; return x ; } public static void Main ( ) { int n = 5 ; int k = 3 ; int [ ] arr = { 4 , 3 , 6 , 2 , 4 } ; int ans = maxAmount ( n , k , arr ) ; Console . Write ( ans ) ; } }
Maximize sum of averages of subsequences of lengths lying in a given range | C # program for the above approach ; Function to find the maximum sum of average of groups ; Sort the given array ; Stores the sum of averages ; Stores count of array element ; Add the current value to the variable sum ; Increment the count by 1 ; If the current size is X ; If the remaining elements can 't become a group ; Iterate until i is less than N ; Update the value of X ; Update the average ; Find the average ; Reset the sum and count ; Print maximum sum of averages ; Driver Code
using System ; public class GFG { static void maxAverage ( int [ ] A , int N , int X , int Y ) { Array . Sort ( A ) ; int sum = 0 ; double res = 0 ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; count ++ ; if ( count == X ) { if ( N - i - 1 < X ) { i ++ ; int cnt = 0 ; while ( i < N ) { cnt ++ ; sum += A [ i ] ; i ++ ; } X = X + cnt ; res += ( double ) sum / ( double ) ( X ) ; break ; } res += ( double ) sum / ( double ) ( X ) ; sum = 0 ; count = 0 ; } } Console . WriteLine ( res ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 10 , 6 , 5 } ; int N = A . Length ; int X = 2 , Y = 3 ; maxAverage ( A , N , X , Y ) ; } }
Rearrange characters in a sorted string such that no pair of adjacent characters are the same | C # program for the above approach ; Function to check if a String S contains pair of adjacent characters that are equal or not ; Traverse the String S ; If S [ i ] and S [ i + 1 ] are equal ; Otherwise , return false ; Function to rearrange characters of a String such that no pair of adjacent characters are the same ; Initialize 3 variables ; Iterate until k < N ; If S [ i ] is not equal to S [ j ] ; Increment i and j by 1 ; If j equals k and increment the value of K by 1 ; Else ; If S [ j ] equals S [ k ] ; Increment k by 1 ; Else ; Swap ; Increment i and j by 1 ; If j equals k ; Increment k by 1 ; Function to rearrange characters in a String so that no two adjacent characters are same ; If String is already valid ; If size of the String is 2 ; Function Call ; Reversing the String ; Function Call ; If the String is valid ; Otherwise ; Driver Code
using System ; public class GFG { static char [ ] S = " aaabc " . ToCharArray ( ) ; static bool isAdjChar ( ) { for ( int i = 0 ; i < S . Length - 1 ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) return true ; } return false ; } static void rearrangeStringUtil ( int N ) { int i = 0 , j = 1 , k = 2 ; while ( k < N ) { if ( S [ i ] != S [ j ] ) { i ++ ; j ++ ; if ( j == k ) { k ++ ; } } else { if ( S [ j ] == S [ k ] ) { k ++ ; } else { swap ( k , j ) ; i ++ ; j ++ ; if ( j == k ) { k ++ ; } } } } } static void swap ( int i , int j ) { char temp = S [ i ] ; S [ i ] = S [ j ] ; S [ j ] = temp ; } static String rearrangeString ( int N ) { if ( isAdjChar ( ) == false ) { return String . Join ( " " , S ) ; } if ( S . Length == 2 ) return " - 1" ; rearrangeStringUtil ( N ) ; reverse ( ) ; rearrangeStringUtil ( N ) ; if ( isAdjChar ( ) == false ) { return String . Join ( " " , S ) ; } return " - 1" ; } static void reverse ( ) { int l , r = S . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = S [ l ] ; S [ l ] = S [ r ] ; S [ r ] = temp ; } } public static void Main ( String [ ] args ) { int N = S . Length ; Console . Write ( rearrangeString ( N ) ) ; } }
Lexicographically largest string possible by repeatedly appending first character of two given strings | C # program for the above approach ; Function to make the lexicographically largest string by merging two strings ; Stores the resultant string ; If the string word1 is lexographically greater than or equal to word2 ; Update the string merge ; Erase the first index of the string word1 ; Otherwise ; Update the string merge ; Erase the first index of the string word2 ; Return the final string ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static string largestMerge ( string word1 , string word2 ) { string merge = " " ; while ( word1 . Length != 0 word2 . Length != 0 ) { if ( String . Compare ( word1 , word2 ) == 0 || String . Compare ( word1 , word2 ) > 0 ) { merge = merge + word1 [ 0 ] ; word1 = word1 . Substring ( 1 ) ; } else { merge = merge + word2 [ 0 ] ; word2 = word2 . Substring ( 1 ) ; } } return merge ; } public static void Main ( ) { string S1 = " xyzxyz " ; string S2 = " xywzxyx " ; Console . Write ( largestMerge ( S1 , S2 ) ) ; } }
Maximum rods to put horizontally such that no two rods overlap on X coordinate | C # program for the above approach ; Function to find the maximum number of rods that can be put horizontally ; Stores the result ; Stores the last occupied point ; Traverse the array arr [ ] ; If the current point can be put on the left side ; Increment the ans by 1 ; Update prev ; Else if the given point can be put on the right side ; Increment the ans by 1 ; Update prev ; Otherwise , ; Update prev ; Return the ans ; Driver code
using System ; class GFG { public static int findMaximumPoints ( int N , int [ ] X , int [ ] H ) { int ans = 0 ; int prev = Int32 . MinValue ; for ( int i = 0 ; i < N ; ++ i ) { if ( prev < ( X [ i ] - H [ i ] ) ) { ++ ans ; prev = X [ i ] ; } else if ( i == N - 1 || ( X [ i ] + H [ i ] ) < X [ i + 1 ] ) { ++ ans ; prev = X [ i ] + H [ i ] ; } else { prev = X [ i ] ; } } return ans ; } static public void Main ( ) { int [ ] X = { 1 , 2 , 3 } ; int [ ] H = { 2 , 5 , 5 } ; int N = X . Length ; Console . WriteLine ( findMaximumPoints ( N , X , H ) ) ; } }
Maximize the missing values in given time in HH : MM format | C # program for the above approach ; Function to find the maximum time by replacing ' ? ' by any digits ; Convert the string to the character array ; If the 0 th index is ' ? ' ; If the 1 st index is ' ? ' ; If the 3 rd index is ' ? ' ; If the 4 th index is ' ? ' ; Return new string ; Driver Code
using System ; using System . Collections . Generic ; class gfg { public static void maxTime ( String S ) { char [ ] s = S . ToCharArray ( ) ; if ( s [ 0 ] == ' ? ' ) { if ( s [ 1 ] <= '3' s [ 1 ] == ' ? ' ) s [ 0 ] = '2' ; else s [ 0 ] = '1' ; } if ( s [ 1 ] == ' ? ' ) { if ( s [ 0 ] != '2' ) { s [ 1 ] = '9' ; } else s [ 1 ] = '3' ; } if ( s [ 3 ] == ' ? ' ) s [ 3 ] = '5' ; if ( s [ 4 ] == ' ? ' ) s [ 4 ] = '9' ; Console . Write ( new String ( s ) ) ; } public static void Main ( String [ ] args ) { String S = " ? 4:5 ? " ; maxTime ( S ) ; } }
Maximum GCD of two numbers possible by adding same value to them | C # program for the above approach ; Function to calculate maximum gcd of two numbers possible by adding same value to both a and b ; Driver Code ; Given Input
using System ; class GFG { static void maxGcd ( int a , int b ) { Console . Write ( Math . Abs ( a - b ) ) ; } static public void Main ( ) { int a = 2231 ; int b = 343 ; maxGcd ( a , b ) ; } }
Count the combination of 4 s and / or 5 s required to make each Array element 0 | C # program for the above approach ; Function to print the count of the combination of 4 or 5 required to make the arr [ i ] for each 0 < i < N ; Vector to store the answer ; Iterate in the range [ 0 , N - 1 ] ; Initialize sum to store the count of numbers and cnt for the current factor of 4 ; Iterate in the range [ 0 , arr [ i ] ] with increment of 4 ; Check if arr [ i ] - j ( the current factor of 4 ) is divisible by 5 or not ; If sum is not maximum then answer is found ; Finally , print the required answer ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void sumOfCombinationOf4OR5 ( int [ ] arr , int N ) { int [ ] ans = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) ans [ i ] = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 4 ) { continue ; } int sum = Int32 . MaxValue , cnt = 0 ; for ( int j = 0 ; j <= arr [ i ] ; j += 4 ) { if ( ( arr [ i ] - j ) % 5 == 0 ) { sum = Math . Min ( sum , cnt + ( int ) ( arr [ i ] - j ) / 5 ) ; } cnt ++ ; } if ( sum != Int32 . MaxValue ) ans [ i ] = sum ; } foreach ( int num in ans ) Console . Write ( num + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 7 , 15 , 17 , 22 } ; int N = arr . Length ; sumOfCombinationOf4OR5 ( arr , N ) ; } }
Find an N | C # program for the above approach ; Function to find an N - length binary string having maximum sum of elements from all given ranges ; Iterate over the range [ 1 , N ] ; If i is odd , then print 0 ; Otherwise , print 1 ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printBinaryString ( int [ , ] arr , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 1 ) { Console . Write ( 0 ) ; } else { Console . Write ( 1 ) ; } } } public static void Main ( ) { int N = 5 ; int [ , ] arr = { { 1 , 3 } , { 2 , 4 } , { 2 , 5 } } ; printBinaryString ( arr , N ) ; } }
Minimum cost to complete given tasks if cost of 1 , 7 and 30 days are given | C # program for the above approach ; Function to find the minimum cost to hire the workers for the given days in the array days [ ] ; Initialize the array dp ; Minimum Cost for Nth day ; Pointer of the array arr [ ] ; Traverse from right to left ; If worker is hired for 1 day ; If worker is hired for 7 days ; If worker is hired for 30 days ; Update the value of dp [ i ] as minimum of 3 options ; If the day is not at the array arr [ ] ; Return the answer ; Driver Code
using System ; class GFG { static int MinCost ( int [ ] days , int [ ] cost , int N ) { int size = days [ N - 1 ] + 1 ; int [ ] dp = new int [ size ] ; dp [ size - 1 ] = Math . Min ( cost [ 0 ] , Math . Min ( cost [ 1 ] , cost [ 2 ] ) ) ; int ptr = N - 2 ; for ( int i = size - 2 ; i > 0 ; i -- ) { if ( ptr >= 0 && days [ ptr ] == i ) { int val1 = dp [ i + 1 ] + cost [ 0 ] ; int val2 = cost [ 1 ] + ( ( i + 7 >= size ) ? 0 : dp [ i + 7 ] ) ; int val3 = cost [ 2 ] + ( ( i + 30 >= size ) ? 0 : dp [ i + 30 ] ) ; dp [ i ] = Math . Min ( val1 , Math . Min ( val2 , val3 ) ) ; ptr -- ; } else { dp [ i ] = dp [ i + 1 ] ; } } return dp [ 1 ] ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 7 , 8 , 10 , 17 } ; int [ ] cost = { 3 , 8 , 20 } ; int N = arr . Length ; Console . WriteLine ( MinCost ( arr , cost , N ) ) ; } }