Given an array **arr[]** and **K** subarrays in the form (L_{i}, R_{i}), the task is to find the maximum possible value of . It is allowed to shuffle the array before calculating this value to get the maximum sum.

**Examples:**

Input:arr[] = {1, 2, 4, 2, 1}, subarrays = {{1, 2}, {2, 4}, {1, 3}, {5, 5}, {3, 5}}

Output:26

Explanation:

Shuffled Array will to get the maximum sum – {2, 4, 2, 1, 1}

Subarray Sum = arr[1:2] + arr[2:4] + arr[1:3] + arr[5:5] + arr[3:5]

=> 6 + 7 + 8 + 1 + 4 = 26

Input:arr[] = {4, 1, 2, 1, 9, 2}, subarrays = {{1, 2}, {1, 3}, {1, 4}, {3, 4}}

Output:49

Explanation:

Shuffled Array will to get the maximum sum – {2, 4, 9, 2, 1, 1}

Subarray Sum = arr[1:2] + arr[1:3] + arr[1:4] + arr[3:4]

=> 6 + 15 + 17 + 11 = 49

**Naive Approach:** A simple solution is to compute the maximum sum of all possible permutations of the given array and check which sequence gives us the maximum summation.

**Efficient Approach:** The idea is to use Prefix Arrays to find out the frequency of indices over all subarrays. We can do this as follows:

//Initialize an array prefSum[n] = {0, 0, ...0} for each L_{i}, R_{i}: prefSum[L_{i}]++ prefSum[R_{i}+1]-- // Find Prefix sum for i=1 to N: prefSum[i] += prefSum[i-1] // prefSum contains frequency of // each index over all subarrays

Finally, greedily choose the index with the highest frequency and put the largest element of the array at that index. This way we will get the largest possible sum.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// maximum sum of K subarrays ` `// when shuffling is allowed ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// maximum sum of all subarrays ` `int` `maximumSubarraySum( ` ` ` `int` `a[], ` `int` `n, ` ` ` `vector<pair<` `int` `, ` `int` `> >& subarrays) ` `{ ` ` ` `// Initialize maxsum ` ` ` `// and prefixArray ` ` ` `int` `i, maxsum = 0; ` ` ` `int` `prefixArray[n] = { 0 }; ` ` ` ` ` `// Find the frequency ` ` ` `// using prefix Array ` ` ` `for` `(i = 0; i < subarrays.size(); ++i) { ` ` ` `prefixArray[subarrays[i].first - 1]++; ` ` ` `prefixArray[subarrays[i].second]--; ` ` ` `} ` ` ` ` ` `// Perform prefix sum ` ` ` `for` `(i = 1; i < n; i++) { ` ` ` `prefixArray[i] += prefixArray[i - 1]; ` ` ` `} ` ` ` ` ` `// Sort both arrays to get a greedy result ` ` ` `sort(prefixArray, ` ` ` `prefixArray + n, ` ` ` `greater<` `int` `>()); ` ` ` `sort(a, a + n, greater<` `int` `>()); ` ` ` ` ` `// Finally multiply largest frequency with ` ` ` `// largest array element. ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `maxsum += a[i] * prefixArray[i]; ` ` ` ` ` `// Return the answer ` ` ` `return` `maxsum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `// Initial Array ` ` ` `int` `a[] = { 4, 1, 2, 1, 9, 2 }; ` ` ` ` ` `// Subarrays ` ` ` `vector<pair<` `int` `, ` `int` `> > subarrays; ` ` ` `subarrays.push_back({ 1, 2 }); ` ` ` `subarrays.push_back({ 1, 3 }); ` ` ` `subarrays.push_back({ 1, 4 }); ` ` ` `subarrays.push_back({ 3, 4 }); ` ` ` ` ` `// Function Call ` ` ` `cout << maximumSubarraySum(a, n, ` ` ` `subarrays); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

49

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Array range queries over range queries
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Find out the correct position of the ball after shuffling
- Queries for counts of array elements with values in given range
- Queries for count of array elements with values in given range with updates
- Find the Initial Array from given array after range sum queries
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Queries for elements having values within the range A to B using MO's Algorithm
- Range Queries to count the number of even parity values with updates
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Perform append, update, delete and range sum queries on the given array
- Sum of prime numbers in range [L, R] from given Array for Q queries
- Maximum array sum with prefix and suffix multiplications with -1 allowed
- Maximum sum of pairwise product in an array with negative allowed
- Sum of even values and update queries on an array
- Range Queries to find the Element having Maximum Digit Sum
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Find the element having maximum set bits in the given range for Q queries

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.