- array-rearrange, Arrays, Mathematical, Sorting

Maximize the sum of modified values after rearranging given array based on condition array

Given a binary array arr1[] and an integer array arr2[], each of length N, the task is to rearrange the elements in the array arr2 such that the total cost generated is maximized. The total cost generated is calculated by summation of modified values in the arr2 array. The values are modified in such a way that an integer corresponding to the value 0 in the arr1 array has no effect on the other elements but an integer corresponding to value 1 in the arr1 array can double the value of the next integer.Examples: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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.Input: N = 2, arr1 = [1, 0], arr2 = [3, 4]Output: 11Explanation: Element 3 corresponds to value 1 so it can double the value of the next element. Out of 2 arrangements possible [3, 4] and [4, 3] so in the 1st case cost generated is 3+4*2 = 11 and in the 2nd case the cost generated is 4+3=7 Input: N = 5, arr1 = [1, 0, 1, 0, 1], arr2 = [3, 7, 2, 12, 5]Output: 53Explanation: Maximum cost can be generated in the arrangement [3, 7, 2, 5, 12] here 1st, 3rd and 4th elements correspond to value 1 and hence their next elements cost can be doubled so cost is 3+7*2+2+5*2+12*2=53Approach:  Given problem can be solved by using the greedy approach. The idea is to sort the array in descending order then iterate it to calculate the cost generated. Below steps can be followed:Initialize an auxiliary array arr1 and copy all elements of arrayarr2 into it, which have corresponding value 1 in the array arr1 Find the min value in the array arr1, remove it from the array and store it in a variable, say valSort the array arr2 in descending orderInitialize a variable ans to calculate the maximum cost generatedIf all elements in the array arr1 are 1 then double the value of all elements except the min value val and return their sumElse add double the value of arr1 elements into ans, and rest all elements without modificationPython3  def max_pow(arr1, arr2):          count = arr1.count(1)                  cost1 = []      for i in range(len(arr1)):        if(arr1[i] == 1):            cost1.append(arr2[i])    val = min(cost1)          del arr2[arr2.index(val)]      arr2.sort(reverse = True)          ans = 0          if(len(arr2) == count-1):        ans = sum(arr2)*2 + val      else:        ans = val + sum(arr2)+sum(arr2[:count])    return ans    N = 5arr_type = [1, 0, 1, 0, 1]arr_power = [3, 2, 7, 12, 5]print(max_pow(arr_type, arr_power))Time Complexity: O(N*log N)Space Complexity: O(N)