code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

Maximumsumsubarray

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class MaximumSumSubarray {

  static int func(int[] nums){
    int result = nums[0];
    int current = nums[0];
    for(int i = 0; i < nums.length; i++){
      current = Math.max(current + nums[i], nums[i]);
      result = Math.max(current, result);
    }
    return result;
  }

  public static void main(String[] args){
    int[] nums = {2, 3, -8, 7, -1, 2, 3};
    int result = func(nums);
    System.out.println(result);
  }
}

Created 2021-11-11T00:19:09+00:00 · Edit