Doubt in subarray

size of array : 3
array elements : 1 2 4
subarray with max sum is : 1 2 4
sum is : 7
This is the output when I use the code with Complexity O(N^3)

size of arra : 3
array elements : 1 2 4
subarray with max sum is : 2 4
sum is : 6
This is the output when I use the code with Complexity O(N^2)

why output is coming different?

Hello @yashsaxena986,

Possibly, you have done a mistake while writing the second code.
Please, share your code so that i can verify it for the mistake.

Actually the code with complexity O(n^2) is working only in coding blocks ide , I tried the code in other online compilers but the result was different and one more thing the line that is finding currentsum that is currsum=cumsum[j]-cumsum[i-1] is pointing to cumsum[-1] which is out of bound so technically it should through a garbage value. I have made some changes in the code and now it is working fine in every ide

Hey @yashsaxena986,

  1. As you have pointed out.
    This is the only reason for getting wrong output in different compilers.
    This compiler is reading 0 at -1 index.

  2. C++ doesn’t support out of bound error.
    This is what you must have read in other languages like java.
    Here, it will read the value stored at one location before cumsum[0] in the computer’s memory.

  3. Yes, this code requires some changes.
    That was the reason I asked for your code.

Its good to see that you have solved this problem yourself.:+1::slightly_smiling_face: