Jayakrishna14

Kadanes Ordered Set

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