At each index
Keep track of Prev_of array A and B
And select the numbers accordingly
Since, Prev_a <= NExt num
For loop should start from NExt_num to nums[index]
To take all positbilites
Now,
At initial stages,
Prev A = 0, Prev B = 50
Take max and min,
So initial will not affect
C++
#define MOD 1000000007
class Solution {
public:
int dp[2002][52][52];
int find_ans(int index, vector<int>& nums, int a_prev, int b_prev){
if(index == nums.size()){
return 1;
}
if(dp[index][a_prev][b_prev] != -1)
return dp[index][a_prev][b_prev];
int ans = 0;
for(int i=a_prev; i<=nums[index]; i++){
int x = i, y = nums[index] - i;
if(a_prev <= x and b_prev>=y)
ans = ((ans%MOD) + (find_ans(index+1, nums, x, y)%MOD)%MOD);
}
return dp[index][a_prev][b_prev] = ans;
}
int countOfPairs(vector<int>& nums) {
memset(dp, -1, sizeof(dp));
return find_ans(0, nums, 0, 50);
}
};