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!
Select
Add answer anonymously...

Meesho Software Developer interview questions & answers

A Software Developer was asked 7mo agoQ. Design a cab booking system (LLD).
A Software Developer was asked 7mo agoQ. How do you implement concurrency?
A Software Developer was asked 9mo agoQ. Lld of cab booking system

Popular interview questions of Software Developer

A Software Developer was asked 7mo agoQ1. Design a cab booking system (LLD).
A Software Developer was asked 7mo agoQ2. How do you implement concurrency?
A Software Developer was asked 9mo agoQ3. Lld of cab booking system
Meesho Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits