Program to find maximum sum obtained of any permutation in Python


Suppose we have an array nums, and another array called requests where requests[i] = [start_i, end_i], this represents the ith request asks for the sum of nums[start_i] + nums[start_i+1] + ... + nums[end_i-1] + nums[end_i]. We have to find the maximum total sum of all requests among all permutations of nums. The answer may be very large, so return it modulo 10^9+7.

So, if the input is like nums = [10,20,30,40,50] requests = [[1,3],[0,1]], then the output will be 190, because if we arrange like [30,50,40,20,10] we get: from requests[0] : nums[1] + nums[2] + nums[3] = 50 + 40 + 20 = 110, and from requests[1] : nums[0] + nums[1] = 30 + 50 = 80, so total sum 110+80 = 190.

To solve this, we will follow these steps:

  • A := a new list
  • for each request (s, e) in requests, do
    • insert pair (s, 0) at the end of A
    • insert pair (e, 1) at the end of A
  • sort the list A
  • fr := an empty map
  • cnt := 0
  • n := size of A
  • i := 0
  • while i < n, do
    • r := 1
    • while i < n - 1 and A[i+1] is same as A[i], do
      • r := r + 1
      • i := i + 1
    • cnt := cnt + r
    • if cnt - r > 0, then
      • insert (pre, p-1) at the end of fr[cnt-r]
        • pre := p
    • otherwise when flag is same as 1, then
      • cnt := cnt - r
      • insert (pre, p) at the end of fr[cnt+r]
      • pre := p+1
    • i := i + 1
  • sort the list nums in reverse order
  • ks := a new list from list of all keys of fr
  • sort the list ks in reverse order
  • ans := 0
  • m := 10^9 + 7
  • i := 0
  • for each k in ks, do
    • for each pair (s, e) in fr[k], do
      • d := e - s + 1
      • ans := ans + (sum of all elements of nums[from index i to i+d-1]) * k
      • ans := ans mod m
      • i := i + d
  • return ans

Example

Let us see the following implementation to get better understanding −

from collections import defaultdict
def solve(nums, requests):
   A = []
   for s, e in requests:
      A.append((s, 0))
      A.append((e, 1))
   A.sort()
   fr = defaultdict(list)
   cnt = 0

   n = len(A)
   i = 0
   while i < n:
      r = 1
      while i < n - 1 and A[i+1] == A[i]:
         r += 1
         i += 1
      p, flag = A[i]
      if flag == 0:
         cnt += r
         if cnt - r > 0:
            fr[cnt-r].append((pre, p-1))
         pre = p
      elif flag == 1:
         cnt -= r
         fr[cnt+r].append((pre, p))
         pre = p+1
      i += 1

   nums.sort(reverse=True)
   ks = list(fr.keys())
   ks.sort(reverse=True)
   ans = 0
   m = 10**9 + 7
   i = 0
   for k in ks:
      for s, e in fr[k]:
         d = e - s + 1
         ans += sum(nums[i:i+d]) * k
         ans %= m
         i += d
   return ans

nums = [10,20,30,40,50]
requests = [[1,3],[0,1]]
print(solve(nums, requests))

Input

[10,20,30,40,50],[[1,3],[0,1]]

Output

190

Updated on: 04-Oct-2021

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements