codeparrot/xlcost-text-to-code huggingface dataset:
Check if it ’ s possible to completely fill every container with same ball | C ++ program for above approach ; A boolean function that returns true if it 's possible to completely fill the container else return false ; Base Case ; Backtracking ; Function to check the conditions ; Storing frequencies ; Function Call for backtracking ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool getans ( int i , vector < int > v , vector < int > q ) { if ( i == q . size ( ) ) return true ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( v [ j ] >= q [ i ] ) { v [ j ] -= q [ i ] ; if ( getans ( i + 1 , v , q ) ) { return true ; } v [ j ] += q [ i ] ; } } return false ; } void Check ( vector < int > c , vector < int > b ) { map < int , int > m ; for ( int i = 0 ; i < b . size ( ) ; i ++ ) { m [ b [ i ] ] ++ ; } vector < int > v ; for ( auto i : m ) { v . push_back ( i . second ) ; } bool check = getans ( 0 , v , c ) ; if ( check ) cout << " YES " << endl ; else cout << " NO " << endl ; } int main ( ) { vector < int > c = { 1 , 3 , 3 } ; vector < int > b = { 2 , 2 , 2 , 2 , 4 , 4 , 4 } ; Check ( c , b ) ; return 0 ; }
Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges | C ++ program for the above approach ; Function to find the maximum Bitwise XOR of any subset of the array of size K ; Number of node must K + 1 for K edges ; Stores the maximum Bitwise XOR ; Generate all subsets of the array ; __builtin_popcount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update the maximum Bitwise XOR obtained so far ; Return the maximum XOR ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumXOR ( int arr [ ] , int n , int K ) { K ++ ; int maxXor = INT_MIN ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( __builtin_popcount ( i ) == K ) { int cur_xor = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = max ( maxXor , cur_xor ) ; } } return maxXor ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( int ) ; int M = 2 ; cout << maximumXOR ( arr , N , M ) ; return 0 ; }
Tower of Hanoi | Set 2 | C ++ program for the above approach ; Function to print order of movement of N disks across three rods to place all disks on the third rod from the first - rod using binary representation ; Iterate over the range [ 0 , 2 ^ N - 1 ] ; Print the movement of the current rod ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void TowerOfHanoi ( int N ) { for ( int x = 1 ; x <= pow ( 2 , N ) - 1 ; x ++ ) { cout << " Move ▁ from ▁ Rod ▁ " << ( ( x & x - 1 ) % 3 + 1 ) << " ▁ to ▁ Rod ▁ " << ( ( ( x x - 1 ) + 1 ) % 3 + 1 ) << endl ; } } int main ( ) { int N = 3 ; TowerOfHanoi ( N ) ; return 0 ; }
Check if a path exists from a given cell to any boundary element of the Matrix with sum of elements not exceeding K | C ++ program for the above approach ; Function to check if it is valid to move to the given index or not ; Otherwise return false ; Function to check if there exists a path from the cell ( X , Y ) of the matrix to any boundary cell with sum of elements at most K or not ; Base Case ; If K >= board [ X ] [ Y ] ; Stores the current element ; Mark the current cell as visited ; Visit all the adjacent cells ; Mark the cell unvisited ; Return false ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( vector < vector < int > > & board , int i , int j , int K ) { if ( board [ i ] [ j ] <= K ) { return true ; } return false ; } bool findPath ( vector < vector < int > > & board , int X , int Y , int M , int N , int K ) { if ( X < 0 X == M Y < 0 Y == N ) { return true ; } if ( isValid ( board , X , Y , K ) ) { int board_XY = board [ X ] [ Y ] ; board [ X ] [ Y ] = INT_MAX ; if ( findPath ( board , X + 1 , Y , M , N , K - board_XY ) || findPath ( board , X - 1 , Y , M , N , K - board_XY ) || findPath ( board , X , Y + 1 , M , N , K - board_XY ) || findPath ( board , X , Y - 1 , M , N , K - board_XY ) ) { return true ; } board [ X ] [ Y ] = board_XY ; } return false ; } int main ( ) { vector < vector < int > > grid = { { 25 , 5 , 25 , 25 , 25 , 25 } , { 25 , 1 , 1 , 5 , 12 , 25 } , { 25 , 1 , 12 , 0 , 15 , 25 } , { 22 , 1 , 11 , 2 , 19 , 15 } , { 25 , 2 , 2 , 1 , 12 , 15 } , { 25 , 9 , 10 , 1 , 11 , 25 } , { 25 , 25 , 25 , 25 , 25 , 25 } } ; int K = 17 ; int M = grid . size ( ) ; int N = grid [ 0 ] . size ( ) ; int X = 2 , Y = 3 ; if ( findPath ( grid , X , Y , M , N , K ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Sum of all subsets whose sum is a Perfect Number from a given array | C ++ program for the above approach ; Function to check is a given number is a perfect number or not ; Stores sum of divisors ; Add all divisors of x to sum_div ; If the sum of divisors is equal to the given number , return true ; Otherwise , return false ; Function to find the sum of all the subsets from an array whose sum is a perfect number ; Stores the total number of subsets , i . e . 2 ^ n ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider array elements from positions of set bits in the binary representation of n ; If sum of chosen elements is a perfect number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPerfect ( int x ) { int sum_div = 1 ; for ( int i = 2 ; i <= x / 2 ; ++ i ) { if ( x % i == 0 ) { sum_div += i ; } } if ( sum_div == x ) { return 1 ; } else return 0 ; } void subsetSum ( int arr [ ] , int n ) { long long total = 1 << n ; for ( long long i = 0 ; i < total ; i ++ ) { long long sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( i & ( 1 << j ) ) sum += arr [ j ] ; if ( isPerfect ( sum ) ) { cout << sum << " ▁ " ; } } } int main ( ) { int arr [ ] = { 5 , 4 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; subsetSum ( arr , N ) ; return 0 ; }
Pen Distribution Problem | C ++ implementation of the above approach ; Recursive function to play Game ; Box is empty , Game Over ! or Both have quit , Game Over ! ; P1 moves ; P2 moves ; Increment X ; Switch moves between P1 and P2 ; Function to find the number of pens remaining in the box and calculate score for each player ; Score of P1 ; Score of P2 ; Initialized to zero ; Move = 0 , P1 ' s ▁ turn ▁ ▁ Move ▁ = ▁ 1 , ▁ P2' s turn ; Has P1 quit ; Has P2 quit ; Recursively continue the game ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int & N , int & P1 , int & P2 , int & X , bool Move , bool QuitP1 , bool QuitP2 ) { if ( N == 0 or ( QuitP1 and QuitP2 ) ) { cout << " Number ▁ of ▁ pens ▁ remaining " << " ▁ in ▁ the ▁ box : ▁ " << N << endl ; cout << " Number ▁ of ▁ pens ▁ collected " << " ▁ by ▁ P1 : ▁ " << P1 << endl ; cout << " Number ▁ of ▁ pens ▁ collected " << " ▁ by ▁ P2 : ▁ " << P2 << endl ; return ; } if ( Move == 0 and QuitP1 == false ) { int req_P1 = pow ( 2 , X ) ; if ( req_P1 <= N ) { P1 += req_P1 ; N -= req_P1 ; } else { QuitP1 = true ; } } else if ( Move == 1 and QuitP2 == false ) { int req_P2 = pow ( 3 , X ) ; if ( req_P2 <= N ) { P2 += req_P2 ; N -= req_P2 ; } else { QuitP2 = true ; } } X ++ ; Move = ( ( Move == 1 ) ? 0 : 1 ) ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } void PenGame ( int N ) { int P1 = 0 ; int P2 = 0 ; int X = 0 ; bool Move = 0 ; bool QuitP1 = 0 ; bool QuitP2 = 0 ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } int main ( ) { int N = 22 ; PenGame ( N ) ; return 0 ; }
Print all possible K | C ++ program to implement the above approach ; Function to print all subsequences of length K from N natural numbers whose sum equal to N ; Base case ; Iterate over the range [ 1 , N ] ; Insert i into temp ; Pop i from temp ; Utility function to print all subsequences of length K with sum equal to N ; Store all subsequences of length K from N natural numbers ; Store current subsequence of length K from N natural numbers ; Stores total count of subsequences ; Print all subsequences ; Treaverse all subsequences ; Print current subsequence ; If current element is last element of subsequence ; If current subsequence is last subsequence from n natural numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSub ( vector < vector < int > > & res , int sum , int K , int N , vector < int > & temp ) { if ( K == 0 && sum == 0 ) { res . push_back ( temp ) ; return ; } if ( sum <= 0 K <= 0 ) { return ; } for ( int i = 1 ; i <= N ; i ++ ) { temp . push_back ( i ) ; findSub ( res , sum - i , K - 1 , N , temp ) ; temp . pop_back ( ) ; } } void UtilPrintSubsequncesOfKSumN ( int N , int K ) { vector < vector < int > > res ; vector < int > temp ; findSub ( res , N , K , N , temp ) ; int sz = res . size ( ) ; cout << " { ▁ " ; for ( int i = 0 ; i < sz ; i ++ ) { cout << " { ▁ " ; for ( int j = 0 ; j < K ; j ++ ) { if ( j == K - 1 ) cout << res [ i ] [ j ] << " ▁ " ; else cout << res [ i ] [ j ] << " , ▁ " ; } if ( i == sz - 1 ) cout << " } " ; else cout << " } , ▁ " ; } cout << " ▁ } " ; } int main ( ) { int N = 4 ; int K = 2 ; UtilPrintSubsequncesOfKSumN ( N , K ) ; }
Generate all N digit numbers having absolute difference as K between adjacent digits | C ++ program for the above approach ; Function that recursively finds the possible numbers and append into ans ; Base Case ; Check the sum of last digit and k less than or equal to 9 or not ; If k == 0 , then subtraction and addition does not make any difference Hence , subtraction is skipped ; Function to call checkUntil function for every integer from 1 to 9 ; check_util function recursively store all numbers starting from i ; Function to print the all numbers which satisfy the conditions ; Driver Code ; Given N and K ; To store the result ; Function Call ; Print Resultant Numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkUntil ( int num , int K , int N , vector < int > & ans ) { if ( N == 1 ) { ans . push_back ( num ) ; return ; } if ( ( num % 10 + K ) <= 9 ) checkUntil ( 10 * num + ( num % 10 + K ) , K , N - 1 , ans ) ; if ( K ) { if ( ( num % 10 - K ) >= 0 ) checkUntil ( 10 * num + num % 10 - K , K , N - 1 , ans ) ; } } void check ( int K , int N , vector < int > & ans ) { for ( int i = 1 ; i <= 9 ; i ++ ) { checkUntil ( i , K , N , ans ) ; } } void print ( vector < int > & ans ) { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] << " , ▁ " ; } } int main ( ) { int N = 4 , K = 8 ; vector < int > ans ; check ( K , N , ans ) ; print ( ans ) ; return 0 ; }
Minimum Cost Path in a directed graph via given set of intermediate nodes | C ++ Program to implement the above approach ; Stores minimum - cost of path from source ; Function to Perform BFS on graph g starting from vertex v ; If destination is reached ; Set flag to true ; Visit all the intermediate nodes ; If any intermediate node is not visited ; If all intermediate nodes are visited ; Update the minSum ; Mark the current node visited ; Traverse adjacent nodes ; Mark the neighbour visited ; Find minimum cost path considering the neighbour as the source ; Mark the neighbour unvisited ; Mark the source unvisited ; Driver Code ; Stores the graph ; Number of nodes ; Source ; Destination ; Keeps a check on visited and unvisited nodes ; Stores intemediate nodes ; If no path is found
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSum = INT_MAX ; void getMinPathSum ( unordered_map < int , vector < pair < int , int > > > & graph , vector < bool > & visited , vector < int > necessary , int src , int dest , int currSum ) { if ( src == dest ) { bool flag = true ; for ( int i : necessary ) { if ( ! visited [ i ] ) { flag = false ; break ; } } if ( flag ) minSum = min ( minSum , currSum ) ; return ; } else { visited [ src ] = true ; for ( auto node : graph [ src ] ) { if ( ! visited [ node . first ] ) { visited [ node . first ] = true ; getMinPathSum ( graph , visited , necessary , node . first , dest , currSum + node . second ) ; visited [ node . first ] = false ; } } visited [ src ] = false ; } } int main ( ) { unordered_map < int , vector < pair < int , int > > > graph ; graph [ 0 ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 2 } } ; graph [ 1 ] = { { 4 , 4 } , { 0 , 1 } } ; graph [ 2 ] = { { 4 , 5 } , { 5 , 6 } } ; graph [ 3 ] = { { 5 , 7 } , { 0 , 1 } } ; graph [ 4 ] = { { 6 , 4 } } ; graph [ 5 ] = { { 6 , 2 } } ; graph [ 6 ] = { { 7 , 11 } } ; int n = 7 ; int source = 0 ; int dest = 6 ; vector < bool > visited ( n , false ) ; vector < int > necessary { 2 , 4 } ; getMinPathSum ( graph , visited , necessary , source , dest , 0 ) ; if ( minSum == INT_MAX ) cout << " - 1 STRNEWLINE " ; else cout << minSum << ' ' ; return 0 ; }
Unique subsequences of length K with given sum | C ++ program for the above approach ; Function to find all the subsequences of a given length and having sum S ; Termination condition ; Add value to sum ; Check if the resultant sum equals to target sum ; If true ; Print resultant array ; End this recursion stack ; Check all the combinations using backtracking ; Check all the combinations using backtracking ; Driver Code ; Given array ; To store the subsequence ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void comb ( int * arr , int len , int r , int ipos , int * op , int opos , int sum ) { if ( opos == r ) { int sum2 = 0 ; for ( int i = 0 ; i < opos ; i ++ ) { sum2 = sum2 + op [ i ] ; } if ( sum == sum2 ) { for ( int i = 0 ; i < opos ; i ++ ) cout << op [ i ] << " , ▁ " ; cout << endl ; } return ; } if ( ipos < len ) { comb ( arr , len , r , ipos + 1 , op , opos , sum ) ; op [ opos ] = arr [ ipos ] ; comb ( arr , len , r , ipos + 1 , op , opos + 1 , sum ) ; } } int main ( ) { int arr [ ] = { 4 , 6 , 8 , 2 , 12 } ; int K = 3 ; int S = 20 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int op [ N ] = { 0 } ; comb ( arr , N , K , 0 , op , 0 , S ) ; return 0 ; }
Traverse the matrix in Diagonally Bottum | C ++ implementation to traverse the matrix in the bottom - up fashion using Recursion ; Recursive function to traverse the matrix Diagonally Bottom - up ; Static variable for changing Row and column ; Flag variable for handling Bottum up diagonal traversing ; Base Condition ; Condition when to traverse Bottom Diagonal of the matrix ; Print matrix cell value ; Recursive function to traverse The matrix diagonally ; Recursive function to change diagonal ; Driver Code ; Initialize the 5 x 5 matrix ; Function call for traversing matrix
#include <iostream> NEW_LINE using namespace std ; bool traverseMatrixDiagonally ( int m [ ] [ 5 ] , int i , int j , int row , int col ) { static int k1 = 0 , k2 = 0 ; static bool flag = true ; if ( i >= row j >= col ) { if ( flag ) { int a = k1 ; k1 = k2 ; k2 = a ; flag = ! flag ; k1 ++ ; } else { int a = k1 ; k1 = k2 ; k2 = a ; flag = ! flag ; } cout << endl ; return false ; } cout << m [ i ] [ j ] << " ▁ " ; if ( traverseMatrixDiagonally ( m , i + 1 , j + 1 , row , col ) ) { return true ; } if ( traverseMatrixDiagonally ( m , k1 , k2 , row , col ) ) { return true ; } return true ; } int main ( ) { int mtrx [ 5 ] [ 5 ] = { { 10 , 11 , 12 , 13 , 14 } , { 15 , 16 , 17 , 18 , 19 } , { 20 , 21 , 22 , 23 , 24 } , { 25 , 26 , 27 , 28 , 29 } , { 30 , 31 , 32 , 33 , 34 } } ; traverseMatrixDiagonally ( mtrx , 0 , 0 , 5 , 5 ) ; }
Minimum colors required such that edges forming cycle do not have same color | C ++ implementation to find the minimum colors required to such that edges forming cycle don 't have same color ; Variable to store the graph ; To store that the vertex is visited or not ; Boolean Value to store that graph contains cycle or not ; Variable to store the color of the edges of the graph ; Function to traverse graph using DFS Traversal ; Loop to iterate for all edges from the source vertex ; If the vertex is not visited ; Condition to check cross and forward edges of the graph ; Presence of Back Edge ; Driver Code ; Loop to run DFS Traversal on vertex which is not visited ; Loop to print the colors of the edges
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int n = 5 , m = 7 ; vector < pair < int , int > > g [ m ] ; int col [ n ] ; bool cyc ; int res [ m ] ; void dfs ( int v ) { col [ v ] = 1 ; for ( auto p : g [ v ] ) { int to = p . first , id = p . second ; if ( col [ to ] == 0 ) { dfs ( to ) ; res [ id ] = 1 ; } else if ( col [ to ] == 2 ) { res [ id ] = 1 ; } else { res [ id ] = 2 ; cyc = true ; } } col [ v ] = 2 ; } int main ( ) { g [ 0 ] . push_back ( make_pair ( 1 , 0 ) ) ; g [ 0 ] . push_back ( make_pair ( 2 , 1 ) ) ; g [ 1 ] . push_back ( make_pair ( 2 , 2 ) ) ; g [ 1 ] . push_back ( make_pair ( 3 , 3 ) ) ; g [ 2 ] . push_back ( make_pair ( 3 , 4 ) ) ; g [ 3 ] . push_back ( make_pair ( 4 , 5 ) ) ; g [ 4 ] . push_back ( make_pair ( 2 , 6 ) ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( col [ i ] == 0 ) { dfs ( i ) ; } } cout << ( cyc ? 2 : 1 ) << endl ; for ( int i = 0 ; i < m ; ++ i ) { cout << res [ i ] << ' ▁ ' ; } return 0 ; }
Perfect Sum Problem | C ++ implementation of the above approach ; Function to print the subsets whose sum is equal to the given target K ; Create the new array with size equal to array set [ ] to create binary array as per n ( decimal number ) ; Convert the array into binary array ; Calculate the sum of this subset ; Check whether sum is equal to target if it is equal , then print the subset ; Function to find the subsets with sum K ; Calculate the total no . of subsets ; Run loop till total no . of subsets and call the function for each subset ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumSubsets ( vector < int > set , int n , int target ) { int x [ set . size ( ) ] ; int j = set . size ( ) - 1 ; while ( n > 0 ) { x [ j ] = n % 2 ; n = n / 2 ; j -- ; } int sum = 0 ; for ( int i = 0 ; i < set . size ( ) ; i ++ ) if ( x [ i ] == 1 ) sum = sum + set [ i ] ; if ( sum == target ) { cout << ( " { " ) ; for ( int i = 0 ; i < set . size ( ) ; i ++ ) if ( x [ i ] == 1 ) cout << set [ i ] << " , ▁ " ; cout << ( " } , ▁ " ) ; } } void findSubsets ( vector < int > arr , int K ) { int x = pow ( 2 , arr . size ( ) ) ; for ( int i = 1 ; i < x ; i ++ ) sumSubsets ( arr , i , K ) ; } int main ( ) { vector < int > arr = { 5 , 10 , 12 , 13 , 15 , 18 } ; int K = 30 ; findSubsets ( arr , K ) ; return 0 ; }
Sum of subsets of all the subsets of an array | O ( N ) | C ++ implementation of the approach ; To store factorial values ; Function to return ncr ; Function to return the required sum ; Initialising factorial ; Multiplier ; Finding the value of multipler according to the formula ; To store the final answer ; Calculate the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxN  10 NEW_LINE int fact [ maxN ] ; int ncr ( int n , int r ) { return ( fact [ n ] / fact [ r ] ) / fact [ n - r ] ; } int findSum ( int * arr , int n ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; int mul = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) mul += ( int ) pow ( 2 , i ) * ncr ( n - 1 , i ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += mul * arr [ i ] ; return ans ; } int main ( ) { int arr [ ] = { 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findSum ( arr , n ) ; return 0 ; }
Count number of ways to reach destination in a maze | C ++ implementation of the approach ; Function to return the count of possible paths in a maze [ R ] [ C ] from ( 0 , 0 ) to ( R - 1 , C - 1 ) that do not pass through any of the marked cells ; If the initial cell is blocked , there is no way of moving anywhere ; Initializing the leftmost column ; If we encounter a blocked cell in leftmost row , there is no way of visiting any cell directly below it . ; Similarly initialize the topmost row ; If we encounter a blocked cell in bottommost row , there is no way of visiting any cell directly below it . ; The only difference is that if a cell is - 1 , simply ignore it else recursively compute count value maze [ i ] [ j ] ; If blockage is found , ignore this cell ; If we can reach maze [ i ] [ j ] from maze [ i - 1 ] [ j ] then increment count . ; If we can reach maze [ i ] [ j ] from maze [ i ] [ j - 1 ] then increment count . ; If the final cell is blocked , output 0 , otherwise the answer ; Function to return the count of all possible paths from ( 0 , 0 ) to ( n - 1 , m - 1 ) ; We have to calculate m + n - 2 C n - 1 here which will be ( m + n - 2 ) ! / ( n - 1 ) ! ( m - 1 ) ! ; Function to return the total count of paths from ( 0 , 0 ) to ( n - 1 , m - 1 ) that pass through at least one of the marked cells ; Total count of paths - Total paths that do not pass through any of the marked cell ; return answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  4 NEW_LINE #define C  4 NEW_LINE int countPaths ( int maze [ ] [ C ] ) { if ( maze [ 0 ] [ 0 ] == -1 ) return 0 ; for ( int i = 0 ; i < R ; i ++ ) { if ( maze [ i ] [ 0 ] == 0 ) maze [ i ] [ 0 ] = 1 ; else break ; } for ( int i = 1 ; i < C ; i ++ ) { if ( maze [ 0 ] [ i ] == 0 ) maze [ 0 ] [ i ] = 1 ; else break ; } for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 1 ; j < C ; j ++ ) { if ( maze [ i ] [ j ] == -1 ) continue ; if ( maze [ i - 1 ] [ j ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) ; if ( maze [ i ] [ j - 1 ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) ; } } return ( maze [ R - 1 ] [ C - 1 ] > 0 ) ? maze [ R - 1 ] [ C - 1 ] : 0 ; } int numberOfPaths ( int m , int n ) { int path = 1 ; for ( int i = n ; i < ( m + n - 1 ) ; i ++ ) { path *= i ; path /= ( i - n + 1 ) ; } return path ; } int solve ( int maze [ ] [ C ] ) { int ans = numberOfPaths ( R , C ) - countPaths ( maze ) ; return ans ; } int main ( ) { int maze [ R ] [ C ] = { { 0 , 0 , 0 , 0 } , { 0 , -1 , 0 , 0 } , { -1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; cout << solve ( maze ) ; return 0 ; }