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

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