This is a snippet from this code to finding sums of all subarrays of a given array, but this doesn't work as intended as the problem seems to be lying with pre-incrementing value of i in the equation.
while(i<vec.size()-1){
sum += -vec[i-2] + vec[++i] ;
nums.emplace_back(sum);
}
return nums;
However, when i deploy this code which seems to be the same implementation as the previous one, the code runs correctly:
while(i<vec.size()-1){
sum += -vec[i-2] + vec[i+1] ;
nums.emplace_back(sum);
i++;
}
Please Clarify!
I tried it on: (1,2,3,4,) Expected: 6,9,12 Output from 1st snippet: 6,8,10 //Incorrect Output from 2nd snippet: 6,9,12 //Correct