Print that sub-array with max sum

can we print the sub-array also in kadanes algorithm without changing the time complexity ?

@rajnisharyan01 yes you can when you are getting a bigger value keep updating the right pointer so at last just go from right to left and when sum becomes equal to max sum you get the left pointer

int main() { int a[] = {-4,1,3,-2,6,2,-1,-4,-7}; int n = sizeof(a)/sizeof(int); int cs = 0; int ms = 0; int right = -1; int left = -1; for(int i=0;i<n;i++){ cs = cs + a[i]; if(cs<0){ cs = 0; } ms = max(cs,ms); }

please edit the code.

@rajnisharyan01 share the code using cb ide

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.