Jayakrishna14

Kadanes Basic

Jul 22nd, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.33 KB | None | 0 0
  1. int maxSubarraySum(int[] arr) {
  2.  
  3.         int maxSum = Integer.MIN_VALUE;
  4.         int curSum = 0;
  5.        
  6.         for(int el: arr) {
  7.             curSum += el;
  8.             maxSum = Math.max(maxSum, curSum);
  9.             if(curSum < 0) {
  10.                 curSum = 0;
  11.             }
  12.         }
  13.        
  14.         return maxSum;
  15.     }
Tags: Java kadanes
Advertisement
Add Comment
Please, Sign In to add comment