Why coin change is not working


Runtime Error. Your submission stopped because of a runtime error. ex: division by zero, array index out of bounds, uncaught exception You can try testing your code with custom input and try putting debug statements in your code.

terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

int coinChange(vector& coins, int amount) {

    int n=coins.size();
    vector<vector<int>> dp(n+1,vector<int>(amount+1));
    for(int i=0;i<=n;i++)dp[i][0]=0;
    for(int j=1;j<=amount;j++)dp[0][j]=INT_MAX-1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=amount;j++){
            else dp[i][j]=dp[i-1][j];
    if(dp[n][amount]==INT_MAX-1)return -1;
    return dp[n][amount];

int Solution::fibsum(int A) {

vector<int> fab={1,1};

    int size=fab.size();
return coinChange(fab,A);



Its because of the constraints .Lets say for A=1e9 and hence the runtime error.