codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of operations to convert a given sequence into a Geometric Progression | C # program to find minimum number of operations to convert a given sequence to an Geometric Progression ; Function to print the GP series ; Check for possibility ; Function for getting the Arithmetic Progression ; The array c describes all the given set of possible operations . ; Size of c ; candidate answer ; loop through all the permutations of the first two elements . ; a1 and a2 are the candidate first two elements of the possible GP . ; temp stores the current answer , including the modification of the first two elements . ; common ratio of the possible GP ; To check if the chosen set is valid , and id yes find the number of operations it takes . ; ai is value of a [ i ] according to the assumed first two elements a1 , a2 ith element of an GP = a1 * ( ( a2 - a1 ) ^ ( i - 1 ) ) ; Check for the " proposed " element to be only differing by one ; set the temporary ans ; to infinity and break ; update answer ; Calling function to print the sequence ; Driver Code ; array is 1 - indexed , with a [ 0 ] = 0 for the sake of simplicity ; Function to print the minimum operations and the sequence of elements
using System ; class GFG { class pair { public double first , second ; public pair ( double first , double second ) { this . first = first ; this . second = second ; } } static void construct ( int n , pair ans_pair ) { if ( ans_pair . first == - 1 ) { Console . Write ( " Not ▁ possible " ) ; return ; } double a1 = ans_pair . first ; double a2 = ans_pair . second ; double r = a2 / a1 ; Console . Write ( " The ▁ resultant ▁ sequence ▁ is : STRNEWLINE " ) ; for ( int i = 1 ; i <= n ; i ++ ) { int ai = ( int ) ( a1 * Math . Pow ( r , i - 1 ) ) ; Console . Write ( ai + " ▁ " ) ; } } static void findMinimumOperations ( double [ ] a , int n ) { int ans = int . MaxValue ; int [ ] c = { - 1 , 0 , 1 } ; int possibilities = 3 ; int pos1 = - 1 , pos2 = - 1 ; for ( int i = 0 ; i < possibilities ; i ++ ) { for ( int j = 0 ; j < possibilities ; j ++ ) { double a1 = a [ 1 ] + c [ i ] ; double a2 = a [ 2 ] + c [ j ] ; int temp = ( int ) ( Math . Abs ( a1 - a [ 1 ] ) + Math . Abs ( a2 - a [ 2 ] ) ) ; if ( a1 == 0 a2 == 0 ) continue ; double r = a2 / a1 ; for ( int pos = 3 ; pos <= n ; pos ++ ) { double ai = a1 * Math . Pow ( r , pos - 1 ) ; if ( a [ pos ] == ai ) { continue ; } else if ( a [ pos ] + 1 == ai a [ pos ] - 1 == ai ) { temp ++ ; } else { temp = int . MaxValue ; break ; } } if ( temp < ans ) { ans = temp ; pos1 = ( int ) a1 ; pos2 = ( int ) a2 ; } } } if ( ans == - 1 ) { Console . Write ( " - 1" ) ; return ; } Console . Write ( " Minimum ▁ Number ▁ of ▁ Operations ▁ are ▁ " + ans + " STRNEWLINE " ) ; pair ans_pair = new pair ( pos1 , pos2 ) ; construct ( n , ans_pair ) ; } public static void Main ( String [ ] args ) { double [ ] a = { 0 , 7 , 20 , 49 , 125 } ; int n = a . Length ; findMinimumOperations ( a , n - 1 ) ; } }
Optimal Storage on Tapes | C # Program to find the order of programs for which MRT is minimized ; This functions outputs the required order and Minimum Retrieval Time ; Here length of i 'th program is L[i] ; Lengths of programs sorted according to increasing lengths . This is the order in which the programs have to be stored on tape for minimum MRT ; MRT - Minimum Retrieval Time ; Driver Code
using System ; class GFG { static void findOrderMRT ( int [ ] L , int n ) { Array . Sort ( L ) ; Console . Write ( " Optimal ▁ order ▁ in ▁ " + " which ▁ programs ▁ are " + " ▁ to ▁ be ▁ stored ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( L [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; double MRT = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j <= i ; j ++ ) sum += L [ j ] ; MRT += sum ; } MRT /= n ; Console . WriteLine ( " Minimum ▁ Retrieval ▁ " + " Time ▁ of ▁ this ▁ order ▁ is ▁ " + MRT ) ; } public static void Main ( ) { int [ ] L = { 2 , 5 , 4 } ; int n = L . Length ; findOrderMRT ( L , n ) ; } }
Minimum operations to make GCD of array a multiple of k | C # program to make GCD of array a multiple of k . ; If array value is not 1 and it is greater than k then we can increase the or decrease the remainder obtained by dividing k from the ith value of array so that we get the number which is either closer to k or its multiple ; Else we only have one choice which is to increment the value to make equal to k ; Driver code
using System ; public class GFG { static int MinOperation ( int [ ] a , int n , int k ) { int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] != 1 && a [ i ] > k ) { result = result + Math . Min ( a [ i ] % k , k - a [ i ] % k ) ; } else { result = result + k - a [ i ] ; } } return result ; } static public void Main ( ) { int [ ] arr = { 4 , 5 , 6 } ; int n = arr . Length ; int k = 5 ; Console . WriteLine ( MinOperation ( arr , n , k ) ) ; } }
Maximum product subset of an array | C # Java program to find maximum product of a subset . ; Find count of negative numbers , count of zeros , negative number with least absolute value and product of non - zero numbers ; If number is 0 , we don 't  multiply it with product. ; Count negatives and keep track of negative number with least absolute value . ; If there are all zeros ; If there are odd number of negative numbers ; Exceptional case : There is only negative and all other are zeros ; Otherwise result is product of all non - zeros divided by negative number with least absolute value . ; Driver code
using System ; class GFG { static int maxProductSubset ( int [ ] a , int n ) { if ( n == 1 ) { return a [ 0 ] ; } int max_neg = int . MinValue ; int count_neg = 0 , count_zero = 0 ; int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 ) { count_neg ++ ; max_neg = Math . Max ( max_neg , a [ i ] ) ; } prod = prod * a [ i ] ; } if ( count_zero == n ) { return 0 ; } if ( count_neg % 2 == 1 ) { if ( count_neg == 1 && count_zero > 0 && count_zero + count_neg == n ) { return 0 ; } prod = prod / max_neg ; } return prod ; } public static void Main ( ) { int [ ] a = { - 1 , - 1 , - 2 , 4 , 3 } ; int n = a . Length ; Console . Write ( maxProductSubset ( a , n ) ) ; } }
Minimum Fibonacci terms with sum equal to K | C # code to find the minimum number of Fibonacci terms that sum to k . ; Function to calaculate Fibonacci Terms ; Calculate all Fibonacci terms which are less than or equal to k . ; If next term is greater than k do not add in arraylist and return . ; Function to find the minimum number of Fibonacci terms having sum equal to k . ; List to store Fibonacci terms . ; Subtract Fibonacci terms from sum k until sum > 0. ; Divide sum k by j - th Fibonacci term to find how many terms it contribute in sum . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static void calcFiboTerms ( List < int > fiboterms , int k ) { int i = 3 , nextTerm = 0 ; fiboterms . Add ( 0 ) ; fiboterms . Add ( 1 ) ; fiboterms . Add ( 1 ) ; while ( true ) { nextTerm = fiboterms [ i - 1 ] + fiboterms [ i - 2 ] ; if ( nextTerm > k ) return ; fiboterms . Add ( nextTerm ) ; i ++ ; } } public static int fibMinTerms ( int k ) { List < int > fiboterms = new List < int > ( ) ; calcFiboTerms ( fiboterms , k ) ; int count = 0 , j = fiboterms . Count - 1 ; while ( k > 0 ) { count += ( k / fiboterms [ j ] ) ; k %= ( fiboterms [ j ] ) ; j -- ; } return count ; } public static void Main ( String [ ] args ) { int k = 17 ; Console . WriteLine ( fibMinTerms ( k ) ) ; } }
Smallest number with sum of digits as N and divisible by 10 ^ N | C # program to find smallest number to find smallest number with N as sum of digits and divisible by 10 ^ N . ; If N = 0 the string will be 0 ; If n is not perfectly divisible by 9 output the remainder ; Print 9 N / 9 times ; Append N zero 's to the number so as to make it divisible by 10^N ) ; Driver Code
using System ; class GFG { static void digitsNum ( int N ) { if ( N == 0 ) Console . Write ( "0" ) ; if ( N % 9 != 0 ) Console . Write ( ( N % 9 ) ) ; for ( int i = 1 ; i <= ( N / 9 ) ; ++ i ) Console . Write ( "9" ) ; for ( int i = 1 ; i <= N ; ++ i ) Console . Write ( "0" ) ; Console . WriteLine ( " " ) ; } public static void Main ( ) { int N = 5 ; Console . Write ( " The ▁ number ▁ is ▁ : ▁ " ) ; digitsNum ( N ) ; } }
Divide 1 to n into two groups with minimum sum difference | C # program to divide n integers in two groups such that absolute difference of their sum is minimum ; To print vector along size ; Print vector size ; Print vector elements ; To divide n in two groups such that absolute difference of their sum is minimum ; Find sum of all elements upto n ; Sum of elements of group1 ; If sum is greater then or equal to 0 include i in group1 otherwise include in group2 ; Decrease sum of group1 ; Print both the groups ; Driver code
using System ; using System . Collections ; class GFG { static void printVector ( ArrayList v ) { Console . WriteLine ( v . Count ) ; for ( int i = 0 ; i < v . Count ; i ++ ) Console . Write ( v [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void findTwoGroup ( int n ) { int sum = n * ( n + 1 ) / 2 ; int group1Sum = sum / 2 ; ArrayList group1 = new ArrayList ( ) ; ArrayList group2 = new ArrayList ( ) ; for ( int i = n ; i > 0 ; i -- ) { if ( group1Sum - i >= 0 ) { group1 . Add ( i ) ; group1Sum -= i ; } else { group2 . Add ( i ) ; } } printVector ( group1 ) ; printVector ( group2 ) ; } public static void Main ( ) { int n = 5 ; findTwoGroup ( n ) ; } }
Partition into two subarrays of lengths k and ( N | C # program to calculate max_difference between the sum of two subarrays of length k and N - k ; Function to calculate max_difference ; Sum of the array ; Sort the array in descending order ; Calculating max_difference ; Driver Code
using System ; class GFG { static int maxDifference ( int [ ] arr , int N , int k ) { int M , S = 0 , S1 = 0 , max_difference = 0 ; for ( int i = 0 ; i < N ; i ++ ) S += arr [ i ] ; int temp ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } M = Math . Max ( k , N - k ) ; for ( int i = 0 ; i < M ; i ++ ) S1 += arr [ i ] ; max_difference = S1 - ( S - S1 ) ; return max_difference ; } public static void Main ( ) { int [ ] arr = { 8 , 4 , 5 , 2 , 10 } ; int N = arr . Length ; int k = 2 ; Console . Write ( maxDifference ( arr , N , k ) ) ; } }
Minimum sum of product of two arrays | C # program to find minimum sum of product of two arrays with k operations allowed on first array . ; Function to find the minimum product ; Find product of current elements and update result . ; If both product and b [ i ] are negative , we must increase value of a [ i ] to minimize result . ; If both product and a [ i ] are negative , we must decrease value of a [ i ] to minimize result . ; Similar to above two cases for positive product . ; Check if current difference becomes higher than the maximum difference so far . ; Driver function
using System ; class GFG { static int minproduct ( int [ ] a , int [ ] b , int n , int k ) { int diff = 0 , res = 0 ; int temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int pro = a [ i ] * b [ i ] ; res = res + pro ; if ( pro < 0 && b [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro < 0 && a [ i ] < 0 ) temp = ( a [ i ] - 2 * k ) * b [ i ] ; else if ( pro > 0 && a [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro > 0 && a [ i ] > 0 ) temp = ( a [ i ] - 2 * k ) * b [ i ] ; int d = Math . Abs ( pro - temp ) ; if ( d > diff ) diff = d ; } return res - diff ; } public static void Main ( ) { int [ ] a = { 2 , 3 , 4 , 5 , 4 } ; int [ ] b = { 3 , 4 , 2 , 3 , 2 } ; int n = 5 , k = 3 ; Console . WriteLine ( minproduct ( a , b , n , k ) ) ; } }
Split n into maximum composite numbers | C # program to split a number into maximum number of composite numbers . ; function to calculate the maximum number of composite numbers adding upto n ; 4 is the smallest composite number ; stores the remainder when n is divided by 4 ; if remainder is 0 , then it is perfectly divisible by 4. ; if the remainder is 1 ; If the number is less then 9 , that is 5 , then it cannot be expressed as 4 is the only composite number less than 5 ; If the number is greater then 8 , and has a remainder of 1 , then express n as n - 9 a and it is perfectly divisible by 4 and for 9 , count 1. ; When remainder is 2 , just subtract 6 from n , so that n is perfectly divisible by 4 and count 1 for 6 which is subtracted . ; if the number is 7 , 11 which cannot be expressed as sum of any composite numbers ; when the remainder is 3 , then subtract 15 from it and n becomes perfectly divisible by 4 and we add 2 for 9 and 6 , which is getting subtracted to make n perfectly divisible by 4. ; Driver program
using System ; class GFG { static int count ( int n ) { if ( n < 4 ) return - 1 ; int rem = n % 4 ; if ( rem == 0 ) return n / 4 ; if ( rem == 1 ) { if ( n < 9 ) return - 1 ; return ( n - 9 ) / 4 + 1 ; } if ( rem == 2 ) return ( n - 6 ) / 4 + 1 ; if ( rem == 3 ) { if ( n < 15 ) return - 1 ; return ( n - 15 ) / 4 + 2 ; } return 0 ; } public static void Main ( ) { int n = 90 ; Console . WriteLine ( count ( n ) ) ; n = 143 ; Console . WriteLine ( count ( n ) ) ; } }
Minimum rotations to unlock a circular lock | C # program for min rotation to unlock ; function for min rotation ; iterate till input and unlock code become 0 ; input and unlock last digit as reminder ; find min rotation ; update code and input ; Driver Code
using System ; class GFG { static int minRotation ( int input , int unlock_code ) { int rotation = 0 ; int input_digit , code_digit ; while ( input > 0 unlock_code > 0 ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += Math . Min ( Math . Abs ( input_digit - code_digit ) , 10 - Math . Abs ( input_digit - code_digit ) ) ; input /= 10 ; unlock_code /= 10 ; } return rotation ; } public static void Main ( ) { int input = 28756 ; int unlock_code = 98234 ; Console . Write ( " Minimum ▁ Rotation ▁ = ▁ " + minRotation ( input , unlock_code ) ) ; } }
Minimum cost for acquiring all coins with k extra coins allowed with every coin | C # program to acquire all n coins at minimum cost with multiple values of k . ; Converts coin [ ] to prefix sum array ; sort the coins value ; Maintain prefix sum array ; Function to calculate min cost when we can get k extra coins after paying cost of one . ; calculate no . of coins needed ; return sum of from prefix array ; Driver Code
using System ; public class GFG { static void preprocess ( int [ ] coin , int n ) { Array . Sort ( coin ) ; for ( int i = 1 ; i <= n - 1 ; i ++ ) coin [ i ] += coin [ i - 1 ] ; } static int minCost ( int [ ] coin , int n , int k ) { int coins_needed = ( int ) Math . Ceiling ( 1.0 * n / ( k + 1 ) ) ; return coin [ coins_needed - 1 ] ; } static public void Main ( ) { int [ ] coin = { 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 } ; int n = coin . Length ; preprocess ( coin , n ) ; int k = 3 ; Console . WriteLine ( minCost ( coin , n , k ) ) ; k = 7 ; Console . WriteLine ( minCost ( coin , n , k ) ) ; } }
Program for Worst Fit algorithm in Memory Management | C # implementation of worst - Fit algorithm ; Method to allocate memory to blocks as per worst 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 code
using System ; class GFG { static void worstFit ( 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 wstIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( wstIdx == - 1 ) wstIdx = j ; else if ( blockSize [ wstIdx ] < blockSize [ j ] ) wstIdx = j ; } } if ( wstIdx != - 1 ) { allocation [ i ] = wstIdx ; blockSize [ wstIdx ] -= 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 TABSYMBOL " + processSize [ i ] + " TABSYMBOL TABSYMBOL TABSYMBOL " ) ; if ( allocation [ i ] != - 1 ) Console . Write ( allocation [ i ] + 1 ) ; else Console . Write ( " Not ▁ Allocated " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ ] blockSize = { 100 , 500 , 200 , 300 , 600 } ; int [ ] processSize = { 212 , 417 , 112 , 426 } ; int m = blockSize . Length ; int n = processSize . Length ; worstFit ( blockSize , m , processSize , n ) ; } }
Maximize array sum after K negations | Set 1 | C # program to maximize array sum after k operations . ; This function does k operations on array in a way that maximize the array sum . index -- > stores the index of current minimum element for j 'th operation ; Modify array K number of times ; Find minimum element in array for current operation and modify it i . e ; arr [ j ] -- > - arr [ j ] ; this the condition if we find 0 as minimum element , so it will useless to replace 0 by - ( 0 ) for remaining operations ; Modify element of array ; Calculate sum of array ; Driver code
using System ; class GFG { static int maximumSum ( int [ ] arr , int n , int k ) { for ( int i = 1 ; i <= k ; i ++ ) { int min = + 2147483647 ; int index = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < min ) { min = arr [ j ] ; index = j ; } } if ( min == 0 ) break ; arr [ index ] = - arr [ index ] ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } public static void Main ( ) { int [ ] arr = { - 2 , 0 , 5 , - 1 , 2 } ; int k = 4 ; int n = arr . Length ; Console . Write ( maximumSum ( arr , n , k ) ) ; } }
Maximize array sum after K negations | Set 1 | C # program to find maximum array sum after at most k negations . ; Sorting given array using in - built java sort function ; If we find a 0 in our sorted array , we stop ; Calculating sum ; Driver code
using System ; class GFG { static int sol ( int [ ] arr , int k ) { Array . Sort ( arr ) ; int sum = 0 ; int i = 0 ; while ( k > 0 ) { if ( arr [ i ] >= 0 ) k = 0 ; else { arr [ i ] = ( - 1 ) * arr [ i ] ; k = k - 1 ; } i ++ ; } for ( int j = 0 ; j < arr . Length ; j ++ ) { sum += arr [ j ] ; } return sum ; } public static void Main ( string [ ] args ) { int [ ] arr = { - 2 , 0 , 5 , - 1 , 2 } ; Console . Write ( sol ( arr , 4 ) ) ; } }
Maximize array sum after K negations | Set 1 | C # program for the above approach ; Function to calculate sum of the array ; Iterate from 0 to n - 1 ; Function to maximize sum ; Iterate from 0 to n - 1 ; Driver Code ; Function Call
using System ; class GFG { static int sumArray ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } return sum ; } static int maximizeSum ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( k != 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; continue ; } break ; } if ( i == n ) i -- ; if ( k == 0 k % 2 == 0 ) { return sumArray ( arr , n ) ; } if ( i != 0 && Math . Abs ( arr [ i ] ) >= Math . Abs ( arr [ i - 1 ] ) ) { i -- ; } arr [ i ] *= - 1 ; return sumArray ( arr , n ) ; } static public void Main ( ) { int n = 5 ; int k = 4 ; int [ ] arr = { - 3 , - 2 , - 1 , 5 , 6 } ; Console . Write ( maximizeSum ( arr , n , k ) ) ; } }
Bin Packing Problem ( Minimize number of used Bins ) | C # program to find number of bins required using First Fit algorithm . ; Returns number of bins required using first 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 first bin that can accommodate weight [ i ] ; If no bin could accommodate weight [ i ] ; Driver code
using System ; class GFG { static int firstFit ( int [ ] weight , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] ) { bin_rem [ j ] = bin_rem [ j ] - weight [ i ] ; break ; } } if ( j == res ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } } 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 ▁ First ▁ Fit ▁ : ▁ " + firstFit ( weight , n , c ) ) ; } }
Split given arrays into subarrays to maximize the sum of maximum and minimum in each subarrays | C # program for the above approach ; Function to find the maximum sum of differences of subarrays by splitting array into non - overlapping subarrays ; Stores the answer for prefix and initialize with zero ; Assume i - th index as right endpoint ; Choose the current value as the maximum and minimum ; Find the left endpoint and update the array dp [ ] ; Return answer ; Driver Code
using System ; class GFG { static int maxDiffSum ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int maxVal = arr [ i ] , minVal = arr [ i ] ; for ( int j = i ; j >= 0 ; j -- ) { minVal = Math . Min ( minVal , arr [ j ] ) ; maxVal = Math . Max ( maxVal , arr [ j ] ) ; if ( j - 1 >= 0 ) dp [ i ] = Math . Max ( dp [ i ] , maxVal - minVal + dp [ j - 1 ] ) ; else dp [ i ] = Math . Max ( dp [ i ] , maxVal - minVal ) ; } } return dp [ n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 8 , 1 , 7 , 9 , 2 } ; int N = arr . Length ; Console . WriteLine ( maxDiffSum ( arr , N ) ) ; } }
Count of N | C # program for the above approach ; Find the value of x raised to the yth power modulo MOD ; Stores the value of x ^ y ; Iterate until y is positive ; Function to perform the Modular Multiplicative Inverse using the Fermat 's little theorem ; Modular division x / y , find modular multiplicative inverse of y and multiply by x ; Function to find Binomial Coefficient C ( n , k ) in O ( k ) time ; Base Case ; Update the value of p and q ; Function to find the count of arrays having K as the first element satisfying the given criteria ; Stores the resultant count of arrays ; Find the factorization of K ; Stores the count of the exponent of the currentprime factor ; N is one last prime factor , for c = 1 -> C ( N - 1 + 1 , 1 ) = N ; Return the totol count ; Driver Code
using System ; public class GFG { public static int MOD = 1000000007 ; public static long modPow ( long x , long y ) { long r = 1 , a = x ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) { r = ( r * a ) % MOD ; } a = ( a * a ) % MOD ; y /= 2 ; } return r ; } public static long modInverse ( long x ) { return modPow ( x , MOD - 2 ) ; } public static long modDivision ( long p , long q ) { return ( p * modInverse ( q ) ) % MOD ; } public static long C ( long n , int k ) { if ( k > n ) { return 0 ; } long p = 1 , q = 1 ; for ( int i = 1 ; i <= k ; i ++ ) { q = ( q * i ) % MOD ; p = ( p * ( n - i + 1 ) ) % MOD ; } return modDivision ( p , q ) ; } public static long countArrays ( int N , int K ) { long res = 1 ; for ( int p = 2 ; p <= K / p ; p ++ ) { int c = 0 ; while ( K % p == 0 ) { K /= p ; c ++ ; } res = ( res * C ( N - 1 + c , c ) ) % MOD ; } if ( N > 1 ) { res = ( res * N ) % MOD ; } return res ; } static public void Main ( ) { int N = 3 , K = 5 ; Console . WriteLine ( countArrays ( N , K ) ) ; } }