Contiguous Subarray Max
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;        
[...]