Given an array arr[] consisting of N integers and an array Q[][], where each row denotes a range {l, r}(0 ? l ? r ? N – 1). The task is to find the maximum sum of all subarrays starting from index 0 by rearranging the array except the elements in the ranges given in Q[][].
Examples:
Input: arr[] = {-8, 4, -2, -6, 4, 7, 1}, Q[][] = {{0, 0}, {4, 5}}Output: -15
Explanation:
The given array can be rearranged as {-8, 4, 1, -2, 4, 7, -6}. Now, the sum of all the subarrays starting from the first index are:
Sum of arr[0, 0] = -8
Sum of arr[0, 1] = -8 + 4 = -4
Sum of arr[0, 2] = -8 + 4 + 1 = -3
Sum of arr[0, 3] = -8 + 4 + 1 + (-2) = -5
Sum of arr[0, 4] = -8 + 4 +1 + (-2) + 4 = -1
Sum of arr[0, 5] = -8 + 4 + 1 + (-2) + 4 + 7= 6
Sum of arr[0, 6] = -8 + 4 +1 + (-2) + 4 + 7 + (-6) = 0
The total sum = -8 + (-4) + (-3)+ (-5) + (-1) + 6 + 0 = -15
Input: arr[] = {-8, 4, 3}, Q[][] = {{0, 2}}
Output: -13
Explanation: All elements are present in the given set of ranges. Therefore, no element can rearranged. Now, the sum of all subarrays from the first index are:
Sum of arr[0, 0] = -8
Sum of arr[0, 1] = -8 + 4 = -4
Sum of arr[0, 2] = -8 + 4 + 3 = -1
Total sum = -8 + (-4) + (-1) = -13
Approach: The idea is to first find the elements that cannot be rearranged. Then, sort the remaining elements in decreasing order and assign them to the indices such that the largest element will be assigned to the smallest index that is allowed to be rearranged. Follow the steps below to solve the problem:
- Create a vector s and v to store the indices and elements respectively, that can be rearranged.
- Traverse the elements for each range {l, r} and mark them visited.
- Traverse the given array and store the index i, if it is not marked visited.
- Sort the vector v in descending order.
- Insert the element in the given array as arr[s[i]] = v[i] where i is up to the number of elements that are allowed to be rearranged.
- After the above steps, print the sum of the prefix sums of the given array as the maximum sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int n, int a[], int l[][2], int q)
{
vector< int > v;
int d[n] = { 0 };
for ( int i = 0; i < q; i++) {
for ( int x = l[i][0];
x <= l[i][1]; x++) {
if (d[x] == 0) {
d[x] = 1;
}
}
}
set< int > st;
for ( int i = 0; i < n; i++) {
if (d[i] == 0) {
v.push_back(a[i]);
st.insert(i);
}
}
sort(v.begin(), v.end(),
greater< int >());
int c = 0;
for ( auto it : st) {
a[it] = v;
c++;
}
int pref_sum = 0;
int temp_sum = 0;
for ( int i = 0; i < n; i++) {
temp_sum += a[i];
pref_sum += temp_sum;
}
return pref_sum;
}
int main()
{
int arr[] = { -8, 4, -2, -6, 4, 7, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
int q[][2] = { { 0, 0 }, { 4, 5 } };
int queries = sizeof (q) / sizeof (q[0]);
cout << maxSum(N, arr, q, queries);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxSum( int n, int a[], int [][]l, int q)
{
Vector<Integer> v = new Vector<>();
int []d = new int [n];
for ( int i = 0 ; i < q; i++)
{
for ( int x = l[i][ 0 ];
x <= l[i][ 1 ]; x++)
{
if (d[x] == 0 )
{
d[x] = 1 ;
}
}
}
HashSet<Integer> st = new HashSet<>();
for ( int i = 0 ; i < n; i++)
{
if (d[i] == 0 )
{
v.add(a[i]);
st.add(i);
}
}
Collections.sort(v);
Collections.reverse(v);
int c = 0 ;
for ( int it : st)
{
a[it] = v.get(c);
c++;
}
int pref_sum = 0 ;
int temp_sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
temp_sum += a[i];
pref_sum += temp_sum;
}
return pref_sum;
}
public static void main(String[] args)
{
int []arr = { - 8 , 4 , - 2 , - 6 , 4 , 7 , 1 };
int N = arr.length;
int [][]q = { { 0 , 0 }, { 4 , 5 } };
int queries = q.length;
System.out.print(maxSum(N, arr, q, queries));
}
}
|
Python3
def maxSum(n, a, l, q):
v = []
d = [ 0 ] * n
for i in range (q):
for x in range (l[i][ 0 ],
l[i][ 1 ] + 1 ):
if (d[x] = = 0 ):
d[x] = 1
st = set ([])
for i in range (n):
if (d[i] = = 0 ):
v.append(a[i])
st.add(i)
v.sort(reverse = True )
c = 0
for it in st:
a[it] = v
c + = 1
pref_sum = 0
temp_sum = 0
for i in range (n):
temp_sum + = a[i]
pref_sum + = temp_sum
return pref_sum
if __name__ = = "__main__" :
arr = [ - 8 , 4 , - 2 ,
- 6 , 4 , 7 , 1 ]
N = len (arr)
q = [[ 0 , 0 ], [ 4 , 5 ]]
queries = len (q)
print (maxSum(N, arr, q, queries))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxSum( int n, int []a, int [,]l, int q)
{
List< int > v = new List< int >();
int []d = new int [n];
for ( int i = 0; i < q; i++)
{
for ( int x = l[i, 0];
x <= l[i, 1]; x++)
{
if (d[x] == 0)
{
d[x] = 1;
}
}
}
HashSet< int > st = new HashSet< int >();
for ( int i = 0; i < n; i++)
{
if (d[i] == 0)
{
v.Add(a[i]);
st.Add(i);
}
}
v.Sort();
v.Reverse();
int c = 0;
foreach ( int it in st)
{
a[it] = v;
c++;
}
int pref_sum = 0;
int temp_sum = 0;
for ( int i = 0; i < n; i++)
{
temp_sum += a[i];
pref_sum += temp_sum;
}
return pref_sum;
}
public static void Main(String[] args)
{
int []arr = { -8, 4, -2, -6, 4, 7, 1 };
int N = arr.Length;
int [,]q = { { 0, 0 }, { 4, 5 } };
int queries = q.GetLength(0);
Console.Write(maxSum(N, arr, q, queries));
}
}
|
Javascript
<script>
function maxSum(n, a, l, q)
{
let v = [];
let d = new Array(n);
for (let i = 0; i < n; i++)
{
d[i] = 0;
}
for (let i = 0; i < q; i++)
{
for (let x = l[i][0];
x <= l[i][1]; x++)
{
if (d[x] == 0)
{
d[x] = 1;
}
}
}
let st = new Set();
for (let i = 0; i < n; i++)
{
if (d[i] == 0)
{
v.push(a[i]);
st.add(i);
}
}
v.sort( function (a, b){ return b - a;});
let c = 0;
for (let it of st.values())
{
a[it] = v;
c++;
}
let pref_sum = 0;
let temp_sum = 0;
for (let i = 0; i < n; i++)
{
temp_sum += a[i];
pref_sum += temp_sum;
}
return pref_sum;
}
let arr = [ -8, 4, -2, -6, 4, 7, 1 ];
let N = arr.length;
let q = [ [ 0, 0 ], [ 4, 5 ] ];
let queries = q.length;
document.write(maxSum(N, arr, q, queries));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)