Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

689. Maximum Sum of 3 Non-Overlapping Subarrays

Hard50.5% Acceptance
ArrayDynamic Programming
Asked by:
F
Facebook
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

Given an integer array nums and an integer k, find three non-overlapping subarrays of length k with maximum sum and return them.

Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.

Example 1:

Input: nums = [1,2,1,2,6,7,5,1], k = 2
Output: [0,3,5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically smaller.

Example 2:

Input: nums = [1,2,1,2,1,2,1,2,1], k = 2
Output: [0,2,4]

Constraints:

  • 1 <= nums.length <= 2 * 104
  • 1 <= nums[i] < 216
  • 1 <= k <= floor(nums.length / 3)
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

The key challenge in #689 Maximum Sum of 3 Non-Overlapping Subarrays is selecting three subarrays of fixed length k such that their total sum is maximized while ensuring they do not overlap. A common strategy combines prefix sums with dynamic programming and a sliding window technique.

First, compute the sum of every window of size k. This can be efficiently done using a sliding window so that each new window is updated in constant time. Next, maintain helper arrays that store the best window index from the left and the best window index from the right for every position. These arrays help quickly determine the optimal first and third subarrays when considering a middle subarray.

By iterating through possible middle subarray positions and combining them with the best choices on both sides, we can track the maximum total sum. This structured approach avoids brute force and ensures an efficient solution with linear time complexity.

Complexity

ApproachTime ComplexitySpace Complexity
Sliding Window + Dynamic ProgrammingO(n)O(n)

Video Solution Available

NeetCodeIO

View all video solutions

Solutions (12)

Prefix Sum with Sliding Window

This approach uses a prefix sum array to quickly calculate the sum of any subarray and a sliding window technique to explore possible starting points for the subarrays.

Time Complexity: O(n), since we make a constant number of passes through the array.
Space Complexity: O(n), due to the prefix, left, and right arrays.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4void maxSumOfThreeSubarrays(int* nums, int numsSize, int k, int* returnSize)





Explanation

This C solution uses three arrays:

  • prefix[] for prefix sums to compute subarray sums quickly.
  • left[] to store the starting index of the max subarray sum from the left.
  • right[] to store the starting index of the max subarray sum from the right.

It iterates over potential middle subarray starting points and uses these auxiliary arrays to compute the total sum efficiently. Updates are made to get the largest sum and store associated index positions in result[].

Dynamic Programming with Sliding Window

This approach employs dynamic programming alongside a sliding window to optimize subarray sum calculations and ensure non-overlapping conditions.

Time Complexity: O(n), for traversal of the nums array multiple times.
Space Complexity: O(n), utilizing the dp, prefix, left, and right arrays.

CC++JavaPythonC#JavaScript
1








Video Solutions

Watch expert explanations and walkthroughs

Maximum Sum of 3 Non-Overlapping Subarrays - Leetcode 689 - Python

NeetCodeIO
21:1711,532 views

Asked By Companies

1 companies
F
Facebook

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Trapping Rain WaterHard
Jump Game IIMedium
Maximum SubarrayMedium
Jump GameMedium
More similar problems

Related Topics

ArrayDynamic Programming

Problem Stats

Acceptance Rate50.5%
DifficultyHard
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why are prefix sums or sliding windows used in this problem?

Prefix sums or sliding windows allow efficient calculation of fixed-length subarray sums. Instead of recalculating sums repeatedly, each new window can be updated in constant time, reducing the overall complexity to linear time.

Is Maximum Sum of 3 Non-Overlapping Subarrays asked in FAANG interviews?

Yes, this type of problem is common in FAANG-style interviews because it tests dynamic programming, array manipulation, and optimization techniques. Candidates must combine multiple ideas like prefix sums and greedy selection efficiently.

What data structure is best for Maximum Sum of 3 Non-Overlapping Subarrays?

Arrays are the primary data structures used in this problem. They store precomputed window sums and track the best left and right subarray indices, enabling constant-time lookups during iteration.

What is the optimal approach for Maximum Sum of 3 Non-Overlapping Subarrays?

The optimal approach uses a combination of sliding window and dynamic programming. First compute sums of all subarrays of length k, then maintain best left and right choices for each position. This allows efficient evaluation of each possible middle subarray.

{
5
int
prefix
[
numsSize
+
1
]
;
6
int
left
[
numsSize
]
;
7
int
right
[
numsSize
]
;
8
int
result
[
3
]
;
9
10
// Calculate the prefix sum array
11
prefix
[
0
]
=
0
;
12
for
(
int
i
=
0
;
i
<
numsSize
;
++
i
)
{
13
prefix
[
i
+
1
]
=
prefix
[
i
]
+
nums
[
i
]
;
14
}
15
16
// Calculate max left subarray positions
17
for
(
int
i
=
k
,
total
=
prefix
[
k
]
-
prefix
[
0
]
;
i
<
numsSize
;
++
i
)
{
18
if
(
prefix
[
i
+
1
]
-
prefix
[
i
+
1
-
k
]
>
total
)
{
19
total
=
prefix
[
i
+
1
]
-
prefix
[
i
+
1
-
k
]
;
20
left
[
i
]
=
i
+
1
-
k
;
21
}
else
{
22
left
[
i
]
=
left
[
i
-
1
]
;
23
}
24
}
25
26
// Calculate max right subarray positions
27
right
[
numsSize
-
k
]
=
numsSize
-
k
;
28
for
(
int
i
=
numsSize
-
k
-
1
,
total
=
prefix
[
numsSize
]
-
prefix
[
numsSize
-
k
]
;
i
>=
0
;
--
i
)
{
29
if
(
prefix
[
i
+
k
]
-
prefix
[
i
]
>=
total
)
{
30
total
=
prefix
[
i
+
k
]
-
prefix
[
i
]
;
31
right
[
i
]
=
i
;
32
}
else
{
33
right
[
i
]
=
right
[
i
+
1
]
;
34
}
35
}
36
37
// Calculate max sum by considering middle subarray
38
int
maxSum
=
0
;
39
for
(
int
i
=
k
;
i
<=
numsSize
-
2
*
k
;
++
i
)
{
40
int
l
=
left
[
i
-
1
]
;
41
int
r
=
right
[
i
+
k
]
;
42
int
totalSum
=
(
prefix
[
i
+
k
]
-
prefix
[
i
]
)
+
(
prefix
[
l
+
k
]
-
prefix
[
l
]
)
+
(
prefix
[
r
+
k
]
-
prefix
[
r
]
)
;
43
if
(
totalSum
>
maxSum
)
{
44
maxSum
=
totalSum
;
45
result
[
0
]
=
l
;
46
result
[
1
]
=
i
;
47
result
[
2
]
=
r
;
48
}
49
}
50
51
*
returnSize
=
3
;
52
for
(
int
i
=
0
;
i
<
3
;
++
i
)
{
53
printf
(
"%d "
,
result
[
i
]
)
;
54
}
55
}
56
57
int
main
(
)
{
58
int
nums
[
]
=
{
1
,
2
,
1
,
2
,
6
,
7
,
5
,
1
}
;
59
int
k
=
2
;
60
int
returnSize
;
61
maxSumOfThreeSubarrays
(
nums
,
8
,
k
,
&
returnSize
)
;
62
return
0
;
63
}
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
void
maxSumOfThreeSubarraysDP
(
int
*
nums
,
int
numsSize
,
int
k
,
int
*
returnSize
)
{
5
int
*
prefix
=
(
int
*
)
malloc
(
(
numsSize
+
1
)
*
sizeof
(
int
)
)
;
6
int
*
left
=
(
int
*
)
malloc
(
numsSize
*
sizeof
(
int
)
)
;
7
int
*
right
=
(
int
*
)
malloc
(
numsSize
*
sizeof
(
int
)
)
;
8
int
*
dp
=
(
int
*
)
malloc
(
numsSize
*
sizeof
(
int
)
)
;
9
int
result
[
3
]
=
{
-
1
,
-
1
,
-
1
}
;
10
11
prefix
[
0
]
=
0
;
12
for
(
int
i
=
0
;
i
<
numsSize
;
++
i
)
{
13
prefix
[
i
+
1
]
=
prefix
[
i
]
+
nums
[
i
]
;
14
}
15
16
for
(
int
i
=
0
;
i
<
k
;
i
++
)
{
17
dp
[
i
]
=
prefix
[
i
+
1
]
;
18
}
19
20
for
(
int
i
=
k
;
i
<
numsSize
;
++
i
)
{
21
if
(
prefix
[
i
+
1
]
-
prefix
[
i
+
1
-
k
]
>=
dp
[
i
-
1
]
)
{
22
dp
[
i
]
=
prefix
[
i
+
1
]
-
prefix
[
i
+
1
-
k
]
;
23
left
[
i
]
=
i
+
1
-
k
;
24
}
else
{
25
dp
[
i
]
=
dp
[
i
-
1
]
;
26
left
[
i
]
=
left
[
i
-
1
]
;
27
}
28
}
29
30
right
[
numsSize
-
k
]
=
numsSize
-
k
;
31
for
(
int
i
=
numsSize
-
k
-
1
;
i
>=
0
;
--
i
)
{
32
if
(
prefix
[
i
+
k
]
-
prefix
[
i
]
>=
prefix
[
right
[
i
+
1
]
+
k
]
-
prefix
[
right
[
i
+
1
]
]
)
{
33
right
[
i
]
=
i
;
34
}
else
{
35
right
[
i
]
=
right
[
i
+
1
]
;
36
}
37
}
38
39
int
maxSum
=
0
;
40
for
(
int
i
=
k
;
i
<=
numsSize
-
2
*
k
;
++
i
)
{
41
int
l
=
left
[
i
-
1
]
;
42
int
r
=
right
[
i
+
k
]
;
43
int
totalSum
=
(
prefix
[
i
+
k
]
-
prefix
[
i
]
)
+
(
prefix
[
l
+
k
]
-
prefix
[
l
]
)
+
(
prefix
[
r
+
k
]
-
prefix
[
r
]
)
;
44
if
(
totalSum
>
maxSum
)
{
45
maxSum
=
totalSum
;
46
result
[
0
]
=
l
;
47
result
[
1
]
=
i
;
48
result
[
2
]
=
r
;
49
}
50
}
51
52
*
returnSize
=
3
;
53
for
(
int
i
=
0
;
i
<
3
;
++
i
)
{
54
printf
(
"%d "
,
result
[
i
]
)
;
55
}
56
57
free
(
prefix
)
;
58
free
(
left
)
;
59
free
(
right
)
;
60
free
(
dp
)
;
61
}
62
63
int
main
(
)
{
64
int
nums
[
]
=
{
1
,
2
,
1
,
2
,
6
,
7
,
5
,
1
}
;
65
int
k
=
2
;
66
int
returnSize
;
67
maxSumOfThreeSubarraysDP
(
nums
,
8
,
k
,
&
returnSize
)
;
68
return
0
;
69
}

Explanation

This C solution introduces a dynamic programming table (dp) to record the maximum sum up to each point. Using left and right tracking, it determines the best left and right subarrays. A single pass checks for optimal middle subarrays using this dynamic information.