Sum of Array maximums after K operations by reducing max element to its half


In this article, we will calculate the sum of the array maximums after K operations in which we reduce the maximum value of the array to its half.

In the first approach, we will implement the brute force solution to this problem. In each iteration, we will use a for loop to find the maximum element in the array. We will then add this element to our answer, then we will reduce the element to its half. We will do this for K iterations as requested. Then, we will return the answer.

In the second approach, we will use a priority queue data structure. Initially, we will insert all the array elements in the priority queue. Then, for K iterations, we will get the maximum element from the priority queue and add it to the answer. We will then reduce the element to half and then insert it back in the priority queue.

Problem Statement

Given an array with N integers and an integer K, we need to find the sum of the maximums after we do the following operation K times.

Operation −

  • Find the maximum element in the array.

  • Reduce the maximum element to its half.

Sample Examples

Input

arr = {1,2,4,8,16}, K=2

Output

24

Explanation

In the first iteration, the maximum value is 16, so we add 16 to our answer and make it half in the array. So, our array becomes {1,2,4,8,8}.

In the second iteration, the maximum value is 8, so we add 8 to our answer.

Hence the answer = 16+8 = 24.

Input

arr = {8,8,8,8}, K=4

Output

32

Explanation

In the first iteration, the maximum value is 8, so we add 8 to our answer and make it half in the array. So, our array becomes {4,8,8,8}.

In the second iteration, the maximum value is still 8, so we add 8 to our answer and make it half in the array. So, our array becomes {4,4,8,8}.

Similarly, we do the third and the fourth iteration.

Hence the answer = 8+8+8+8 = 32.

Input

arr = {2,33,71,81,93}, K=10

Output

459

Explanation

In the first iteration, the maximum value is 93, so we add 93 to our answer and make it half in the array. So, our array becomes {2,33,71,81,46}.

Similarly, we do the remaining 9 operations to get our final answer.

Hence the answer = 93+81+71+46+40+35+33+23+20+17 = 459.

Approach 1

In this approach, we will essentially use the brute force approach to solve the given problem. We will use a while loop, which will run K times. In each iteration of the while loop, we will use a for loop, which will traverse through the array and find the maximum value of the elements present in the array and the index of the maximum value element. We will then add this maximum value to our answer, and we will reduce the value to half inside the array. After the while loop ends, we will return the answer.

Algorithm

  • Step 1 − Make a while loop which will run K number of times.

  • Step 2 − In each iteration of the while loop:

  • Step 2.1 − Make a for loop that will traverse the elements of the array

  • Step 2.2 − Find the maximum element of the array and store it in a variable, mx.

  • Step 2.3 − Also find the index of the index of the maximum element of the array and store it in another variable, ind.

  • Step 3.1 − Add the variable mx to the answer.

  • Step 3.2 − Reduce the value of mx to half by accessing the element using its index, ind.

  • Step 4.1 − After the while loop ends, return the answer.

Example

#include <bits/stdc++.h>
using namespace std;
int sums_of_maximum(vector<int> &arr,int K){
   int N = arr.size();
   int res = 0;
   while(K--){
      int mx = arr[0];
      int ind = 0;
      for(int i=1;i<N;i++){
         if(arr[i]>mx){
            mx = arr[i];
            ind = i;
         }
      }
      res = res + mx;
      arr[ind] = arr[ind]/2;
   }
   return res;
}
int main(){
   vector<int> arr = {2, 33, 71, 81, 93};
   int K = 10;
   cout<<sums_of_maximum(arr,K)<<endl;
   return 0;
}

Output

459

Time complexity − O(N^2), where N is the number of elements in the array.

Space complexity − O(N)

Approach 2

In this approach, we will reduce the time complexity by using the priority queue data structure, max heap, to store the elements of the array and get the maximum of the array in log(N) time. We will store all the elements of the array in the priority queue and for each of the K iterations, we will take out the maximum element of the array, add it to our answer, reduce it by half and finally push the element back into the queue.

Algorithm

  • Step 1 − Make a priority queue.

  • Step 1.1 − Insert all the elements of the array in the priority queue.

  • Step 2 − Make a while loop which will run K number of times.

  • Step 3 − In each iteration of the while loop:

  • Step 3.1 − Get the maximum element of the array from the priority queue.

  • Step 3.2 − Add the maximum element to the answer.

  • Step 3.3 − Reduce the maximum element by half and push it back in the queue.

  • Step 4.1 − After the while loop ends, return the answer.

Example

#include <bits/stdc++.h>
using namespace std;
int sums_of_maximum(vector<int> &arr,int K){
   int N = arr.size();
   priority_queue<int> ele;
   for(int i=0;i<N;i++)ele.push(arr[i]);
   int res = 0;
   while(K--){
      int mx = ele.top();
      ele.pop();
      res = res + mx;
      mx = mx/2;
      ele.push(mx);
   }
   return res;
}
int main(){
   vector<int> arr = {2, 33, 71, 81, 93};
   int K = 10;
   cout<<sums_of_maximum(arr,K)<<endl;
   return 0;
}

Output

459

Time complexity – O(N + K*log(N)), where N is the number of elements in the array.

Space complexity – O(N), to store the elements in the queue.

Updated on: 01-Nov-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements