Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximum Sum Obtained of Any Permutation

XPChallenge Points: 20
levelLevel: Medium

We are given an integer array nums and another list called requests.
Each request is a pair [l, r], meaning that we must consider the sum:

nums[l] + nums[l+1] + ... + nums[r]

You are allowed to rearrange (permute) the elements of nums in any order.

Your goal is to place larger numbers in positions that appear more frequently across requests, so that the total contribution of all requested ranges becomes as large as possible.

The result may become very large, so return it modulo 1,000,000,007 (10^9 + 7).

Example 1:

Input: nums = [5, 1, 3, 2] requests = [[0,2], [1,3]]

Output: 19

Explanation:

An optimal permutation is [5,3,2,1]. Request [0,2] → 5 + 3 + 2 = 10 Request [1,3] → 3 + 2 + 1 = 6 Total = 10 + 6 = 16 A better arrangement gives total = 26.

Example 2:

Input: nums = [4,7,2] requests = [[1,1]]

Output: 7

Explanation:

Only index 1 is counted, so we place the largest number at index 1.

Example 3:

Input: nums = [8,1,6,2,9] requests = [[0,4], [2,4], [3,3]]

Output: 58

Explanation:

Index 3 appears more often, so we place one of the largest values there.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result