Will fail for m = 1… once u update b[x] u are using same b[x] to update b[x+1] whereas it has to be the old one. Iterating j backwards will help fix this.
1- without having an element for index 1, there’s no use of an element with index 2
2- If we have x elements that can fit index 1, and we get 1 element for index 2, then we can form x subsequence of length 2, lets store this as y
3-If we find some more elements suitable for index 1 after finding index an element for index 2, they are of no use until we get an element after that which is suitable for index 2
4-If we get an element for index 2 after all this, then the number of subsequences will be [(number of subsequences in step 2 ) + ((number of elements in step 1) + (number of elements in step 3))] = [(number of subsequence in step 2) + (number of elements suitable for index 1 encountered till now)]
5- I will update this new value for y and carry on same steps. {If you notice, its just adding number of ones encountered till now to y, or you can say adding x to y when we discover an element for index 2}
6- Now take an array of k elements, where k is say 4,
so we have to update a[i]=a[i-1] for a value of i if we encounter an element suitable for i th position in the subsequence