Contiguous Subarray Max I'm trying to solve the contiguous subarray problem on CodeSignal &amp; I came up with the following solution which is based off of Kadane's Algorithm. My implementation passed the given test data set however upon submission, it failed. There's no reason given on CodeSignal &amp; I can't look at it since I just started doing it &amp; have less coins.
I can't figure out why my solution is failing but the solution based on online pseudo code works, even though my solution passed the preliminary tests. I think it might have to do with integer overflow or some other reason that's why I've come here to seek help from experts.
My implementation:
 public int findConsecutiveSubarray(int nums[]) {
    int currentSum = 0;
    int max =Integer.MIN_VALUE;;

    int i=0;
    while(i &lt; nums.length) {
        currentSum += nums[i];

        if(max &lt; currentSum)
            max = currentSum;

        if(max &lt; nums[i]) {
            max = nums[i];
            currentSum = nums[i];
        }

        i++;
    }
    System.out.println(&quot;maxSum : &quot;+ max);
    return max;
}

Solution based on online:
int arrayMaxConsecutiveSum2(int[] nums) {
        int currentMax = 0;
        int globalMax = 0;
        
        globalMax = currentMax = nums[0];
        
        for(int i=1; i&lt;nums.length; i++) {
            currentMax = Math.max(nums[i], currentMax + nums[i]);
            globalMax = Math.max(globalMax, currentMax);
        }
    
    return globalMax;
}
The issue is that you might not enter the second &quot;if&quot; when the current sum is smaller than nums[i].
Imagine, e.g., that at the beginning of the iteration you have  currentSum=-5, max=100, nums[i]=10.
At the end of the iteration you have currentSum=5 and max=100, yet the desired state is currentSum=10, max=100.
A corresponding instance where your algorithm fails is: 100, -105, 10, 95.
To fix this, the condition of the second &quot;if&quot; should be currentSum &lt; nums[i].
Notice that, once you change this condition, you'll also have to revise other parts of your code accordingly: you can no longer unconditionally assign max to nums[i] in the body of the second &quot;if&quot; statement. This can be easily fixed by removing that assignment and swapping the order of your two ifs.
[...]
        if(currentSum &lt; nums[i])
            currentSum = nums[i];

        if(max &lt; currentSum)
            max = currentSum;        
[...]