codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence | C # program to find the Maximum sum of all elements which are not a part of longest increasing sub sequence ; Function to find maximum sum ; Find total sum of array ; Maintain a 2D array ; Update the dp array along with sum in the second row ; In case of greater length Update the length along with sum ; In case of equal length find length update length with minimum sum ; Find the sum that need to be subtracted from total sum ; Return the sum ; Driver code
using System ; class GFG { static int findSum ( int [ ] arr , int n ) { int totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; } int [ , ] dp = new int [ 2 , n ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ 0 , i ] = 1 ; dp [ 1 , i ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { if ( dp [ 0 , i ] < dp [ 0 , j ] + 1 ) { dp [ 0 , i ] = dp [ 0 , j ] + 1 ; dp [ 1 , i ] = dp [ 1 , j ] + arr [ i ] ; } else if ( dp [ 0 , i ] == dp [ 0 , j ] + 1 ) { dp [ 1 , i ] = Math . Min ( dp [ 1 , i ] , dp [ 1 , j ] + arr [ i ] ) ; } } } } int maxm = 0 ; int subtractSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ 0 , i ] > maxm ) { maxm = dp [ 0 , i ] ; subtractSum = dp [ 1 , i ] ; } else if ( dp [ 0 , i ] == maxm ) { subtractSum = Math . Min ( subtractSum , dp [ 1 , i ] ) ; } } return totalSum - subtractSum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 6 , 1 , 2 , 3 , 8 } ; int n = arr . Length ; Console . Write ( findSum ( arr , n ) ) ; } }
Count of possible permutations of a number represented as a sum of 2 ' s , ▁ 4' s and 6 's only | C # code for above implementation ; Returns number of ways to reach score n ; table [ i ] will store count of solutions for value i . ; Initialize all table values as 0 ; Base case ( If given value is 0 , 1 , 2 , or 4 ) ; Driver Code
using System ; class GFG { static int count ( int n ) { if ( n == 2 ) return 1 ; else if ( n == 4 ) return 2 ; else if ( n == 6 ) return 4 ; int [ ] table = new int [ n + 1 ] ; int i ; for ( i = 0 ; i < n + 1 ; i ++ ) table [ i ] = 0 ; table [ 0 ] = 0 ; table [ 2 ] = 1 ; table [ 4 ] = 2 ; table [ 6 ] = 4 ; for ( i = 8 ; i <= n ; i = i + 2 ) { table [ i ] = table [ i - 2 ] + table [ i - 4 ] + table [ i - 6 ] ; } return table [ n ] ; } public static void Main ( ) { int n = 8 ; Console . Write ( count ( n ) ) ; } }
Count of ways to split a given number into prime segments | C # implementation to count total number of ways to split a string to get prime numbers ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split String into prime numbers ; 1 based indexing ; Consider every suffix up to 6 digits ; Number should not have a leading zero and it should be a prime number ; Return the readonly result ; Driver code
using System ; class GFG { static readonly int MOD = 1000000007 ; static bool isPrime ( String number ) { int num = Int32 . Parse ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) { if ( ( num % i ) == 0 ) return false ; } return num > 1 ? true : false ; } static int countPrimeStrings ( String number , int i ) { if ( i == 0 ) return 1 ; int cnt = 0 ; for ( int j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 && number [ i - j ] != '0' && isPrime ( number . Substring ( i - j , j ) ) ) { cnt += countPrimeStrings ( number , i - j ) ; cnt %= MOD ; } } return cnt ; } public static void Main ( String [ ] args ) { String s1 = "3175" ; int l = s1 . Length ; Console . Write ( countPrimeStrings ( s1 , l ) ) ; } }
Count of ways to split a given number into prime segments | C # implementation to count total number of ways to split a String to get prime numbers ; Function to build sieve ; If p is a prime ; Update all multiples of p as non prime ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split String into prime numbers ; Number should not have a leading zero and it should be a prime number ; Function to count the number of prime Strings ; Driver code
using System ; class GFG { static int MOD = 1000000007 ; static bool [ ] sieve = new bool [ 1000000 ] ; static void buildSieve ( ) { for ( int j = 0 ; j < sieve . Length ; j ++ ) sieve [ j ] = true ; sieve [ 0 ] = false ; sieve [ 1 ] = false ; for ( int p = 2 ; p * p <= 1000000 ; p ++ ) { if ( sieve [ p ] == true ) { for ( int i = p * p ; i < 1000000 ; i += p ) sieve [ i ] = false ; } } } static bool isPrime ( String number ) { int num = Int32 . Parse ( number ) ; return sieve [ num ] ; } static int rec ( String number , int i , int [ ] dp ) { if ( dp [ i ] != - 1 ) return dp [ i ] ; int cnt = 0 ; for ( int j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 && number [ i - j ] != '0' && isPrime ( number . Substring ( i - j , j ) ) ) { cnt += rec ( number , i - j , dp ) ; cnt %= MOD ; } } return dp [ i ] = cnt ; } static int countPrimeStrings ( String number ) { int n = number . Length ; int [ ] dp = new int [ n + 1 ] ; for ( int j = 0 ; j < dp . Length ; j ++ ) dp [ j ] = - 1 ; dp [ 0 ] = 1 ; return rec ( number , n , dp ) ; } public static void Main ( String [ ] args ) { buildSieve ( ) ; String s1 = "3175" ; Console . Write ( countPrimeStrings ( s1 ) + " STRNEWLINE " ) ; } }
Check if a matrix contains a square submatrix with 0 as boundary element | C # implementation of the above approach ; Function checks if square with all 0 's in boundary  exists in the matrix ; r1 is the top row , c1 is the left col r2 is the bottom row , c2 is the right ; Function checks if the boundary of the square consists of 0 's ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int squareOfZeroes ( int [ , ] matrix ) { int lastIdx = matrix . GetLength ( 0 ) - 1 ; Dictionary < string , bool > cache = new Dictionary < string , bool > ( ) ; if ( hasSquareOfZeroes ( matrix , 0 , 0 , lastIdx , lastIdx , cache ) ) { return 1 ; } else { return 0 ; } } static bool hasSquareOfZeroes ( int [ , ] matrix , int r1 , int c1 , int r2 , int c2 , Dictionary < string , bool > cache ) { if ( r1 >= r2 c1 >= c2 ) { return false ; } string key = r1 + " - " + c1 + " - " + r2 + " - " + c2 ; if ( cache . ContainsKey ( key ) ) { return cache [ key ] ; } cache [ key ] = ( isSquareOfZeroes ( matrix , r1 , c1 , r2 , c2 ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 + 1 , r2 - 1 , c2 - 1 , cache ) || hasSquareOfZeroes ( matrix , r1 , c1 + 1 , r2 - 1 , c2 , cache ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 , r2 , c2 - 1 , cache ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 + 1 , r2 , c2 , cache ) || hasSquareOfZeroes ( matrix , r1 , c1 , r2 - 1 , c2 - 1 , cache ) ) ; return cache [ key ] ; } static bool isSquareOfZeroes ( int [ , ] matrix , int r1 , int c1 , int r2 , int c2 ) { for ( int row = r1 ; row < r2 + 1 ; row ++ ) { if ( matrix [ row , c1 ] != 0 matrix [ row , c2 ] != 0 ) { return false ; } } for ( int col = c1 ; col < c2 + 1 ; col ++ ) { if ( matrix [ r1 , col ] != 0 matrix [ r2 , col ] != 0 ) { return false ; } } return true ; } static public void Main ( ) { int [ , ] matrix = { { 1 , 1 , 1 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 1 , 0 , 1 } , { 0 , 1 , 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 1 } } ; int ans ; ans = squareOfZeroes ( matrix ) ; if ( ans == 1 ) { Console . WriteLine ( " True " ) ; } else { Console . WriteLine ( " False " ) ; } } }
Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | C # program to calculate the number of ways ; Initialising the array with zeros . You can do using memset too . ; Printing the array ; Driver code
using System ; class GFG { public static void CountWays ( int a , int b , int c ) { int x = b + c + 1 ; int [ ] arr = new int [ x ] ; for ( int i = a ; i <= b ; i ++ ) { for ( int j = b ; j <= c ; j ++ ) { arr [ i + j ] ++ ; } } for ( int i = 1 ; i < x ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int a = 1 ; int b = 2 ; int c = 2 ; CountWays ( a , b , c ) ; } }
Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | C # program to calculate the number of ways ; 2 is added because sometimes we will decrease the value out of bounds . ; Initialising the array with zeros . ; Printing the array ; Driver code
using System ; class GFG { static void CountWays ( int a , int b , int c ) { int x = b + c + 2 ; int [ ] arr = new int [ x ] ; for ( int i = 1 ; i <= b ; i ++ ) { arr [ i + b ] ++ ; arr [ i + c + 1 ] -- ; } for ( int i = 1 ; i < x - 1 ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( ) { int a = 1 ; int b = 2 ; int c = 2 ; CountWays ( a , b , c ) ; } }
Count of distinct possible strings after performing given operations | C # implementation of the above approach ; Function that prints the number of different strings that can be formed ; Computing the length of the given string ; Base case ; Traverse the given string ; If two consecutive 1 ' s ▁ ▁ or ▁ 2' s are present ; Otherwise take the previous value ; Driver code
using System ; class GFG { static void differentStrings ( string s ) { int n = s . Length ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] && ( s [ i ] == '1' s [ i ] == '2' ) ) dp [ i + 1 ] = dp [ i ] + dp [ i - 1 ] ; else dp [ i + 1 ] = dp [ i ] ; } Console . Write ( dp [ n ] ) ; } public static void Main ( ) { string S = "0111022110" ; differentStrings ( S ) ; } }
Sum of cost of all paths to reach a given cell in a Matrix | C # implementation to find the sum of cost of all paths to reach ( M , N ) ; Function for computing combination ; Function to find the factorial of N ; Loop to find the factorial of a given number ; Function for coumputing the sum of all path cost ; Loop to find the contribution of each ( i , j ) in the all possible ways ; Count number of times ( i , j ) visited ; Add the contribution of grid [ i ] [ j ] in the result ; Driver code ; Function Call
using System ; class GFG { static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int sumPathCost ( int [ , ] grid , int m , int n ) { int sum = 0 , count ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { count = nCr ( i + j , i ) * nCr ( m + n - i - j , m - i ) ; sum += count * grid [ i , j ] ; } } return sum ; } public static void Main ( ) { int m = 2 ; int n = 2 ; int [ , ] grid = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Console . Write ( sumPathCost ( grid , m , n ) ) ; } }
Count numbers in a range with digit sum divisible by K having first and last digit different | C # program to count numbers in a range with digit sum divisible by K having first and last digit different ; For calculating the upper bound of the sequence . ; Checking whether the sum of digits = 0 or not and the corner case as number equal to 1 ; If the state is visited then return the answer of this state directly . ; For checking whether digit to be placed is up to upper bound at the position pos or upto 9 ; Calculating new digit sum modulo k ; Check if there is a prefix of 0 s and current digit != 0 ; Then current digit will be the starting digit ; Update the bool flag that the starting digit has been found ; At n - 1 , check if current digit and starting digit are the same then no need to calculate this answer . ; Else find the answer ; Function to find the required count ; Setting up the upper bound ; Calculating F ( R ) ; Setting up the upper bound ; calculating F ( L - 1 ) ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int K ; static int N ; static List < int > v = new List < int > ( ) ; static int [ , , , , ] dp = new int [ 20 , 1000 , 10 , 2 , 2 ] ; static void init ( int x ) { for ( int i = 0 ; i < 20 ; i ++ ) for ( int j = 0 ; j < 1000 ; j ++ ) for ( int k = 0 ; k < 10 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) for ( int m = 0 ; m < 2 ; m ++ ) dp [ i , j , k , l , m ] = - 1 ; v . Clear ( ) ; while ( x > 0 ) { v . Add ( x % 10 ) ; x /= 10 ; } v . Reverse ( ) ; N = v . Count ; } static int fun ( int pos , int sum , int st , int check , int f ) { if ( pos == N ) { return ( sum == 0 && check == 1 ) ? 1 : 0 ; } if ( dp [ pos , sum , st , check , f ] != - 1 ) return dp [ pos , sum , st , check , f ] ; int lmt = 9 ; if ( f == 0 ) lmt = v [ pos ] ; int ans = 0 ; for ( int digit = 0 ; digit <= lmt ; digit ++ ) { int nf = f ; int new_sum = ( sum + digit ) % K ; int new_check = check ; int new_st = st ; if ( f == 0 && digit < lmt ) nf = 1 ; if ( check == 0 && digit != 0 ) { new_st = digit ; new_check = 1 ; } if ( pos == N - 1 && new_st == digit ) continue ; ans += fun ( pos + 1 , new_sum , new_st , new_check , nf ) ; } return dp [ pos , sum , st , check , f ] = ans ; } static void findCount ( int L , int R , int K ) { init ( R ) ; int r_ans = fun ( 0 , 0 , 0 , 0 , 0 ) ; init ( L - 1 ) ; int l_ans = fun ( 0 , 0 , 0 , 0 , 0 ) ; Console . Write ( r_ans - l_ans ) ; } public static void Main ( String [ ] args ) { int L = 10 ; int R = 20 ; K = 2 ; findCount ( L , R , K ) ; } }
Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1 | C # program to split a given String into K segments such that the sum of product of occurence of characters in them is minimized ; Function to return the minimum sum of products of occurences of 0 and 1 in each segments ; Store the length of the String ; Not possible to generate subsets greater than the length of String ; If the number of subsets equals the length ; Precompute the sum of products for all index ; Calculate the minimum sum of products for K subsets ; Driver code
using System ; class GFG { static int minSumProd ( string S , int K ) { int len = S . Length ; if ( K > len ) return - 1 ; if ( K == len ) return 0 ; int [ ] dp = new int [ len ] ; int count_zero = 0 , count_one = 0 ; for ( int j = 0 ; j < len ; j ++ ) { if ( S [ j ] == '0' ) { count_zero ++ ; } else { count_one ++ ; } dp [ j ] = count_zero * count_one ; } for ( int i = 1 ; i < K ; i ++ ) { for ( int j = len - 1 ; j >= i ; j -- ) { count_zero = 0 ; count_one = 0 ; dp [ j ] = Int32 . MaxValue ; for ( int k = j ; k >= i ; k -- ) { if ( S [ k ] == '0' ) { count_zero ++ ; } else { count_one ++ ; } dp [ j ] = Math . Min ( dp [ j ] , count_zero * count_one + dp [ k - 1 ] ) ; } } } return dp [ len - 1 ] ; } public static void Main ( string [ ] args ) { string S = "1011000110110100" ; int K = 5 ; Console . Write ( minSumProd ( S , K ) ) ; } }
Minimize prize count required such that smaller value gets less prize in an adjacent pair | C # implementation to find the minimum prizes required such that adjacent smaller elements gets less number of prizes ; Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes ; Loop to compute the smaller elements at the left ; Loop to find the smaller elements at the right ; Loop to find the minimum prizes required ; Driver Code
using System ; class GFG { static int minPrizes ( int [ ] arr , int n ) { int [ ] dpLeft = new int [ n ] ; dpLeft [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { dpLeft [ i ] = dpLeft [ i - 1 ] + 1 ; } else { dpLeft [ i ] = 1 ; } } int [ ] dpRight = new int [ n ] ; dpRight [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { dpRight [ i ] = dpRight [ i + 1 ] + 1 ; } else { dpRight [ i ] = 1 ; } } int totalPrizes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalPrizes += Math . Max ( dpLeft [ i ] , dpRight [ i ] ) ; } Console . Write ( totalPrizes + " STRNEWLINE " ) ; return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 } ; int n = arr . Length ; minPrizes ( arr , n ) ; } }
Maximum sum subsequence with values differing by at least 2 | C # program to find maximum sum subsequence with values differing by at least 2 ; Function to find maximum sum subsequence such that two adjacent values elements are not selected ; map to store the frequency of array elements ; Make a dp array to store answer upto i th value ; Base cases ; Iterate for all possible values of arr [ i ] ; Return the last value ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int get_max_sum ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { int x = freq [ arr [ i ] ] ; freq [ arr [ i ] ] = x + 1 ; } else freq . Add ( arr [ i ] , 1 ) ; } int [ ] dp = new int [ 100001 ] ; for ( int i = 0 ; i < 100001 ; i ++ ) dp [ i ] = 0 ; dp [ 0 ] = 0 ; if ( freq . ContainsKey ( 0 ) ) dp [ 1 ] = freq [ 0 ] ; else dp [ 1 ] = 0 ; for ( int i = 2 ; i <= 100000 ; i ++ ) { int temp = ( freq . ContainsKey ( i ) ) ? freq [ i ] : 0 ; dp [ i ] = Math . Max ( dp [ i - 1 ] , dp [ i - 2 ] + i * temp ) ; } return dp [ 100000 ] ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ ] arr = { 2 , 2 , 3 } ; Console . WriteLine ( get_max_sum ( arr , N ) ) ; } }
Minimum flips required to keep all 1 s together in a Binary string | C # implementation for Minimum number of flips required in a binary string such that all the 1 aTMs are together ; Length of the binary string ; Initial state of the dp dp [ 0 ] [ 0 ] will be 1 if the current bit is 1 and we have to flip it ; Initial state of the dp dp [ 0 ] [ 1 ] will be 1 if the current bit is 0 and we have to flip it ; dp [ i ] [ 0 ] = Flips required to make all previous bits zero + Flip required to make current bit zero ; dp [ i ] [ 1 ] = minimum flips required to make all previous states 0 or make previous states 1 satisfying the condition ; Minimum of answer and flips required to make all bits 0 ; Driver code
using System ; class GFG { static int minFlip ( string a ) { int n = a . Length ; int [ , ] dp = new int [ n + 1 , 2 ] ; dp [ 0 , 0 ] = ( a [ 0 ] == '1' ? 1 : 0 ) ; dp [ 0 , 1 ] = ( a [ 0 ] == '0' ? 1 : 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i , 0 ] = dp [ i - 1 , 0 ] + ( a [ i ] == '1' ? 1 : 0 ) ; dp [ i , 1 ] = Math . Min ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) + ( a [ i ] == '0' ? 1 : 0 ) ; } int answer = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { answer = Math . Min ( answer , dp [ i , 1 ] + dp [ n - 1 , 0 ] - dp [ i , 0 ] ) ; } return Math . Min ( answer , dp [ n - 1 , 0 ] ) ; } public static void Main ( string [ ] args ) { string s = "1100111000101" ; Console . Write ( minFlip ( s ) ) ; } }
Length of longest increasing index dividing subsequence | C # program to find the length of the longest increasing sub - sequence such that the index of the element is divisible by index of previous element ; Function to find the length of the longest increasing sub - sequence such that the index of the element is divisible by index of previous element ; Initialize the dp [ ] array with 1 as a single element will be of 1 length ; Traverse the given array ; If the index is divisible by the previous index ; If increasing subsequence identified ; Longest length is stored ; Driver code
using System ; class GFG { static int LIIDS ( int [ ] arr , int N ) { int [ ] dp = new int [ N + 1 ] ; int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { dp [ i ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i + i ; j <= N ; j += i ) { if ( j < arr . Length && arr [ j ] > arr [ i ] ) { dp [ j ] = Math . Max ( dp [ j ] , dp [ i ] + 1 ) ; } } ans = Math . Max ( ans , dp [ i ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 7 , 9 , 10 } ; int N = arr . Length ; Console . WriteLine ( LIIDS ( arr , N ) ) ; } }
Count of subarrays having exactly K perfect square numbers | C # program to count of subarrays having exactly K perfect square numbers . ; A utility function to check if the number n is perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to find number of subarrays with sum exactly equal to k ; Map to store number of subarrays starting from index zero having particular value of sum . ; To store the sum of element traverse so far ; Add current element to currsum ; If currsum = K , then a new subarray is found ; If currsum > K then find the no . of subarrays with sum currsum - K and exclude those subarrays ; Add currsum to count of different values of sum ; Return the final result ; Function to count the subarray with K perfect square numbers ; Update the array element ; If current element is perfect square then update the arr [ i ] to 1 ; Else change arr [ i ] to 0 ; Function call ; Driver Code ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static bool isPerfectSquare ( double x ) { double sr = Math . Sqrt ( x ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ; } static int findSubarraySum ( int [ ] arr , int n , int K ) { Dictionary < int , int > prevSum = new Dictionary < int , int > ( ) ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == K ) { res ++ ; } if ( prevSum . ContainsKey ( currsum - K ) ) res += ( prevSum [ currsum - K ] ) ; if ( prevSum . ContainsKey ( currsum ) ) { prevSum [ currsum ] ++ ; } else { prevSum [ currsum ] = 1 ; } } return res ; } static void countSubarray ( int [ ] arr , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) { if ( isPerfectSquare ( arr [ i ] ) ) { arr [ i ] = 1 ; } else { arr [ i ] = 0 ; } } Console . Write ( findSubarraySum ( arr , n , K ) ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 9 , 2 } ; int K = 2 ; int N = arr . Length ; countSubarray ( arr , N , K ) ; } }
Number of ways to split N as sum of K numbers from the given range | C # implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] ; Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] ; Base Case ; If N is divides completely into less than k groups ; Put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Driver Code
using System ; class GFG { static int mod = 1000000007 ; static int calculate ( int pos , int left , int k , int L , int R ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; int answer = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( i > left ) break ; answer = ( ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod ) ; } return answer ; } static int countWaystoDivide ( int n , int k , int L , int R ) { return calculate ( 0 , n , k , L , R ) ; } public static void Main ( String [ ] args ) { int N = 12 ; int K = 3 ; int L = 1 ; int R = 5 ; Console . Write ( countWaystoDivide ( N , K , L , R ) ) ; } }
Check if it is possible to get given sum by taking one element from each row | C # implementation to find whether is it possible to make sum equal to K ; Function that prints whether is it possible to make sum equal to K ; Base case ; Condition if we can make sum equal to current column by using above rows ; Iterate through current column and check whether we can make sum less than or equal to k ; Printing whether is it possible or not ; Driver code
using System ; class GFG { static void PossibleSum ( int n , int m , int [ , ] v , int k ) { int [ , ] dp = new int [ n + 1 , k + 1 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= k ; j ++ ) { if ( dp [ i , j ] == 1 ) { for ( int d = 0 ; d < m ; d ++ ) { if ( ( j + v [ i , d ] ) <= k ) { dp [ i + 1 , j + v [ i , d ] ] = 1 ; } } } } } if ( dp [ n , k ] == 1 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int N = 2 , M = 10 , K = 5 ; int [ , ] arr = new int [ , ] { { 4 , 0 , 15 , 3 , 2 , 20 , 10 , 1 , 5 , 4 } , { 4 , 0 , 10 , 3 , 2 , 25 , 4 , 1 , 5 , 4 } } ; PossibleSum ( N , M , arr , K ) ; } }