codeparrot/xlcost-text-to-code huggingface dataset:
Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | C # program for the above approach ; Function that count the number of integers from array [ ] B such that subtracting element in the range [ 0 , K ] given any element in [ ] A ; To store the count of element ; Traverse the array [ ] B ; Traverse the array [ ] A ; Find the difference ; If difference is atmost K then increment the cnt ; Print the count ; Driver Code ; Given array [ ] A and [ ] B ; Given K ; Function call
using System ; class GFG { static void countElement ( int [ ] A , int N , int [ ] B , int M , int K ) { int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int currentElement = B [ i ] ; for ( int j = 0 ; j < N ; j ++ ) { int diff = Math . Abs ( currentElement - A [ j ] ) ; if ( diff <= K ) { cnt ++ ; break ; } } } Console . Write ( cnt ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 100 , 65 , 35 , 85 , 55 } ; int [ ] B = { 30 , 60 , 75 , 95 } ; int K = 5 ; int N = A . Length ; int M = B . Length ; countElement ( A , N , B , M , K ) ; } }
Generate a String of having N * N distinct non | C # program to implement the above approach ; Function to construct a string having N * N non - palindromic substrings ; Driver Code
using System ; class GFG { static void createString ( int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( ' a ' ) ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( ' b ' ) ; } } public static void Main ( String [ ] args ) { int N = 4 ; createString ( N ) ; } }
Maximum Subset Sum possible by negating the entire sum after selecting the first Array element | C # Program to implement the above approach ; Function returns maximum subset sum from the given array ; Case 2 : Negate values from A [ 1 ] to A [ N - 1 ] ; Include only positives for max subset sum ; Return max sum obtained ; Function to return maximum of the maximum subset sum calculated for the two cases ; Case 1 ; Case 2 ; Modifying the sum ; Including first element ; Negating again ; Return the required answer ; Driver Code
using System ; class GFG { static int maxSubset ( int [ ] A , bool flag ) { int n = A . Length ; int sum = 0 ; if ( flag ) { for ( int i = 1 ; i < n ; i ++ ) A [ i ] = - A [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > 0 ) { sum += A [ i ] ; } } return sum ; } static int findBest ( int [ ] A ) { int x = maxSubset ( A , false ) ; int y = maxSubset ( A , true ) ; y = - y ; y += A [ 0 ] ; y = - y ; return Math . Max ( x , y ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 10 , 4 , - 6 , 3 } ; Console . Write ( findBest ( A ) + " STRNEWLINE " ) ; } }
Construct an Array of Strings having Longest Common Prefix specified by the given Array | C # Program to implement the above approach ; Function to find the array of strings ; Marks the ( N + 1 ) th string ; To generate remaining N strings ; Find i - th string using ( i + 1 ) - th string ; Check if current character is b ; Otherwise ; Insert the string ; Return the answer ; Driver Code ; Print the strings
using System ; using System . Collections . Generic ; class GFG { static List < string > solve ( int n , int [ ] arr ) { string s = " aaa " ; List < string > ans = new List < string > ( ) ; ans . Add ( s ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . Length - 1 >= arr [ i ] ) { char ch = s [ arr [ i ] ] ; if ( ch == ' b ' ) ch = ' a ' ; else ch = ' b ' ; char [ ] chr = s . ToCharArray ( ) ; chr [ arr [ i ] ] = ch ; s = new string ( chr ) ; } ans . Add ( s ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 0 , 3 } ; int n = arr . Length ; List < string > ans = solve ( n , arr ) ; for ( int i = ans . Count - 1 ; i >= 0 ; i -- ) { Console . WriteLine ( ans [ i ] ) ; } } }
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | C # Program to implement the above approach ; Function to find the minimum number of swaps required ; Sort the array in ascending order ; Iterate until a greater element is found ; Keep incrementing ind ; If a greater element is found ; Increase count of swap ; Increment ind ; If end of array is reached ; Return the answer ; Driver Code
using System ; class GFG { static int countSwaps ( int [ ] A , int n ) { Array . Sort ( A ) ; int ind = 1 , res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ind < n && A [ ind ] == A [ i ] ) ind ++ ; if ( ind < n && A [ ind ] > A [ i ] ) { res ++ ; ind ++ ; } if ( ind >= n ) break ; } return res ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 3 , 3 , 2 , 5 } ; Console . Write ( countSwaps ( A , 5 ) ) ; } }
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | C # program to implement the above approach ; Function to find the minimum number of swaps required ; Stores the frequency of the array elements ; Stores maximum frequency ; Find the max frequency ; Update frequency ; Update maximum frequency ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int countSwaps ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int max_frequency = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } max_frequency = Math . Max ( max_frequency , mp [ arr [ i ] ] ) ; } return n - max_frequency ; } public static void Main ( String [ ] args ) { int [ ] A = { 6 , 5 , 4 , 3 , 2 , 1 } ; Console . Write ( countSwaps ( A , 6 ) ) ; } }
Minimum number of steps required to obtain the given Array by the given operations | C # Program to implement the above approach ; Function to calculate the minimum steps to obtain the desired array ; Initialize variable ; Iterate over the array [ ] arr ; Check if i > 0 ; Update the answer ; Return the result ; Driver Code
using System ; class GFG { static int min_operation ( int [ ] a , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) ans += Math . Abs ( a [ i ] - a [ i - 1 ] ) ; else ans += Math . Abs ( a [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . Write ( min_operation ( arr , n ) ) ; } }
Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | C # implementation of the above approach ; Function to print the required matrix ; For 1 * 1 matrix ; Greater number ; Smaller number ; Sets / Resets for alternate filling of the matrix ; Print the matrix ; If end of row is reached ; Driver Code
using System ; class GFG { static void FindMatrix ( int n , int m , int x , int y ) { int a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { Console . Write ( y + " STRNEWLINE " ) ; } else { Console . Write ( x + " STRNEWLINE " ) ; } return ; } a = Math . Min ( x , y ) ; b = Math . Min ( 2 * x , y ) - a ; bool flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) Console . Write ( a + " ▁ " ) ; else Console . Write ( b + " ▁ " ) ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ; } }
Travelling Salesman Problem | Greedy Approach | C # program for the above approach ; Function to find the minimum cost path for all the paths ; Starting from the 0 th indexed city i . e . , the first city ; Traverse the adjacency matrix tsp [ , ] ; Corner of the Matrix ; If this path is unvisited then and if the cost is less then update the cost ; Check all paths from the ith indexed city ; Update the ending city in array from city which was last visited ; Started from the node where we finished as well . ; Driver Code ; Input Matrix ; Function call
using System ; using System . Collections . Generic ; class TSPGreedy { static void findMinRoute ( int [ , ] tsp ) { int sum = 0 ; int counter = 0 ; int j = 0 , i = 0 ; int min = int . MaxValue ; List < int > visitedRouteList = new List < int > ( ) ; visitedRouteList . Add ( 0 ) ; int [ ] route = new int [ tsp . Length ] ; while ( i < tsp . GetLength ( 0 ) && j < tsp . GetLength ( 1 ) ) { if ( counter >= tsp . GetLength ( 0 ) - 1 ) { break ; } if ( j != i && ! ( visitedRouteList . Contains ( j ) ) ) { if ( tsp [ i , j ] < min ) { min = tsp [ i , j ] ; route [ counter ] = j + 1 ; } } j ++ ; if ( j == tsp . GetLength ( 0 ) ) { sum += min ; min = int . MaxValue ; visitedRouteList . Add ( route [ counter ] - 1 ) ; j = 0 ; i = route [ counter ] - 1 ; counter ++ ; } } i = route [ counter - 1 ] - 1 ; for ( j = 0 ; j < tsp . GetLength ( 0 ) ; j ++ ) { if ( ( i != j ) && tsp [ i , j ] < min ) { min = tsp [ i , j ] ; route [ counter ] = j + 1 ; } } sum += min ; Console . Write ( " Minimum ▁ Cost ▁ is ▁ : ▁ " ) ; Console . WriteLine ( sum ) ; } public static void Main ( String [ ] args ) { int [ , ] tsp = { { - 1 , 10 , 15 , 20 } , { 10 , - 1 , 35 , 25 } , { 15 , 35 , - 1 , 30 } , { 20 , 25 , 30 , - 1 } } ; findMinRoute ( tsp ) ; } }
Count distinct elements after adding each element of First Array with Second Array | C # program for the above approach ; Function to find Occurrence of each element from 1 to 2 * MAX ; Initialise MAX ; Count vector to store count of each element from 1 to 2 * MAX ; Size of Arr1 and Arr2 ; Find the elements of arr3 [ ] and increase count of element by 1 ; Print the result ; Driver Code ; Given arrays arr1 [ ] and arr2 [ ] ; Function call
using System ; using System . Linq ; class GFG { static void findCount ( int [ ] Arr1 , int [ ] Arr2 ) { int MAX = Math . Max ( Arr1 . Max ( ) , Arr2 . Max ( ) ) ; int [ ] Count = new int [ 2 * MAX + 1 ] ; int n = Arr1 . Length , m = Arr2 . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int element = Arr1 [ i ] + Arr2 [ j ] ; Count [ element ] ++ ; } } for ( int i = 1 ; i <= 2 * MAX ; i ++ ) { if ( Count [ i ] > 0 ) { Console . Write ( i + " - > " + Count [ i ] + " STRNEWLINE " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 } ; int [ ] arr2 = { 1 , 2 , 1 } ; findCount ( arr1 , arr2 ) ; } }
Find two numbers with difference and division both same as N | C # program for the above approach ; Function to find two numbers with difference and division both as N ; Condition if the answer is not possible ; Calculate a and b ; Print the values ; Driver Code ; Given N ; Function call
using System ; class GFG { static void findAandB ( double N ) { if ( N == 1 ) { Console . Write ( " No " ) ; return ; } double a = N * N / ( N - 1 ) ; double b = a / N ; Console . Write ( " a ▁ = ▁ " + a + " STRNEWLINE " ) ; Console . Write ( " b ▁ = ▁ " + b + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { double N = 6 ; findAandB ( N ) ; } }
Maximize number of elements from Array with sum at most K | C # program for the above approach ; Function to select a maximum number of elements in array whose sum is at most K ; Sort the array ; Calculate the sum and count while iterating the sorted array ; Iterate for all the elements in the array ; Add the current element to sum ; Increment the count ; Return the answer ; Driver Code ; Given array ; Given sum k ; Function call
using System ; class GFG { static int maxSelections ( int [ ] A , int n , int k ) { Array . Sort ( A ) ; int sum = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + A [ i ] ; if ( sum > k ) { break ; } count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 7 , 2 , 9 , 4 } ; int k = 15 ; int n = A . Length ; Console . Write ( maxSelections ( A , n , k ) ) ; } }
Pair of integers with difference K having an element as the K | C # program to implement the above problem ; Function to find the required pair ; No pair possible ; Driver Code
using System ; class GFG { static void computePair ( double K ) { if ( K == 1 ) { Console . Write ( " No " ) ; return ; } else { Console . Write ( K * K / ( K - 1 ) + " ▁ " ) ; Console . Write ( K / ( K - 1 ) + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { double K = 6 ; computePair ( K ) ; } }
Generate a Binary String without any consecutive 0 ' s ▁ and ▁ at ▁ most ▁ K ▁ consecutive ▁ 1' s | C # implementation of the above approach ; Function to construct the binary string ; Conditions when string construction is not possible ; Stores maximum 1 's that  can be placed in between ; Place 0 's ; Place 1 's in between ; Count remaining M 's ; Place 1 's at the end ; Place 1 's at the beginning ; Return the final string ; Driver code
using System ; class GFG { static String ConstructBinaryString ( int N , int M , int K ) { if ( M < ( N - 1 ) || M > K * ( N + 1 ) ) return " - 1" ; string ans = " " ; int l = Math . Min ( K , M / ( N - 1 ) ) ; int temp = N ; while ( temp != 0 ) { temp -- ; ans += '0' ; if ( temp == 0 ) break ; for ( int i = 0 ; i < l ; i ++ ) { ans += '1' ; } } M -= ( N - 1 ) * l ; if ( M == 0 ) return ans ; l = Math . Min ( M , K ) ; for ( int i = 0 ; i < l ; i ++ ) ans += '1' ; M -= l ; while ( M > 0 ) { ans = '1' + ans ; M -- ; } return ans ; } public static void Main ( string [ ] args ) { int N = 5 , M = 9 , K = 2 ; Console . Write ( ConstructBinaryString ( N , M , K ) ) ; } }
Find smallest number formed by inverting digits of given number N | C # program for the above approach ; Function to invert the digits of integer N to form minimum possible number ; Initialize the array ; Iterate till the number N exists ; Last digit of the number N ; Checking if the digit is smaller than 9 - digit ; Store the smaller digit in the array ; Reduce the number each time ; Check if the digit starts with 0 or not ; Print the answer ; Driver Code ; Given Number ; Function Call
using System ; class GFG { static void number ( int num ) { int [ ] a = new int [ 20 ] ; int r , i = 0 , j ; while ( num > 0 ) { r = num % 10 ; if ( 9 - r > r ) a [ i ] = r ; else a [ i ] = 9 - r ; i ++ ; num = num / 10 ; } if ( a [ i - 1 ] == 0 ) { Console . Write ( "9" ) ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) Console . Write ( a [ j ] ) ; } public static void Main ( string [ ] args ) { int num = 4545 ; number ( num ) ; } }
Check if the concatenation of first N natural numbers is divisible by 3 | C # program for the above approach ; Function that returns True if concatenation of first N natural numbers is divisible by 3 ; Check using the formula ; Driver Code ; Given Number ; Function Call
using System ; class GFG { static bool isDivisible ( int N ) { return ( N a 1 ) % 3 != 0 ; } public static void Main ( ) { int N = 6 ; if ( isDivisible ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Count of N digit Numbers whose sum of every K consecutive digits is equal | C # program for the above approach ; Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits ; If sum is not equal then break the loop ; Increment the count if it satisfy the given condition ; Driver Code ; Given N and K ; Function call
using System ; class GFG { static int countDigitSum ( int N , int K ) { int l = ( int ) Math . Pow ( 10 , N - 1 ) , r = ( int ) Math . Pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; int [ ] digits = new int [ N ] ; for ( int j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num /= 10 ; } int sum = 0 , flag = 0 ; for ( int j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( int j = 1 ; j < N - K + 1 ; j ++ ) { int curr_sum = 0 ; for ( int m = j ; m < j + K ; m ++ ) { curr_sum += digits [ m ] ; } if ( sum != curr_sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } public static void Main ( ) { int N = 2 , K = 1 ; Console . Write ( countDigitSum ( N , K ) ) ; } }
Perfect Square factors of a Number | C # program to implement the above approach ; Function that returns the count of factors that are perfect squares ; Stores the count of number of times a prime number divides N . ; Stores the number of factors that are perfect square ; Count number of 2 's  that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Driver Code
using System ; class GFG { static int noOfFactors ( int N ) { if ( N == 1 ) return 1 ; int count = 0 ; int ans = 1 ; while ( N % 2 == 0 ) { count ++ ; N = N / 2 ; } ans *= ( count / 2 + 1 ) ; for ( int i = 3 ; i * i <= N ; i = i + 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } ans *= ( count / 2 + 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 100 ; Console . Write ( noOfFactors ( N ) ) ; } }
Nth angle of a Polygon whose initial angle and per angle increment is given | C # program for the above approach ; Function to check if the angle is possible or not ; Angular sum of a N - sided polygon ; Angular sum of N - sided given polygon ; Check if both sum are equal ; Function to find the nth angle ; Calculate nth angle ; Return the nth angle ; Driver Code ; Checks the possibility of the polygon exist or not ; Print nth angle of the polygon
using System ; class GFG { static bool possible ( int N , int a , int b , int n ) { int sum_of_angle = 180 * ( N - 2 ) ; int Total_angle = ( N * ( ( 2 * a ) + ( N - 1 ) * b ) ) / 2 ; if ( sum_of_angle != Total_angle ) return false ; else return true ; } static int nth_angle ( int N , int a , int b , int n ) { int nth = 0 ; nth = a + ( n - 1 ) * b ; return nth ; } public static void Main ( string [ ] args ) { int N = 3 , a = 30 , b = 30 , n = 3 ; if ( possible ( N , a , b , n ) ) Console . Write ( nth_angle ( N , a , b , n ) ) ; else Console . Write ( " Not ▁ Possible " ) ; } }
Check if the given string is linear or not | C # program for the above approach ; Function to check if the given string is linear or not ; Iterate over string ; If character is not same as the first character then return false ; Increment the tmp ; Driver code ; Given String str ; Function call
using System ; class GFG { static bool is_linear ( String s ) { int tmp = 0 ; char first = s [ 0 ] ; for ( int pos = 0 ; pos < s . Length ; pos += tmp ) { if ( s [ pos ] != first ) { return false ; } tmp ++ ; } return true ; } public static void Main ( String [ ] args ) { String str = " aapaxyayziabcde " ; if ( is_linear ( str ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Minimize cost to color all the vertices of an Undirected Graph | C # program to implement the above approach ; Function to implement DFS Traversal to marks all the vertices visited from vertex U ; Mark U as visited ; Traverse the adjacency list of U ; Function to find the minimum cost to color all the vertices of graph ; To store adjacency list ; Loop through the edges to create adjacency list ; To check if a vertex of the graph is visited ; Mark visited to all the vertices that can be reached by colored vertices ; Perform DFS ; To store count of uncolored sub - graphs ; Loop through vertex to count uncolored sub - graphs ; If vertex not visited ; Increase count of uncolored sub - graphs ; Perform DFS to mark visited to all vertices of current sub - graphs ; Calculate minimum cost to color all vertices ; Print the result ; Driver code ; Given number of vertices and edges ; Given edges ; Given cost of coloring and adding an edge ; Given array of colored vertices
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void DFS ( int U , int [ ] vis , ArrayList adj ) { vis [ U ] = 1 ; foreach ( int V in ( ArrayList ) adj [ U ] ) { if ( vis [ V ] == 0 ) DFS ( V , vis , adj ) ; } } static void minCost ( int N , int M , int vCost , int eCost , int [ ] sorc , ArrayList colored , int [ ] destination ) { ArrayList adj = new ArrayList ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) adj . Add ( new ArrayList ( ) ) ; for ( int i = 0 ; i < M ; i ++ ) { ( ( ArrayList ) adj [ sorc [ i ] ] ) . Add ( destination [ i ] ) ; ( ( ArrayList ) adj [ destination [ i ] ] ) . Add ( sorc [ i ] ) ; } int [ ] vis = new int [ N + 1 ] ; for ( int i = 0 ; i < colored . Count ; i ++ ) { DFS ( ( int ) colored [ i ] , vis , adj ) ; } int X = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( vis [ i ] == 0 ) { X ++ ; DFS ( i , vis , adj ) ; } } int mincost = X * Math . Min ( vCost , eCost ) ; Console . Write ( mincost ) ; } public static void Main ( string [ ] args ) { int N = 3 , M = 1 ; int [ ] sorc = { 1 } ; int [ ] destination = { 2 } ; int vCost = 3 , eCost = 2 ; ArrayList colored = new ArrayList ( ) ; colored . Add ( 1 ) ; minCost ( N , M , vCost , eCost , sorc , colored , destination ) ; } }
Minimum cost to reduce the integer N to 1 as per given conditions | C # program for the above approach ; Function to find the minimum cost to reduce the integer N to 1 by the given operations ; Check if x is 1 ; Print the answer ; Prestore the answer ; Iterate till n exists ; Divide N by x ; Reduce n by x ; Add the cost ; Update the answer ; Return the answer ; Driver code ; Initialize the variables ; Function call
using System ; class GFG { static int min_cost ( int n , int x , int p , int q ) { if ( x == 1 ) { Console . WriteLine ( ( n - 1 ) * p ) ; return 0 ; } int ans = ( n - 1 ) * p ; int pre = 0 ; while ( n > 1 ) { int tmp = n / x ; if ( tmp < 0 ) break ; pre += ( n - tmp * x ) * p ; n /= x ; pre += q ; ans = Math . Min ( ans , pre + ( n - 1 ) * p ) ; } return ans ; } public static void Main ( String [ ] args ) { int n = 5 , x = 2 , p = 2 , q = 3 ; Console . WriteLine ( min_cost ( n , x , p , q ) ) ; } }
Number of ways to sum up a total of N from limited denominations | C # program for the above approach ; Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the readonly result ; As one of the denominations is rupee 1 , so we can reduce the computation by checking the equality for N - ( A * 1 ) = N - A ; Increment the count for number of ways ; Driver Code ; Given denominations ; Function call
using System ; class GFG { static int calculateWays ( int [ ] arr1 , int [ ] arr2 , int N ) { int A = arr2 [ 0 ] , B = arr2 [ 1 ] ; int C = arr2 [ 2 ] , D = arr2 [ 3 ] ; int ans = 0 ; for ( int b = 0 ; b <= B && b * 5 <= ( N ) ; b ++ ) for ( int c = 0 ; c <= C && b * 5 + c * 10 <= ( N ) ; c ++ ) for ( int d = 0 ; d <= D && b * 5 + c * 10 + d * 20 <= ( N ) ; d ++ ) if ( ( b * 5 ) + ( c * 10 ) + ( d * 20 ) >= ( N - A ) ) ans ++ ; return ans ; } public static void Main ( String [ ] args ) { int N = 123 ; int [ ] arr1 = { 1 , 5 , 10 , 20 } ; int [ ] arr2 = { 6 , 4 , 3 , 5 } ; Console . Write ( calculateWays ( arr1 , arr2 , N ) ) ; } }
Number of ways to sum up a total of N from limited denominations | C # program for the above approach ; Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the readonly result ; This will give the number of coins for all combinations of coins with value 1 and 5 ; L2 will sum the values of those indices of ways [ ] which is equal to ( N - ( c * 10 + d * 20 ) ) ; Return the final count ; Driver Code ; Given denominations ; Function call
using System ; class GFG { static int [ ] ways = new int [ 1010 ] ; static int calculateWays ( int [ ] arr1 , int [ ] arr2 , int N ) { int A = arr2 [ 0 ] , B = arr2 [ 1 ] ; int C = arr2 [ 2 ] , D = arr2 [ 3 ] ; int ans = 0 ; for ( int b = 0 ; b <= B && b * 5 <= N ; b ++ ) { for ( int a = 0 ; a <= A && a * 1 + b * 5 <= N ; a ++ ) { ways [ a + b * 5 ] ++ ; } } for ( int c = 0 ; c <= C && c * 10 <= ( N ) ; c ++ ) { for ( int d = 0 ; d <= D && c * 10 + d * 20 <= ( N ) ; d ++ ) { ans += ways [ N - c * 10 - d * 20 ] ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 123 ; int [ ] arr1 = { 1 , 5 , 10 , 20 } ; int [ ] arr2 = { 6 , 4 , 3 , 5 } ; Console . Write ( calculateWays ( arr1 , arr2 , N ) ) ; } }