codeparrot/xlcost-text-to-code huggingface dataset:
Queries on number of Binary sub | C # Program to answer queries on number of submatrix of given size ; Return the minimum of three numbers ; Solve each query on matrix ; For each of the cell . ; finding submatrix size of oth row and column . ; intermediate cells . ; Find frequency of each distinct size for 0 s and 1 s . ; Find the Cumulative Sum . ; Output the answer for each query ; Driver Code
using System ; class GFG { static int MAX = 100 ; static int N = 5 ; static int M = 4 ; static int min ( int a , int b , int c ) { return Math . Min ( a , Math . Min ( b , c ) ) ; } static void solveQuery ( int n , int m , int [ , ] mat , int q , int [ ] a , int [ ] binary ) { int [ , ] dp = new int [ n , m ] ; int max = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i , j ] = 1 ; else if ( ( mat [ i , j ] == mat [ i - 1 , j ] ) && ( mat [ i , j ] == mat [ i , j - 1 ] ) && ( mat [ i , j ] == mat [ i - 1 , j - 1 ] ) ) { dp [ i , j ] = min ( dp [ i - 1 , j ] , dp [ i - 1 , j - 1 ] , dp [ i , j - 1 ] ) + 1 ; if ( max < dp [ i , j ] ) max = dp [ i , j ] ; } else dp [ i , j ] = 1 ; } } int [ ] freq0 = new int [ MAX ] ; int [ ] freq1 = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i , j ] == 0 ) freq0 [ dp [ i , j ] ] ++ ; else freq1 [ dp [ i , j ] ] ++ ; } } for ( int i = max - 1 ; i >= 0 ; i -- ) { freq0 [ i ] += freq0 [ i + 1 ] ; freq1 [ i ] += freq1 [ i + 1 ] ; } for ( int i = 0 ; i < q ; i ++ ) { if ( binary [ i ] == 0 ) Console . WriteLine ( freq0 [ a [ i ] ] ) ; else Console . WriteLine ( freq1 [ a [ i ] ] ) ; } } public static void Main ( ) { int n = 5 , m = 4 ; int [ , ] mat = { { 0 , 0 , 1 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 } , { 0 , 1 , 1 , 1 } } ; int q = 2 ; int [ ] a = { 2 , 2 } ; int [ ] binary = { 1 , 0 } ; solveQuery ( n , m , mat , q , a , binary ) ; } }
Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space | C # program to implement wildcard pattern matching algorithm ; Function that matches input text with given wildcard pattern ; empty pattern can only match with empty string . Base Case : ; step - 1 : initialize markers : ; For step - ( 2 , 5 ) ; For step - ( 3 ) ; For step - ( 4 ) ; For step - ( 5 ) ; For step - ( 6 ) ; For step - ( 7 ) ; Final Check ; Driver code
using System ; class GFG { static Boolean strmatch ( char [ ] txt , char [ ] pat , int n , int m ) { if ( m == 0 ) return ( n == 0 ) ; int i = 0 , j = 0 , index_txt = - 1 , index_pat = - 1 ; while ( i < n ) { if ( j < m && txt [ i ] == pat [ j ] ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' ? ' ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' * ' ) { index_txt = i ; index_pat = j ; j ++ ; } else if ( index_pat != - 1 ) { j = index_pat + 1 ; i = index_txt + 1 ; index_txt ++ ; } else { return false ; } } while ( j < m && pat [ j ] == ' * ' ) { j ++ ; } if ( j == m ) { return true ; } return false ; } public static void Main ( String [ ] args ) { char [ ] str = " baaabab " . ToCharArray ( ) ; char [ ] pattern = " * * * * * ba * * * * * ab " . ToCharArray ( ) ; char pattern [ ] = " ba * * * * * ab " ; char pattern [ ] = " ba * ab " ; char pattern [ ] = " a * ab " ; if ( strmatch ( str , pattern , str . Length , pattern . Length ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; char [ ] pattern2 = " a * * * * * ab " . ToCharArray ( ) ; if ( strmatch ( str , pattern2 , str . Length , pattern2 . Length ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find n | C # program to find the nth element of Stern 's Diatomic Series ; function to find nth stern ' diatomic series ; Initializing the DP array ; SET the Base case ; Traversing the array from 2 nd Element to nth Element ; Case 1 : for even n ; Case 2 : for odd n ; Driver Code
using System ; class GFG { static int findSDSFunc ( int n ) { int [ ] DP = new int [ n + 1 ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) DP [ i ] = DP [ i / 2 ] ; else DP [ i ] = DP [ ( i - 1 ) / 2 ] + DP [ ( i + 1 ) / 2 ] ; } return DP [ n ] ; } static public void Main ( ) { int n = 15 ; Console . WriteLine ( findSDSFunc ( n ) ) ; } }
Check if any valid sequence is divisible by M | C # implementation of above approach ; Calculate modulo for this call ; Base case ; check if sum is divisible by M ; check if the current state is already computed ; 1. Try placing ' + ' ; 2. Try placing ' - ' ; calculate value of res for recursive case ; store the value for res for current states and return for parent call ; Driver code ; MAX is the Maximum value M can take
using System ; class GFG { static int MAX = 100 ; static int isPossible ( int n , int index , int modulo , int M , int [ ] arr , int [ , ] dp ) { modulo = ( ( modulo % M ) + M ) % M ; if ( index == n ) { if ( modulo == 0 ) { return 1 ; } return 0 ; } if ( dp [ index , modulo ] != - 1 ) { return dp [ index , modulo ] ; } int placeAdd = isPossible ( n , index + 1 , modulo + arr [ index ] , M , arr , dp ) ; int placeMinus = isPossible ( n , index + 1 , modulo - arr [ index ] , M , arr , dp ) ; int res = placeAdd ; dp [ index , modulo ] = res ; return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 6 } ; int n = arr . Length ; int M = 4 ; int [ , ] dp = new int [ n + 1 , MAX ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = - 1 ; } } bool res ; if ( isPossible ( n , 1 , arr [ 0 ] , M , arr , dp ) == 1 ) { res = true ; } else { res = false ; } Console . WriteLine ( res ? " True " : " False " ) ; } }
Find common elements in three sorted arrays | C # program to find common elements in three arrays ; This function prints common element s in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Iterate through three arrays while all arrays have elements ; If x = y and y = z , print any of them and move ahead in all arrays ; x < y ; y < z ; We reach here when x > y and z < y , i . e . , z is smallest ; Driver code to test above
using System ; class GFG { static void findCommon ( int [ ] ar1 , int [ ] ar2 , int [ ] ar3 ) { int i = 0 , j = 0 , k = 0 ; while ( i < ar1 . Length && j < ar2 . Length && k < ar3 . Length ) { if ( ar1 [ i ] == ar2 [ j ] && ar2 [ j ] == ar3 [ k ] ) { Console . Write ( ar1 [ i ] + " ▁ " ) ; i ++ ; j ++ ; k ++ ; } else if ( ar1 [ i ] < ar2 [ j ] ) i ++ ; else if ( ar2 [ j ] < ar3 [ k ] ) j ++ ; else k ++ ; } } public static void Main ( ) { int [ ] ar1 = { 1 , 5 , 10 , 20 , 40 , 80 } ; int [ ] ar2 = { 6 , 7 , 20 , 80 , 100 } ; int [ ] ar3 = { 3 , 4 , 15 , 20 , 30 , 70 , 80 , 120 } ; Console . Write ( " Common ▁ elements ▁ are ▁ " ) ; findCommon ( ar1 , ar2 , ar3 ) ; } }
Dynamic Programming on Trees | Set | C # code to find the maximum path sum ; function for dfs traversal and to store the maximum value in [ ] dp for every node till the leaves ; initially dp [ u ] is always a [ u ] ; stores the maximum value from nodes ; traverse the tree ; if child is parent , then we continue without recursing further ; call dfs for further traversal ; store the maximum of previous visited node and present visited node ; add the maximum value returned to the parent node ; function that returns the maximum value ; Driver Code ; Number of nodes ; adjacency list ; create undirected edges initialize the tree given in the diagram ; values of node 1 , 2 , 3. ... 14 ; function call
using System ; using System . Collections . Generic ; class GFG { static int [ ] dp = new int [ 100 ] ; static void dfs ( int [ ] a , List < int > [ ] v , int u , int parent ) { dp [ u ] = a [ u - 1 ] ; int maximum = 0 ; foreach ( int child in v [ u ] ) { if ( child == parent ) continue ; dfs ( a , v , child , u ) ; maximum = Math . Max ( maximum , dp [ child ] ) ; } dp [ u ] += maximum ; } static int maximumValue ( int [ ] a , List < int > [ ] v ) { dfs ( a , v , 1 , 0 ) ; return dp [ 1 ] ; } public static void Main ( String [ ] args ) { int n = 14 ; List < int > [ ] v = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < v . Length ; i ++ ) v [ i ] = new List < int > ( ) ; v [ 1 ] . Add ( 2 ) ; v [ 2 ] . Add ( 1 ) ; v [ 1 ] . Add ( 3 ) ; v [ 3 ] . Add ( 1 ) ; v [ 1 ] . Add ( 4 ) ; v [ 4 ] . Add ( 1 ) ; v [ 2 ] . Add ( 5 ) ; v [ 5 ] . Add ( 2 ) ; v [ 2 ] . Add ( 6 ) ; v [ 6 ] . Add ( 2 ) ; v [ 3 ] . Add ( 7 ) ; v [ 7 ] . Add ( 3 ) ; v [ 4 ] . Add ( 8 ) ; v [ 8 ] . Add ( 4 ) ; v [ 4 ] . Add ( 9 ) ; v [ 9 ] . Add ( 4 ) ; v [ 4 ] . Add ( 10 ) ; v [ 10 ] . Add ( 4 ) ; v [ 5 ] . Add ( 11 ) ; v [ 11 ] . Add ( 5 ) ; v [ 5 ] . Add ( 12 ) ; v [ 12 ] . Add ( 5 ) ; v [ 7 ] . Add ( 13 ) ; v [ 13 ] . Add ( 7 ) ; v [ 7 ] . Add ( 14 ) ; v [ 14 ] . Add ( 7 ) ; int [ ] a = { 3 , 2 , 1 , 10 , 1 , 3 , 9 , 1 , 5 , 3 , 4 , 5 , 9 , 8 } ; Console . WriteLine ( maximumValue ( a , v ) ) ; } }
Jacobsthal and Jacobsthal | A DP based solution to find Jacobsthal and Jacobsthal - Lucas numbers ; Return nth Jacobsthal number . ; base case ; Return nth Jacobsthal - Lucas number . ; base case ; Driver Code
using System ; public class GfG { public static int Jacobsthal ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; return dp [ n ] ; } public static int Jacobsthal_Lucas ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 2 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; return dp [ n ] ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( " Jacobsthal ▁ number : ▁ " + Jacobsthal ( n ) ) ; Console . WriteLine ( " Jacobsthal - Lucas ▁ number : ▁ " + Jacobsthal_Lucas ( n ) ) ; } }
Find position of an element in a sorted array of infinite numbers | C # program to demonstrate working of an algorithm that finds an element in an array of infinite size ; Simple binary search algorithm ; Method takes an infinite size array and a key to be searched and returns its position if found else - 1. We don 't know size of arr[] and we can  assume size to be infinite in this  function.  NOTE THAT THIS FUNCTION ASSUMES  arr[] TO BE OF INFINITE SIZE  THEREFORE, THERE IS NO INDEX OUT  OF BOUND CHECKING ; Find h to do binary search ; store previous high ; double high index ; update new val ; at this point we have updated low and high indices , thus use binary search between them ; Driver method to test the above function
using System ; class GFG { static int binarySearch ( int [ ] arr , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } static int findPos ( int [ ] arr , int key ) { int l = 0 , h = 1 ; int val = arr [ 0 ] ; while ( val < key ) { l = h ; h = 2 * h ; val = arr [ h ] ; } return binarySearch ( arr , l , h , key ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 } ; int ans = findPos ( arr , 10 ) ; if ( ans == - 1 ) Console . Write ( " Element ▁ not ▁ found " ) ; else Console . Write ( " Element ▁ found ▁ at ▁ " + " index ▁ " + ans ) ; } }
Count of possible hexagonal walks | C # implementation of counting number of possible hexagonal walks ; We initialize our origin with 1 ; For each N = 1 to 14 , we traverse in all possible direction . Using this 3D array we calculate the number of ways at each step and the total ways for a given step shall be found at ways [ step number ] [ 8 ] [ 8 ] because all the steps after that will be used to trace back to the original point index 0 : 0 according to the image . ; This array stores the number of ways possible for a given step ; Driver program to test above function ; Preprocessing all possible ways
using System ; class GFG { static int depth = 14 ; static int [ , , ] ways = new int [ 16 , 16 , 16 ] ; static int stepNum ; static void preprocess ( int [ ] list ) { ways [ 0 , 8 , 8 ] = 1 ; for ( int N = 1 ; N <= 14 ; N ++ ) { for ( int i = 1 ; i < depth ; i ++ ) { for ( int j = 1 ; j < depth ; j ++ ) { ways [ N , i , j ] = ways [ N - 1 , i , j + 1 ] + ways [ N - 1 , i , j - 1 ] + ways [ N - 1 , i + 1 , j ] + ways [ N - 1 , i - 1 , j ] + ways [ N - 1 , i + 1 , j - 1 ] + ways [ N - 1 , i - 1 , j + 1 ] ; } } list [ N ] = ways [ N , 8 , 8 ] ; } } public static void Main ( ) { int [ ] list = new int [ 15 ] ; preprocess ( list ) ; int steps = 4 ; Console . WriteLine ( " Number ▁ of ▁ walks " + " ▁ possible ▁ is / are ▁ " + list [ steps ] ) ; } }
Check if possible to cross the matrix with given power | C # program to find if it is possible to cross the matrix with given power ; Initializing array dp with false value . ; For each value of dp [ i ] [ j ] [ k ] ; For first cell and for each value of k ; For first cell of each row ; For first cell of each column ; For rest of the cell ; Down movement . ; Right movement . ; Diagonal movement . ; Finding maximum k . ; Driver code
using System ; class GFG { static int N = 105 ; static int R = 3 ; static int C = 4 ; static int maximumValue ( int n , int m , int p , int [ , ] grid ) { bool [ , , ] dp = new bool [ N , N , N ] ; int i , j , k ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { for ( k = 0 ; k < N ; k ++ ) dp [ i , j , k ] = false ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { for ( k = grid [ i , j ] ; k <= p ; k ++ ) { if ( i == 0 && j == 0 ) { if ( k == grid [ i , j ] ) dp [ i , j , k ] = true ; } else if ( i == 0 ) { dp [ i , j , k ] = ( dp [ i , j , k ] dp [ i , j - 1 , k - grid [ i , j ] ] ) ; } else if ( j == 0 ) { dp [ i , j , k ] = ( dp [ i , j , k ] dp [ i - 1 , j , k - grid [ i , j ] ] ) ; } else { dp [ i , j , k ] = ( dp [ i , j , k ] dp [ i , j - 1 , k - grid [ i , j ] ] ) ; dp [ i , j , k ] = ( dp [ i , j , k ] dp [ i - 1 , j , k - grid [ i , j ] ] ) ; dp [ i , j , k ] = ( dp [ i , j , k ] dp [ i - 1 , j - 1 , k - grid [ i , j ] ] ) ; } } } } k = p ; int ans = 0 ; for ( ans = k ; ans >= 0 ; ans -- ) if ( dp [ n - 1 , m - 1 , ans ] ) break ; return ans ; } public static void Main ( ) { int n = 3 , m = 4 , p = 9 ; int [ , ] grid = { { 2 , 3 , 4 , 1 } , { 6 , 5 , 5 , 3 } , { 5 , 2 , 3 , 4 } } ; Console . WriteLine ( maximumValue ( n , m , p , grid ) ) ; } }
Number of n digit stepping numbers | C # program to calculate the number of n digit stepping numbers . ; function that calculates the answer ; dp [ i ] [ j ] stores count of i digit stepping numbers ending with digit j . ; if n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; driver program to test the above function
using System ; class GFG { static long answer ( int n ) { int [ , ] dp = new int [ n + 1 , 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ 1 , j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ i , j ] = dp [ i - 1 , j + 1 ] ; else if ( j == 9 ) dp [ i , j ] = dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = dp [ i - 1 , j - 1 ] + dp [ i - 1 , j + 1 ] ; } } long sum = 0 ; for ( int j = 1 ; j <= 9 ; j ++ ) sum += dp [ n , j ] ; return sum ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( answer ( n ) ) ; } }
Print Longest Palindromic Subsequence | C # program to print longest palindromic subsequence ; Returns LCS X and Y ; Following steps build L [ m + 1 , n + 1 ] in bottom up fashion . Note that L [ i , j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Following code is used to print LCS ; Create a String length index + 1 and fill it with \ 0 ; Start from the right - most - bottom - most corner and one by one store characters in lcs [ ] ; If current character in X [ ] and Y are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and index ; If not same , then find the larger of two and go in the direction of larger value ; Returns longest palindromic subsequence of str ; Find reverse of str ; Return LCS of str and its reverse ; convert String to character array by using toCharArray ; Driver program to test above function
using System ; public class GFG { static String lcs ( String a , String b ) { int m = a . Length ; int n = b . Length ; char [ ] X = a . ToCharArray ( ) ; char [ ] Y = b . ToCharArray ( ) ; int [ , ] L = new int [ m + 1 , n + 1 ] ; int i , j ; for ( i = 0 ; i <= m ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i , j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; } else { L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } } int index = L [ m , n ] ; char [ ] lcs = new char [ index + 1 ] ; i = m ; j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { lcs [ index - 1 ] = X [ i - 1 ] ; i -- ; j -- ; index -- ; } else if ( L [ i - 1 , j ] > L [ i , j - 1 ] ) { i -- ; } else { j -- ; } } String ans = " " ; for ( int x = 0 ; x < lcs . Length ; x ++ ) { ans += lcs [ x ] ; } return ans ; } static String longestPalSubseq ( String str ) { String rev = str ; rev = reverse ( rev ) ; return lcs ( str , rev ) ; } static String reverse ( String str ) { String ans = " " ; char [ ] try1 = str . ToCharArray ( ) ; for ( int i = try1 . Length - 1 ; i >= 0 ; i -- ) { ans += try1 [ i ] ; } return ans ; } public static void Main ( ) { String str = " GEEKSFORGEEKS " ; Console . Write ( longestPalSubseq ( str ) ) ; } }
Count all subsequences having product less than K | C # program to find number of subarrays having product less than k . ; Function to count numbers of such subsequences having product less than k . ; number of subsequence using j - 1 terms ; if arr [ j - 1 ] > i it will surely make product greater thus it won 't contribute  then ; number of subsequence using 1 to j - 1 terms and j - th term ; Driver code
using System ; using System . Collections ; class CountSubsequences { public static int productSubSeqCount ( ArrayList arr , int k ) { int n = arr . Count ; int [ , ] dp = new int [ k + 1 , n + 1 ] ; for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { dp [ i , j ] = dp [ i , j - 1 ] ; if ( Convert . ToInt32 ( arr [ j - 1 ] ) <= i && Convert . ToInt32 ( arr [ j - 1 ] ) > 0 ) dp [ i , j ] += dp [ i / Convert . ToInt32 ( arr [ j - 1 ] ) , j - 1 ] + 1 ; } } return dp [ k , n ] ; } public static void Main ( ) { ArrayList A = new ArrayList ( ) ; A . Add ( 1 ) ; A . Add ( 2 ) ; A . Add ( 3 ) ; A . Add ( 4 ) ; int k = 10 ; Console . WriteLine ( productSubSeqCount ( A , k ) ) ; } }
Find the element that appears once in an array where every other element appears twice | C # program to find the array element that appears only once ; Return the maximum Sum of difference between consecutive elements . ; Do XOR of all elements and return ; Driver code
using System ; class GFG { static int findSingle ( int [ ] ar , int ar_size ) { int res = ar [ 0 ] ; for ( int i = 1 ; i < ar_size ; i ++ ) res = res ^ ar [ i ] ; return res ; } public static void Main ( ) { int [ ] ar = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = ar . Length ; Console . Write ( " Element ▁ occurring ▁ once ▁ is ▁ " + findSingle ( ar , n ) + " ▁ " ) ; } }
Count all triplets whose sum is equal to a perfect cube | C # Code for Count all triplets whose sum is equal to a perfect cube ; Function to calculate all occurrence of a number in a given range ; if i == 0 assign 1 to present state ; else add + 1 to current state with previous state ; Function to calculate triplets whose sum is equal to the perfect cube ; Initialize answer ; count all occurrence of third triplet in range from j + 1 to n ; Driver Code
using System ; class GFG { public static int [ , ] dp ; public static void computeDpArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 1 ; j <= 15000 ; ++ j ) { if ( i == 0 && j == arr [ i ] ) dp [ i , j ] = 1 ; else if ( i == 0 ) dp [ i , j ] = 0 ; else if ( arr [ i ] == j ) dp [ i , j ] = dp [ i - 1 , j ] + 1 ; else dp [ i , j ] = dp [ i - 1 , j ] ; } } } public static int countTripletSum ( int [ ] arr , int n ) { computeDpArray ( arr , n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { for ( int j = i + 1 ; j < n - 1 ; ++ j ) { for ( int k = 1 ; k <= 24 ; ++ k ) { int cube = k * k * k ; int rem = cube - ( arr [ i ] + arr [ j ] ) ; if ( rem > 0 ) ans += dp [ n - 1 , rem ] - dp [ j , rem ] ; } } } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 1 , 20 , 6 } ; int n = arr . Length ; dp = new int [ 1001 , 15001 ] ; Console . Write ( countTripletSum ( arr , n ) ) ; } }
Find the element that appears once in an array where every other element appears twice | C # program to find element that appears once ; function which find number ; applying the formula . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int singleNumber ( int [ ] nums , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; long sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . ContainsKey ( nums [ i ] ) ) { sum1 += nums [ i ] ; m . Add ( nums [ i ] , 1 ) ; } sum2 += nums [ i ] ; } return ( int ) ( 2 * ( sum1 ) - sum2 ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = 7 ; Console . WriteLine ( singleNumber ( a , n ) ) ; int [ ] b = { 15 , 18 , 16 , 18 , 16 , 15 , 89 } ; Console . WriteLine ( singleNumber ( b , n ) ) ; } }
Maximum Subarray Sum Excluding Certain Elements | C # Program to find max subarray sum excluding some elements ; Function to check the element present in array B ; Utility function for findMaxSubarraySum ( ) with the following parameters A = > Array A , B = > Array B , n = > Number of elements in Array A , m = > Number of elements in Array B ; set max_so_far to INT_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver Code ; Function call
using System ; class GFG { static bool isPresent ( int [ ] B , int m , int x ) { for ( int i = 0 ; i < m ; i ++ ) if ( B [ i ] == x ) return true ; return false ; } static int findMaxSubarraySumUtil ( int [ ] A , int [ ] B , int n , int m ) { int max_so_far = - 2147483648 , curr_max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPresent ( B , m , A [ i ] ) ) { curr_max = 0 ; continue ; } curr_max = Math . Max ( A [ i ] , curr_max + A [ i ] ) ; max_so_far = Math . Max ( max_so_far , curr_max ) ; } return max_so_far ; } static void findMaxSubarraySum ( int [ ] A , int [ ] B , int n , int m ) { int maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) ; if ( maxSubarraySum == - 2147483648 ) { Console . Write ( " Maximum ▁ Subarray ▁ Sum " + " ▁ " + " can ' t ▁ be ▁ found " ) ; } else { Console . Write ( " The ▁ Maximum ▁ Subarray ▁ Sum ▁ = ▁ " + maxSubarraySum ) ; } } static public void Main ( ) { int [ ] A = { 3 , 4 , 5 , - 4 , 6 } ; int [ ] B = { 1 , 8 , 5 } ; int n = A . Length ; int m = B . Length ; findMaxSubarraySum ( A , B , n , m ) ; } }