code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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