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

Maximumsumofkconsecutive

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

class MaximumSumOfKConsecutive {

  static int func(int[] nums, int k) {
    int maxSum = 0;
    int currentSum = 0;
    for (int i = 0; i < k; i++) {
      currentSum += nums[i];
    }
    maxSum = currentSum;
    for( int i = k; i < nums.length; i++ ){
      currentSum += nums[i] - nums[i-k];
      maxSum = Math.max(currentSum, maxSum);
    }
    return maxSum;
  }

  public static void main(String[] args) {
    int[] nums = { 5, -10, 6, 90, 3 };
    int result = func(nums, 2);
    System.out.println(result);
  }
}

Created 2021-11-14T05:24:05+00:00 · Edit