class Solution { public int maxSubArray(int[] nums) { //kadane's algorithm will be used to find maximum subarray //current sum , overall max subarray int csum = nums[0] , omax = nums[0] , n = nums.length; for(int i =1 ; i < n ; i++){ int value = nums[i]; //if previous train is +ve jump into it if(csum >= 0) csum += value; //else start your own else csum = value; //update max omax = Math.max(omax , csum); } return omax; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter