1 Maximize The Power Given An Array Arr Of N Non Negative Integers And An Array Power Of K Integers Where K Is An Even 1 (193.21 KiB) Viewed 89 times
1 Maximize The Power Given An Array Arr Of N Non Negative Integers And An Array Power Of K Integers Where K Is An Even 2 (97.69 KiB) Viewed 89 times
1 Maximize The Power Given An Array Arr Of N Non Negative Integers And An Array Power Of K Integers Where K Is An Even 3 (75.49 KiB) Viewed 89 times
1. Maximize the Power Given an array arr of n non-negative integers and an array power of k integers where k is an even number, perform the following operation in steps. • Select two integers and such that 0≤i<j</power/. In each operation, i and jare selected independently. • if power ≤ power] add the summation of the subarray arr[power...power[j]] to the power. • if power > power] add the summation of the subarray arr[power[j]...power] to the power. • Delete i-th and j-th elements from power, and its length is reduced by 2. Starting at 0 initial power, maximize the final power after exactly k/2 operations. Return that maximum power modulo (10⁹+7). Note: Subarray arr[...] denotes the subarray formed by the elements arr[l], arr[+1],... arr[r-1], arr[r]. Example arr=[3, 5, 6, 0,7] and power = [3, 1, 0,2]. k= 4, the size of power, so perform k/2=2 operations. One of the optimal approach is shown. • Select -0,j-2. Here, power[0] =3 and power[2]=0. Add the sum of subarray arr[0...3], (3+5+6+0) = 14 to the power, 0+14=14. Remove the two elements from power, and power is [1, 2], • Select i=0, j-1. Here, power[0]=1 and power[1] =2. Add the sum of subarray arr[1..2], 5+6=11, so power is 14+ 11 = 25. 1> #include <assert.h>... 19 20 21 22 23 24 25 26 27 28 29 /* * Complete the 'getMaximumPower' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: 1. INTEGER_ARRAY arr * 2. INTEGER ARRAY power */ int getMaximumPower (int arr_count, int* arr, int power int* power) { 30 31 } 32 33> int main()... Test Custom Innust Run Code Run Tests
One of the optimal approach is shown. • Select /-0, 1-2. Here, power[0] =3 and power[2] = 0. Add the sum of subarray arrf0...3), (3+5+6+0) = 14 to the power, 0+14=14. Remove the two elements from power, and power is [1, 2], • Select i-0, j-1. Here, power(0)=1 and power[1]= 2. Add the sum of subarray arr[1..2], 5+6=11, so power is 14+11=25. Return the maximum power possible, 25. (25 modulo (109+7)=25) Function Description Complete the function getMaximumPower in the editor below. getMaximum Power has the following parameter(s): int arrin): An array of integers int powerfk): An array of integers Returns int: the maximum power modulo 10⁹+7 Constraints 1sns105 • 2sks 105, kis even. Osam≤ 107,0 ≤i<n Os power<n
Constraints 1≤n≤105 • 2≤ k≤ 105, kis even. • Os arr ≤ 107,0si<n ● Os power<n ►Input Format for Custom Testing Sample Case O Sample Input 0 STDIN 5 3 ➜ 20 FUNCTION Explanation ‒‒‒‒‒‒ n = 5 arr = [2, 4, 2, 1, 6] Sample Output 0 k = 4 power = [4, 1, 1, 3]
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!