algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
2 shown · 0/261 solved

Maximum Third-Smallest Partition Sum

GoldCommunity Beta
Asked atInfosys
Solve problem →
2000ms256MBAdded Sep 11, 2026

You are given an integer array nums.

Partition the entire array into contiguous groups such that every group contains at least 3 elements. The score of a group is its third-smallest value. For the group [8, 2, 5, 1, 10] the sorted order is [1, 2, 5, 8, 10], so its score is 5. Equal values count separately: the third-smallest of [4, 4, 4] is 4.

Return the maximum total score over all valid partitions. If the array cannot be partitioned into valid groups (fewer than 3 elements), return -1.

Examples

Example 1

Input: nums = [8, 2, 5, 1, 10]
Output: 5
Explanation: With 5 elements the only valid partition is the whole array as one group, whose third-smallest value is 5.

Example 2

Input: nums = [1, 2, 3, 4, 5, 6]
Output: 9
Explanation: [1, 2, 3] scores 3 and [4, 5, 6] scores 6, total 9. Keeping everything in one group would score only 3.

Example 3

Input: nums = [5, 5]
Output: -1
Explanation: Two elements cannot form a group of at least 3.

Constraints

  • 1≤1 \leq1≤ nums.length ≤105\leq 10^5≤105
  • 1≤1 \leq1≤ nums[i] ≤109\leq 10^9≤109

Details

Solved by2 people
Time limit2000ms
Memory256MB
AddedSep 11, 202620 hours ago