why is this code giving runtime error //{ Driver Code Starts #include using namespace std; // } Driver Code Ends class Solution { public: long long numberOfPath(int n, int k, vector &arr){ // Code Here vector dp(n, vector(n, vector(k + 1, 0))); // Initialization for the starting point dp[0][0][arr[0][0]] = 1; for(int s = 0; s 0 && arr[i][j] 0 && arr[i][j]
The code you provided has an issue in the initialization of the `dp` array. Specifically, the initialization of `dp[0][0][arr[0][0]]` is causing a runtime error because it is trying to access an out-of-bounds element. Here's the corrected code: ```cpp #include using namespace std; class Solution { public: long long numberOfPath(int n, int k, vector &arr){ vector dp(n, vector(n, vector(k + 1, 0))); // Initialize dp[0][0][0] to 1 dp[0][0][0] = 1; for(int s = 1; s s) continue; long long up = (i > 0) ? dp[i-1][j][s-arr[i][j]] : 0; long long left = (j > 0) ? dp[i][j-1][s-arr[i][j]] : 0; dp[i][j][s] = up + left; } } } return dp[n-1][n-1][k]; } }; int main() { Solution obj; int n = 3, k = 12; vector arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; cout
Bro this code is getting accepted #include using namespace std; class Solution { public: vector dp; long long helper(int r, int c, int sum, int n, const vector& arr) { if (sum < 0) return 0; if (arr[r][c] > sum) return 0; if (r == n - 1 && c == n - 1) return (arr[r][c] == sum); if (dp[r][c][sum] != -1) return dp[r][c][sum]; long long paths = 0; for (auto d : dir) { int nr = r + d[0]; int nc = c + d[1]; if (nr >= 0 && nc >= 0 && nr < n && nc < n) { paths += helper(nr, nc, sum - arr[r][c], n, arr); } } return dp[r][c][sum] = paths; } long long numberOfPath(int n, int k, const vector& arr) { dp.assign(n, vector(n, vector(k + 1, -1))); return helper(0, 0, k, n, arr); } private: const vector dir = {{1, 0}, {0, 1}}; };
long long dp[101][101][101]; long long solve(vector &arr,int &n,int i,int j, int sum){ if(i==n-1 and j==n-1 ) return dp[i][j][sum]= sum == arr[i][j]; if(dp[i][j][sum]!=-1) return dp[i][j][sum]; dp[i][j][sum]=0; if(arr[i][j]
which software you are using to explain the question?
why is this code giving runtime error
//{ Driver Code Starts
#include
using namespace std;
// } Driver Code Ends
class Solution {
public:
long long numberOfPath(int n, int k, vector &arr){
// Code Here
vector dp(n, vector(n, vector(k + 1, 0)));
// Initialization for the starting point
dp[0][0][arr[0][0]] = 1;
for(int s = 0; s 0 && arr[i][j] 0 && arr[i][j]
The code you provided has an issue in the initialization of the `dp` array. Specifically, the initialization of `dp[0][0][arr[0][0]]` is causing a runtime error because it is trying to access an out-of-bounds element.
Here's the corrected code:
```cpp
#include
using namespace std;
class Solution {
public:
long long numberOfPath(int n, int k, vector &arr){
vector dp(n, vector(n, vector(k + 1, 0)));
// Initialize dp[0][0][0] to 1
dp[0][0][0] = 1;
for(int s = 1; s s)
continue;
long long up = (i > 0) ? dp[i-1][j][s-arr[i][j]] : 0;
long long left = (j > 0) ? dp[i][j-1][s-arr[i][j]] : 0;
dp[i][j][s] = up + left;
}
}
}
return dp[n-1][n-1][k];
}
};
int main() {
Solution obj;
int n = 3, k = 12;
vector arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
cout
Bro this code is getting accepted
#include
using namespace std;
class Solution {
public:
vector dp;
long long helper(int r, int c, int sum, int n, const vector& arr) {
if (sum < 0) return 0;
if (arr[r][c] > sum) return 0;
if (r == n - 1 && c == n - 1) return (arr[r][c] == sum);
if (dp[r][c][sum] != -1) return dp[r][c][sum];
long long paths = 0;
for (auto d : dir) {
int nr = r + d[0];
int nc = c + d[1];
if (nr >= 0 && nc >= 0 && nr < n && nc < n) {
paths += helper(nr, nc, sum - arr[r][c], n, arr);
}
}
return dp[r][c][sum] = paths;
}
long long numberOfPath(int n, int k, const vector& arr) {
dp.assign(n, vector(n, vector(k + 1, -1)));
return helper(0, 0, k, n, arr);
}
private:
const vector dir = {{1, 0}, {0, 1}};
};
long long dp[101][101][101];
long long solve(vector &arr,int &n,int i,int j, int sum){
if(i==n-1 and j==n-1 ) return dp[i][j][sum]= sum == arr[i][j];
if(dp[i][j][sum]!=-1) return dp[i][j][sum];
dp[i][j][sum]=0;
if(arr[i][j]