codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to choose elements from the array such that their average is K | C # implementation of the above approach ; This dp array is used to store our values so that we don 't have to calculate same  values again and again ; Base cases Index can 't be less than 0 ; No element is picked hence average cannot be calculated ; If remainder is non zero , we cannot divide the sum by count i . e . the average will not be an integer ; If we find an average return 1 ; If we have already calculated this function simply return it instead of calculating it again ; If we don 't pick the current element  simple recur for index -1 ; If we pick the current element add it to our current sum and increment count by 1 ; Store the value for the current function ; Function to return the number of ways ; Push - 1 at the beginning to make it 1 - based indexing ; Initialize dp array by - 1 ; Call recursive function waysutil to calculate total ways ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX_INDEX = 51 ; static int MAX_SUM = 2505 ; static int [ , , ] dp = new int [ MAX_INDEX , MAX_SUM , MAX_INDEX ] ; static int waysutil ( int index , int sum , int count , List < int > arr , int K ) { if ( index < 0 ) { return 0 ; } if ( index == 0 ) { if ( count == 0 ) { return 0 ; } int remainder = sum % count ; if ( remainder != 0 ) { return 0 ; } int average = sum / count ; if ( average == K ) { return 1 ; } } if ( dp [ index , sum , count ] != - 1 ) { return dp [ index , sum , count ] ; } int dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; int pick = waysutil ( index - 1 , sum + arr [ index ] , count + 1 , arr , K ) ; int total = pick + dontpick ; dp [ index , sum , count ] = total ; return total ; } static int ways ( int N , int K , int [ ] arr ) { List < int > Arr = new List < int > ( ) ; Arr . Add ( - 1 ) ; for ( int i = 0 ; i < N ; ++ i ) { Arr . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < MAX_INDEX ; i ++ ) { for ( int j = 0 ; j < MAX_SUM ; j ++ ) { for ( int l = 0 ; l < MAX_INDEX ; l ++ ) { dp [ i , j , l ] = - 1 ; } } } int answer = waysutil ( N , 0 , 0 , Arr , K ) ; return answer ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . Length ; int K = 5 ; Console . WriteLine ( ways ( N , K , arr ) ) ; } }
Subset with sum closest to zero | C # Program for above approach ; Variable to store states of dp ; Function to return the number closer to integer s ; To find the sum closest to zero Since sum can be negative , we will add MAX to it to make it positive ; Base cases ; Checks if a state is already solved ; Recurrence relation ; Returning the value ; Function to calculate the closest sum value ; Calculate the Closest value for every subarray arr [ i - 1 : n ] ; Driver Code ; Input array
using System ; class GFG { static int arrSize = 51 ; static int maxSum = 201 ; static int MAX = 100 ; static int inf = 999999 ; static int [ , ] dp = new int [ arrSize , maxSum ] ; static int [ , ] visit = new int [ arrSize , maxSum ] ; static int RetClose ( int a , int b , int s ) { if ( Math . Abs ( a - s ) < Math . Abs ( b - s ) ) return a ; else return b ; } static int MinDiff ( int i , int sum , int [ ] arr , int n ) { if ( i == n ) return 0 ; if ( visit [ i , sum + MAX ] > 0 ) return dp [ i , sum + MAX ] ; visit [ i , sum + MAX ] = 1 ; dp [ i , sum + MAX ] = RetClose ( arr [ i ] + MinDiff ( i + 1 , sum + arr [ i ] , arr , n ) , MinDiff ( i + 1 , sum , arr , n ) , - 1 * sum ) ; return dp [ i , sum + MAX ] ; } static void FindClose ( int [ ] arr , int n ) { int ans = inf ; for ( int i = 1 ; i <= n ; i ++ ) ans = RetClose ( arr [ i - 1 ] + MinDiff ( i , arr [ i - 1 ] , arr , n ) , ans , 0 ) ; Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 25 , - 9 , - 10 , - 4 , - 7 , - 33 } ; int n = arr . Length ; FindClose ( arr , n ) ; } }
Paths from entry to exit in matrix and maximum path sum | C # implementation of the approach ; Recursive function to return the total paths from grid [ i ] [ j ] to grid [ n - 1 ] [ n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Recursive function to return the maximum sum along the path from grid [ i , j ] to grid [ n - 1 , n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Driver code ; Fill the dp [ , ] array with - 1 ; When source and destination are same then there is only 1 path ; Print the count of paths from grid [ 0 , 0 ] to grid [ n - 1 , n - 1 ] ; Fill the dp [ , ] array again with - 1 ; When source and destination are same then the sum is grid [ n - 1 , n - 1 ] ; Print the maximum sum among all the paths from grid [ 0 , 0 ] to grid [ n - 1 , n - 1 ]
using System ; class GFG { static int totalPaths ( int i , int j , int n , int [ , ] grid , int [ , ] dp ) { if ( i < 0 j < 0 i >= n j >= n ) return 0 ; if ( dp [ i , j ] == - 1 ) { if ( grid [ i , j ] == 1 ) dp [ i , j ] = totalPaths ( i , j + 1 , n , grid , dp ) ; else if ( grid [ i , j ] == 2 ) dp [ i , j ] = totalPaths ( i + 1 , j , n , grid , dp ) ; else dp [ i , j ] = totalPaths ( i , j + 1 , n , grid , dp ) + totalPaths ( i + 1 , j , n , grid , dp ) ; } return dp [ i , j ] ; } static int maxSumPath ( int i , int j , int n , int [ , ] grid , int [ , ] dp ) { if ( i < 0 j < 0 i >= n j >= n ) return 0 ; if ( dp [ i , j ] == - 1 ) { if ( grid [ i , j ] == 1 ) dp [ i , j ] = grid [ i , j ] + maxSumPath ( i , j + 1 , n , grid , dp ) ; else if ( grid [ i , j ] == 2 ) dp [ i , j ] = grid [ i , j ] + maxSumPath ( i + 1 , j , n , grid , dp ) ; else dp [ i , j ] = grid [ i , j ] + Math . Max ( maxSumPath ( i , j + 1 , n , grid , dp ) , maxSumPath ( i + 1 , j , n , grid , dp ) ) ; } return dp [ i , j ] ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 1 , 3 , 2 , 1 } , { 3 , 2 , 2 , 1 , 2 } , { 1 , 3 , 3 , 1 , 3 } , { 1 , 2 , 3 , 1 , 2 } , { 1 , 1 , 1 , 3 , 1 } } ; int n = grid . GetLength ( 0 ) ; int [ , ] dp = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i , j ] = - 1 ; dp [ n - 1 , n - 1 ] = 1 ; Console . WriteLine ( " Total ▁ paths : ▁ " + totalPaths ( 0 , 0 , n , grid , dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i , j ] = - 1 ; dp [ n - 1 , n - 1 ] = grid [ n - 1 , n - 1 ] ; Console . WriteLine ( " Maximum ▁ sum : ▁ " + maxSumPath ( 0 , 0 , n , grid , dp ) ) ; } }
Queries for bitwise OR in the index range [ L , R ] of the given array | C # implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
using System ; class GFG { static int MAX = 100000 ; static int bitscount = 32 ; static int [ , ] prefix_count = new int [ bitscount , MAX ] ; static void findPrefixCount ( int [ ] arr , int n ) { for ( int i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i , 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( int j = 1 ; j < n ; j ++ ) { prefix_count [ i , j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefix_count [ i , j ] += prefix_count [ i , j - 1 ] ; } } } static int rangeOr ( int l , int r ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int x ; if ( l == 0 ) x = prefix_count [ i , r ] ; else x = prefix_count [ i , r ] - prefix_count [ i , l - 1 ] ; if ( x != 0 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 5 , 3 , 5 , 2 , 3 } ; int n = arr . Length ; findPrefixCount ( arr , n ) ; int [ , ] queries = { { 1 , 3 } , { 4 , 5 } } ; int q = queries . GetLength ( 0 ) ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( rangeOr ( queries [ i , 0 ] , queries [ i , 1 ] ) ) ; } }
Distinct palindromic sub | C # implementation of the approach ; Function to return the count of distinct palindromic sub - strings of the given string s ; To store the positions of palindromic sub - strings ; Map to store the sub - strings ; Sub - strings of length 1 are palindromes ; Store continuous palindromic sub - strings ; Store palindromes of size 2 ; If str [ i ... ( i + 1 ) ] is not a palindromic then set dp [ i , i + 1 ] = 0 ; Find palindromic sub - strings of length >= 3 ; End of palindromic substring ; If s [ start ] = = s [ end ] and dp [ start + 1 , end - 1 ] is already palindrome then s [ start ... . end ] is also a palindrome ; Set dp [ start , end ] = 1 ; Not a palindrome ; Return the count of distinct palindromes ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int palindromeSubStrs ( String s ) { int [ , ] dp = new int [ s . Length , s . Length ] ; int st , end , i , len ; Dictionary < String , Boolean > m = new Dictionary < String , Boolean > ( ) ; for ( i = 0 ; i < s . Length ; i ++ ) { dp [ i , i ] = 1 ; if ( ! m . ContainsKey ( s . Substring ( i , 1 ) ) ) m . Add ( s . Substring ( i , 1 ) , true ) ; } for ( i = 0 ; i < s . Length - 1 ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { dp [ i , i + 1 ] = 1 ; if ( ! m . ContainsKey ( s . Substring ( i , 2 ) ) ) m . Add ( s . Substring ( i , 2 ) , true ) ; } else dp [ i , i + 1 ] = 0 ; } for ( len = 3 ; len <= s . Length ; len ++ ) { for ( st = 0 ; st <= s . Length - len ; st ++ ) { end = st + len - 1 ; if ( s [ st ] == s [ end ] && dp [ st + 1 , end - 1 ] == 1 ) { dp [ st , end ] = 1 ; m . Add ( s . Substring ( st , end + 1 - st ) , true ) ; } else dp [ st , end ] = 0 ; } } return m . Count ; } public static void Main ( String [ ] args ) { String s = " abaaa " ; Console . WriteLine ( palindromeSubStrs ( s ) ) ; } }
Maximum sum path in a matrix from top to bottom and back | C # implementation of the approach ; Input matrix ; DP matrix ; Function to return the sum of the cells arr [ i1 ] [ j1 ] and arr [ i2 ] [ j2 ] ; Recursive function to return the required maximum cost path ; Column number of second path ; Base Case ; If already calculated , return from DP matrix ; Recurring for neighbouring cells of both paths together ; Saving result to the DP matrix for current state ; Driver code ; set initial value
using System ; class GFG { static int n = 4 , m = 4 ; static int [ , ] arr = { { 1 , 0 , 3 , - 1 } , { 3 , 5 , 1 , - 2 } , { - 2 , 0 , 1 , 1 } , { 2 , 1 , - 1 , 1 } } ; static int [ , , ] cache = new int [ 5 , 5 , 5 ] ; static int sum ( int i1 , int j1 , int i2 , int j2 ) { if ( i1 == i2 && j1 == j2 ) { return arr [ i1 , j1 ] ; } return arr [ i1 , j1 ] + arr [ i2 , j2 ] ; } static int maxSumPath ( int i1 , int j1 , int i2 ) { int j2 = i1 + j1 - i2 ; if ( i1 >= n i2 >= n j1 >= m j2 >= m ) { return 0 ; } if ( cache [ i1 , j1 , i2 ] != - 1 ) { return cache [ i1 , j1 , i2 ] ; } int ans = int . MinValue ; ans = Math . Max ( ans , maxSumPath ( i1 + 1 , j1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . Max ( ans , maxSumPath ( i1 , j1 + 1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . Max ( ans , maxSumPath ( i1 , j1 + 1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . Max ( ans , maxSumPath ( i1 + 1 , j1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; cache [ i1 , j1 , i2 ] = ans ; return ans ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < 5 ; i ++ ) for ( int i1 = 0 ; i1 < 5 ; i1 ++ ) for ( int i2 = 0 ; i2 < 5 ; i2 ++ ) cache [ i , i1 , i2 ] = - 1 ; Console . WriteLine ( maxSumPath ( 0 , 0 , 0 ) ) ; } }
Bellman Ford Algorithm ( Simple Implementation ) | C # program for Bellman - Ford 's single source shortest path algorithm. ; The main function that finds shortest distances from src to all other vertices using Bellman - Ford algorithm . The function also detects negative weight cycle The row graph [ i ] represents i - th edge with three values u , v and w . ; Initialize distance of all vertices as infinite . ; initialize distance of source as 0 ; Relax all edges | V | - 1 times . A simple shortest path from src to any other vertex can have at - most | V | - 1 edges ; check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle. ; Driver code ; Every edge has three values ( u , v , w ) where the edge is from vertex u to v . And weight of the edge is w .
using System ; class GFG { static void BellmanFord ( int [ , ] graph , int V , int E , int src ) { int [ ] dis = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) dis [ i ] = int . MaxValue ; dis [ src ] = 0 ; for ( int i = 0 ; i < V - 1 ; i ++ ) { for ( int j = 0 ; j < E ; j ++ ) { if ( dis [ graph [ j , 0 ] ] = int . MaxValue && dis [ graph [ j , 0 ] ] + graph [ j , 2 ] < dis [ graph [ j , 1 ] ] ) dis [ graph [ j , 1 ] ] = dis [ graph [ j , 0 ] ] + graph [ j , 2 ] ; } } for ( int i = 0 ; i < E ; i ++ ) { int x = graph [ i , 0 ] ; int y = graph [ i , 1 ] ; int weight = graph [ i , 2 ] ; if ( dis [ x ] != int . MaxValue && dis [ x ] + weight < dis [ y ] ) Console . WriteLine ( " Graph ▁ contains ▁ negative " + " ▁ weight ▁ cycle " ) ; } Console . WriteLine ( " Vertex ▁ Distance ▁ from ▁ Source " ) ; for ( int i = 0 ; i < V ; i ++ ) Console . WriteLine ( i + " TABSYMBOL TABSYMBOL " + dis [ i ] ) ; } public static void Main ( String [ ] args ) { int [ , ] graph = { { 0 , 1 , - 1 } , { 0 , 2 , 4 } , { 1 , 2 , 3 } , { 1 , 3 , 2 } , { 1 , 4 , 2 } , { 3 , 2 , 5 } , { 3 , 1 , 1 } , { 4 , 3 , - 3 } } ; BellmanFord ( graph , V , E , 0 ) ; } }
Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal | C # implementation of the approach ; Function to perform simple DFS ; Finding the number of times each bit is set in all the values of a subtree rooted at v ; Checking for each bit whether the numbers with that particular bit as set are either zero in both the resulting trees or greater than zero in both the resulting trees ; Driver code ; Number of nodes ; ArrayList to store the tree ; Array to store the value of nodes ; Array to store the number of times each bit is set in all the values of a subtree ; Array to store the number of times each bit is set in all the values in complete tree ; Finding the set bits in the value of node i ; Add edges
using System ; using System . Collections . Generic ; class GFG { static int [ ] m ; static int [ , ] a ; static int [ ] x ; static List < int > [ ] g ; static int ans = 0 ; static void dfs ( int u , int p ) { foreach ( int v in g [ u ] ) { if ( v != p ) { dfs ( v , u ) ; for ( int i = 0 ; i < 22 ; i ++ ) a [ u , i ] += a [ v , i ] ; } } int pp = 0 ; for ( int i = 0 ; i < 22 ; i ++ ) { if ( ! ( ( a [ u , i ] > 0 && x [ i ] - a [ u , i ] > 0 ) || ( a [ u , i ] == 0 && x [ i ] == 0 ) ) ) { pp = 1 ; break ; } } if ( pp == 0 ) ans ++ ; } public static void Main ( String [ ] args ) { int n = 4 ; g = new List < int > [ n + 1 ] ; m = new int [ n + 1 ] ; m [ 1 ] = 1 ; m [ 2 ] = 3 ; m [ 3 ] = 2 ; m [ 4 ] = 3 ; a = new int [ n + 1 , 22 ] ; x = new int [ 22 ] ; for ( int i = 1 ; i <= n ; i ++ ) { g [ i ] = new List < int > ( ) ; int y = m [ i ] ; int k = 0 ; while ( y != 0 ) { int p = y % 2 ; if ( p == 1 ) { x [ k ] ++ ; a [ i , k ] ++ ; } y = y / 2 ; k ++ ; } } g [ 1 ] . Add ( 2 ) ; g [ 2 ] . Add ( 1 ) ; g [ 1 ] . Add ( 3 ) ; g [ 3 ] . Add ( 1 ) ; g [ 1 ] . Add ( 4 ) ; g [ 4 ] . Add ( 1 ) ; dfs ( 1 , 0 ) ; Console . WriteLine ( ans ) ; } }
Find the maximum number of composite summands of a number | C # implementation of the above approach ; Function to generate the dp array ; combination of three integers ; take the maximum number of summands ; Function to find the maximum number of summands ; If n is a smaller number , less than 16 return dp [ n ] ; Else , find a minimal number t as explained in solution ; Driver code ; Generate dp array
using System ; using System . Collections ; class GFG { static int maxn = 16 ; static int [ ] dp = new int [ maxn + 1 ] ; static void precompute ( ) { for ( int i = 0 ; i <= maxn ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; int [ ] vec = { 4 , 6 , 9 } ; for ( int i = 1 ; i < maxn ; ++ i ) { foreach ( int j in vec ) { if ( i >= j && dp [ i - j ] != - 1 ) { dp [ i ] = Math . Max ( dp [ i ] , dp [ i - j ] + 1 ) ; } } } } static int Maximum_Summands ( int n ) { if ( n < maxn ) return dp [ n ] ; else { int t = ( n - maxn ) / 4 + 1 ; return t + dp [ n - 4 * t ] ; } } static void Main ( ) { int n = 12 ; precompute ( ) ; Console . WriteLine ( Maximum_Summands ( n ) ) ; } }
Minimum cost to reach end of array array when a maximum jump of K index is allowed | C # implementation of the approach ; Function for returning the min of two elements ; for calculating the number of elements ; Allocating Memo table and initializing with INT_MAX ; Base case ; For every element relax every reachable element ie relax next k elements ; reaching next k element ; Relaxing the element ; return the last element in the array ; Driver Code
using System ; class GFG { static int min ( int a , int b ) { return ( a > b ) ? b : a ; } static int minCostJumpsDP ( int [ ] A , int k ) { int size = A . Length ; int [ ] x = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) x [ i ] = int . MaxValue ; x [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = i + 1 ; j < i + k + 1 && j < size ; j ++ ) { x [ j ] = min ( x [ j ] , x [ i ] + Math . Abs ( A [ i ] - A [ j ] ) ) ; } } return x [ size - 1 ] ; } public static void Main ( String [ ] args ) { int [ ] input = { 83 , 26 , 37 , 35 , 33 , 35 , 56 } ; Console . WriteLine ( minCostJumpsDP ( input , 3 ) ) ; } }
DP on Trees | Set | C # program to find diameter of a tree using DFS . ; Function to find the diameter of the tree using Dynamic Programming ; Store the first maximum and secondmax ; Traverse for all children of node ; Call DFS function again ; Find first max ; Secondmaximum ; Find secondmaximum ; Base case for every node ; if ( firstmax != - 1 ) Add ; Find dp [ 2 ] ; Return maximum of both ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Find diameter by calling function
using System ; using System . Collections . Generic ; class GFG { static int dfs ( int node , int parent , int [ ] dp1 , int [ ] dp2 , List < List < int > > adj ) { int firstmax = - 1 ; int secondmax = - 1 ; for ( int i = 0 ; i < adj [ node ] . Count ; ++ i ) { if ( adj [ node ] [ i ] == parent ) continue ; dfs ( adj [ node ] [ i ] , node , dp1 , dp2 , adj ) ; if ( firstmax == - 1 ) { firstmax = dp1 [ adj [ node ] [ i ] ] ; } else if ( dp1 [ adj [ node ] [ i ] ] >= firstmax ) { secondmax = firstmax ; firstmax = dp1 [ adj [ node ] [ i ] ] ; } else if ( dp1 [ adj [ node ] [ i ] ] > secondmax ) { secondmax = dp1 [ adj [ node ] [ i ] ] ; } } dp1 [ node ] = 1 ; dp1 [ node ] += firstmax ; if ( secondmax != - 1 ) dp2 [ node ] = 1 + firstmax + secondmax ; return Math . Max ( dp1 [ node ] , dp2 [ node ] ) ; } static void Main ( ) { int n = 5 ; List < List < int > > adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } adj [ 1 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 1 ) ; adj [ 1 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 1 ) ; adj [ 2 ] . Add ( 4 ) ; adj [ 4 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 5 ) ; adj [ 5 ] . Add ( 2 ) ; int [ ] dp1 = new int [ n + 1 ] ; int [ ] dp2 = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dp1 [ i ] = 0 ; dp2 [ i ] = 0 ; } Console . WriteLine ( " Diameter ▁ of ▁ the ▁ given ▁ tree ▁ is ▁ " + dfs ( 1 , 1 , dp1 , dp2 , adj ) ) ; } }
Find sub | C # implementation of the approach ; Function to return the sum of the sub - matrix ; Function that returns true if it is possible to find the sub - matrix with required sum ; 2 - D array to store the sum of all the sub - matrices ; Filling of dp [ , ] array ; Checking for each possible sub - matrix of size k X k ; Sub - matrix with the given sum not found ; Driver code ; Function call
using System ; class GfG { static int N = 4 ; static int getSum ( int r1 , int r2 , int c1 , int c2 , int [ , ] dp ) { return dp [ r2 , c2 ] - dp [ r2 , c1 ] - dp [ r1 , c2 ] + dp [ r1 , c1 ] ; } static bool sumFound ( int K , int S , int [ , ] grid ) { int [ , ] dp = new int [ N + 1 , N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i + 1 , j + 1 ] = dp [ i + 1 , j ] + dp [ i , j + 1 ] - dp [ i , j ] + grid [ i , j ] ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int sum = getSum ( i , i + K , j , j + K , dp ) ; if ( sum == S ) { return true ; } } } return false ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int K = 2 ; int S = 14 ; if ( sumFound ( K , S , grid ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Stepping Numbers | A C # program to find all the Stepping Number in range [ n , m ] ; Prints all stepping numbers reachable from num and in range [ n , m ] ; Queue will contain all the stepping Numbers ; Get the front element and pop from the queue ; If the Stepping Number is in the range [ n , m ] then display ; If Stepping Number is 0 or greater then m , no need to explore the neighbors ; Get the last digit of the currently visited Stepping Number ; There can be 2 cases either digit to be appended is lastDigit + 1 or lastDigit - 1 ; If lastDigit is 0 then only possible digit after 0 can be 1 for a Stepping Number ; If lastDigit is 9 then only possible digit after 9 can be 8 for a Stepping Number ; Prints all stepping numbers in range [ n , m ] using BFS . ; For every single digit Number ' i ' find all the Stepping Numbers starting with i ; Driver code ; Display Stepping Numbers in the range [ n , m ]
using System ; using System . Collections . Generic ; public class GFG { static void bfs ( int n , int m , int num ) { Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( num ) ; while ( q . Count != 0 ) { int stepNum = q . Dequeue ( ) ; if ( stepNum <= m && stepNum >= n ) { Console . Write ( stepNum + " ▁ " ) ; } if ( stepNum == 0 stepNum > m ) continue ; int lastDigit = stepNum % 10 ; int stepNumA = stepNum * 10 + ( lastDigit - 1 ) ; int stepNumB = stepNum * 10 + ( lastDigit + 1 ) ; if ( lastDigit == 0 ) q . Enqueue ( stepNumB ) ; else if ( lastDigit == 9 ) q . Enqueue ( stepNumA ) ; else { q . Enqueue ( stepNumA ) ; q . Enqueue ( stepNumB ) ; } } } static void displaySteppingNumbers ( int n , int m ) { for ( int i = 0 ; i <= 9 ; i ++ ) bfs ( n , m , i ) ; } static public void Main ( ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; } }