Maximum Sum of Two Non-Overlapping Subarrays

Given an integer array ARR and a positive integer K, your task is to determine two non-overlapping subarrays of length K that yield the maximum combined sum.

Example:

Input:
ARR = [2, 5, 1, 2, 7, 3, 0], K = 2
Output:
17
Explanation:

The subarrays [2, 5] and [7, 3] can be selected, providing a sum of 2 + 5 + 7 + 3 = 17, which is the maximum sum possible with non-overlapping subarrays of length K.

Constraints:

  • 1 <= T <= 10^2 (number of test cases)
  • 2 <= N <= 5 * 10^3 (length of the array)
  • 1 <= K <= N / 2
  • -10^5 <= ARR[i] <= 10^5 (element value range)

Input:

First line contains an integer 'T', representing the number of test cases. For each test case: First line contains two integers, 'N' and 'K'. Second line contains 'N' space-separated integers, representing elements of the array ARR.

Output:

For each test case, output a single integer denoting the maximum possible sum of two non-overlapping subarrays, each of length K.

Note:

You do not need to print anything, as it has already been taken care of. Just implement the function to get the desired result.
AnswerBot
1y

The task is to find two non-overlapping subarrays of length K in an array, such that their sum is maximum.

  • Iterate through the array and calculate the sum of each subarray of length K

  • Store the maximum ...read more

Help your peers!
Add answer anonymously...
Meesho Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter