Open In App

Maximize Array sum by adding multiple of another Array element in given ranges

Last Updated : 21 Feb, 2023
Comments
Improve
Suggest changes
3 Likes
Like
Report

Given two arrays X[] and Y[] of length N along with Q queries each of type [L, R] that denotes the subarray of X[] from L to R. The task is to find the maximum sum that can be obtained by applying the following operation for each query:

  • Choose an element from Y[].
  •  Add multiples with alternate +ve and -ve signs to the elements of the subarray. i.e., if the chosen element is 4, then modify the subarray as {XL+ 4, XL+1 - 8, . . . (till Rth) index}
  • Delete the element chosen from Y.

Note: 1-based indexing is used here.

Examples:

Input: N = 4, X[] = {1, 2, 3, 4}, Y[] = {2, 3, 5, 6}, K = 1, query = {{3, 3}}
Output: 16
Explanation:
Number of queries = 1
Sub-array from start to end index of X[]: {3}
Chose 6 from Y[] and then add alternative series of multiple of 6 = {3 + 6} = {9}. Rest of the elements except the sub-array will remain the same, Therefore, new X[] is: {1, 2, 9, 4}. The maximum sum that can obtain from 
X[] = 1+ 2+ 9+ 4 = 16

Input: N = 5, X[] = {5, 7, 2, 1, 8}, Y[] = {1, 2, 3, 4, 5}, K = 2, queries = {{1, 4}, {1, 5}}
Output: 36
Explanation:
start = 1, end = 4
The subarray = {7, 2, 1, 8}
Lets chose 1 from Y[] and add series of multiple of 1 in subarray = {7 + 1, 2 - 2, 1 + 3, 8 - 4} = {8, 0, 4, 4}.
X[]: {5, 8, 0, 4, 4}
Now, start = 1, end = 5
The subarray = {5, 8, 0, 4, 4}
lets chose 5 from Y[] and add series of multiple of 5 in subarray = {5 + 5, 8 - 10, 0 + 15,  4 - 20, 4 + 25} = {10, -2, 15, -16, 29}. Now updated X[] will be: {10, -2, 15, -16, 29}.
Overall sum of X[] is : (10 - 2 + 15 - 16 + 29) = 36. It can be verified that this sum is maximum possible.  

Intuition: The intuition behind the approach is provided below

Let us take an example of series of multiple of an integer let say K. Then the series will be as the picture below:

Series of multiple of K

It can be seen clearly that if subarray of series is of odd length then it will contribute a positive sum in the overall sum, While series of even length will contribute negative sum to the total sum.

So the optimal idea is to add the multiples of the biggest value to the largest odd length subarray and the multiples of the smallest value to the largest even length subarray.

Naive Approach: 

In this method, we will do the same as mentioned in the problem statement. We will traverse on each sub-array by the given start and end indices in each query and add series of multiple of the optimal element at that current state so that our sum is maximized.

Follow the steps mentioned below to implement the idea:

  • Make ArrayList of Pairs<Start, End> DataType and initialize it with Pairs of start and end indices of given sub-arrays in query.
  • Sort Queries in ArrayList according to the length of sub-arrays, Formally arrange Pairs in descending order of length.  
  • Sort Y[]. It will be convenient to get minimum and maximum elements and delete them after use.
  • Run a loop number of times queries are asked then do the following steps:
    • Calculate the length of the current subarray. If the length is even take a minimum element of Y[] else take the maximum. 
    • Traverse on sub-array and add the series of Multiple into it.
    • Delete the used element of Y[].
  • Calculate the overall sum of elements present in the X[] by traversing array X[] 
  • Print the sum as the required answer.

Below is the implementation of the above approach.

C++
Java Python3 JavaScript C#

Output
17

Time Complexity: O(N2), As Selection Sort is used
Auxiliary Space: O(K), As ArrayList of Pair is used of Size K 

Efficient Approach: 

In this method, we will not be traversing on sub-array for each query. We will direct obtain the increment or decrement using a direct mathematical formula. From the intuition we can conclude that:

  • If length of sub-array is odd, Then increment in overall sum of X[] will be = (((length + 1) / 2) * element)
  • If the length of the sub-array is even, Then the decrement in overall sum of X[] will be = - ((length / 2) * element)

Here element is chosen element from Y[], and length is length of sub-array in query.

Follow the steps mentioned below to implement the idea:

  • Create a variable sum and calculate the overall sum of the elements initially present in X[].
  • Create a list and initialize it with the length of subarrays in K queries.
  • Sort list and the array Y[].
  • Run a loop from the back to the front of the list(Formally Descending order length) and do the following:
    • If length is odd add  (((length+1)/2)*element) in sum variable else subtract ((length/2)*element) from sum variable.
  •  Print the value of the sum variable.

Below is the implementation of the above approach.

C++
Java Python3 C# JavaScript

Output
17

Time Complexity: O(Y * log Y), As sorting is performed on Y[]. 
Auxiliary Space: O(K), As an ArrayList of size K is used.

Related Articles:


Next Article

Similar Reads