Choose Three Disjoint Equal-Length Windows with Lexicographic Ties

Quick Overview

Choose Three Disjoint Equal-Length Windows with Lexicographic Ties. Choose three nonoverlapping length-k contiguous subarrays of nums maximizing their combined sum.

Choose Three Disjoint Equal-Length Windows with Lexicographic Ties

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Choose three nonoverlapping length-k contiguous subarrays of nums maximizing their combined sum. Return their starting indices in increasing order. Among equal-sum choices return the lexicographically smallest index triple. ```cpp class Solution { public: vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k); }; ``` ### Examples **Example 1:** ```text nums = [2,1,5,1,5,1], k = 1 Output: [0,2,4] Explanation: Values 2,5,5 give the maximum sum 12. ``` **Example 2:** ```text nums = [1,1,1,1,1,1,1], k = 2 Output: [0,2,4] Explanation: Every valid triple ties, so choose the earliest one. ``` ### Constraints - Array size: 1..20000. Values are positive and less than 65536. Window length k is between 1 and floor(nums.length/3). ### Clarifying Questions Enforce all three nonoverlap gaps and the lexicographically smallest maximizing triple. ```hint Read the boundary condition Once a middle window is fixed, can the best left and right windows be chosen independently? ``` ### Contract considerations Enforce all three nonoverlap gaps and the lexicographically smallest maximizing triple.

Overview: Choose Three Disjoint Equal-Length Windows with Lexicographic Ties. Choose three nonoverlapping length-k contiguous subarrays of nums maximizing their combined sum.

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Choose three nonoverlapping length-k contiguous subarrays of nums maximizing their combined sum. Return their starting indices in increasing order. Among equal-sum choices return the lexicographically smallest index triple.

class Solution {
public:
    vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k);
};

Examples

Example 1:

nums = [2,1,5,1,5,1], k = 1
Output: [0,2,4]
Explanation: Values 2,5,5 give the maximum sum 12.

Example 2:

nums = [1,1,1,1,1,1,1], k = 2
Output: [0,2,4]
Explanation: Every valid triple ties, so choose the earliest one.

Constraints

  • Array size: 1..20000. Values are positive and less than 65536. Window length k is between 1 and floor(nums.length/3).

Clarifying Questions Guidance

Enforce all three nonoverlap gaps and the lexicographically smallest maximizing triple.

Contract considerations

Enforce all three nonoverlap gaps and the lexicographically smallest maximizing triple.