Sum of Array maximums after K operations by reducing max element to its half
Last Updated :
23 Feb, 2023
Given an array arr[] of N integers and an integer K, the task is to find the sum of maximum of the array possible wherein each operation the current maximum of the array is replaced with its half.
Example:
Input: arr[] = {2, 4, 6, 8, 10}, K = 5
Output: 33
Explanation: In 1st operation, the maximum of the given array is 10. Hence, the value becomes 10 and the array after 1st operation becomes arr[] = {2, 4, 6, 8, 5}.
The value after 2nd operation = 18 and arr[] = {2, 4, 6, 4, 5}.
Similarly, proceeding forward, value after 5th operation will be 33.
Input: arr[] = {6, 5}, K = 3
Output: 14
Approach: The given problem can be solved with the help of a greedy approach. The idea is to use a max heap data structure. Therefore, traverse the given array and insert all the elements in the array arr[] into a max priority queue. At each operation, remove the maximum from the heap using pop operation, add it to the value and reinsert the value after dividing it by two into the heap. The value after repeating the above operation K times is the required answer.
Below is the implementation of the above approach:
C++
// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum possible
// value after K given operations
int maxValue(vector<int> arr, int K)
{
// Stores required value
int val = 0;
// Initializing priority queue
// with all elements of array
priority_queue<int> pq(arr.begin(),
arr.end());
// Loop to iterate through
// each operation
while (K--) {
// Current Maximum
int max = pq.top();
pq.pop();
// Update value
val += max;
// Reinsert maximum
pq.push(max / 2);
}
// Return Answer
return val;
}
// Driver Call
int main()
{
vector<int> arr = { 2, 4, 6, 8, 10 };
int K = 5;
cout << maxValue(arr, K);
}
Java
// Java code for the above approach
import java.util.Comparator;
import java.util.PriorityQueue;
class CustomComparator implements Comparator<Integer> {
@Override
public int compare(Integer number1, Integer number2)
{
int value = number1.compareTo(number2);
// elements are sorted in reverse order
if (value > 0) {
return -1;
}
else if (value < 0) {
return 1;
}
else {
return 0;
}
}
}
class GFG {
// Function to find maximum possible
// value after K given operations
static int maxValue(int[] arr, int K)
{
// Stores required value
int val = 0;
// Initializing priority queue
// with all elements of array
PriorityQueue<Integer> pq
= new PriorityQueue<Integer>(new CustomComparator());
for (int i = 0; i < arr.length; i++) {
pq.add(arr[i]);
}
// Loop to iterate through
// each operation
while (K != 0) {
// Current Maximum
int max = pq.poll();
// Update value
val += max;
// Reinsert maximum
pq.add((int)Math.floor(max / 2));
K = K - 1;
}
// Return Answer
return val;
}
// Driver Call
public static void main(String[] args)
{
int[] arr = { 2, 4, 6, 8, 10 };
int K = 5;
System.out.println(maxValue(arr, K));
}
}
// This code is conbtributed by Potta Lokesh
Python3
# python3 program of the above approach
from queue import PriorityQueue
# Function to find maximum possible
# value after K given operations
def maxValue(arr, K):
# Stores required value
val = 0
# Initializing priority queue
# with all elements of array
pq = PriorityQueue()
for dt in arr:
pq.put(-1*dt)
# Loop to iterate through
# each operation
while (True):
# Current Maximum
max = -1 * pq.get()
# Update value
val += max
# Reinsert maximum
pq.put(-1 * (max // 2))
K -= 1
if K == 0:
break
# Return Answer
return val
# Driver Call
if __name__ == "__main__":
arr = [2, 4, 6, 8, 10]
K = 5
print(maxValue(arr, K))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
class CustomComparator : IComparer<int> {
public int Compare(int number1, int number2)
{
int value = number1.CompareTo(number2);
// elements are sorted in reverse order
if (value > 0) {
return -1;
}
else if (value < 0) {
return 1;
}
else {
return 0;
}
}
}
public class GFG {
// Function to find maximum possible
// value after K given operations
static int maxValue(int[] arr, int K)
{
// Stores required value
int val = 0;
// Initializing priority queue
// with all elements of array
SortedSet<int> pq
= new SortedSet<int>(new CustomComparator());
for (int i = 0; i < arr.Length; i++) {
pq.Add(arr[i]);
}
// Loop to iterate through each operation
while (K != 0) {
// Current Maximum
int max = pq.Min;
// Update value
val += max;
// Reinsert maximum
pq.Add((int)Math.Floor((double)max / 2));
pq.Remove(max);
K = K - 1;
}
// Return Answer
return val;
}
static public void Main()
{
// Code
int[] arr = { 2, 4, 6, 8, 10 };
int K = 5;
Console.WriteLine(maxValue(arr, K));
}
}
// This code is conbtributed by lokesh.
JavaScript
class PriorityQueue {
constructor(comparator) {
this.heap = [];
this.comparator = comparator;
}
add(element) {
this.heap.push(element);
this.bubbleUp(this.heap.length - 1);
}
peek() {
return this.heap[0];
}
poll() {
const poppedValue = this.heap[0];
const bottom = this.heap.length - 1;
if (bottom > 0) {
this.swap(0, bottom);
}
this.heap.pop();
this.bubbleDown(0);
return poppedValue;
}
size() {
return this.heap.length;
}
bubbleUp(index) {
while (index > 0) {
const parentIndex = Math.floor((index + 1) / 2) - 1;
if (this.comparator(this.heap[index], this.heap[parentIndex]) < 0) {
this.swap(index, parentIndex);
index = parentIndex;
} else {
break;
}
}
}
bubbleDown(index) {
let swapping = true;
while (swapping) {
const leftChildIndex = 2 * (index + 1) - 1;
const rightChildIndex = 2 * (index + 1);
let minIndex = index;
if (
leftChildIndex < this.heap.length &&
this.comparator(this.heap[leftChildIndex], this.heap[minIndex]) < 0
) {
minIndex = leftChildIndex;
}
if (
rightChildIndex < this.heap.length &&
this.comparator(this.heap[rightChildIndex], this.heap[minIndex]) < 0
) {
minIndex = rightChildIndex;
}
if (minIndex !== index) {
this.swap(index, minIndex);
index = minIndex;
} else {
swapping = false;
}
}
}
swap(index1, index2) {
const temp = this.heap[index1];
this.heap[index1] = this.heap[index2];
this.heap[index2] = temp;
}
}
// Function to find maximum possible
// value after K given operations
function maxValue(arr, K) {
// Stores required value
let val = 0;
// Initializing priority queue
// with all elements of array
const pq = new PriorityQueue((a, b) => b - a);
arr.forEach((element) => pq.add(element));
// Loop to iterate through
// each operation
while (K--) {
// Current Maximum
const max = pq.peek();
pq.poll();
// Update value
val += max;
// Reinsert maximum
pq.add(Math.floor(max / 2));
}
// Return Answer
return val;
}
// Driver Call
const arr = [2, 4, 6, 8, 10];
const K = 5;
document.write(maxValue(arr, K));
Time Complexity: O(K*log N)
Auxiliary Space: O(N)
Similar Reads
Maximum sum of all elements of array after performing given operations Given an array of integers. The task is to find the maximum sum of all the elements of the array after performing the given two operations once each. The operations are: 1. Select some(possibly none) continuous elements from the beginning of the array and multiply by -1. 2. Select some(possibly none
7 min read
Find the number of operations required to make all array elements Equal Given an array of N integers, the task is to find the number of operations required to make all elements in the array equal. In one operation we can distribute equal weights from the maximum element to the rest of the array elements. If it is not possible to make the array elements equal after perfo
6 min read
Maximum value in an array after m range increment operations Consider an array of size n with all initial values as 0. We need to perform the following m range increment operations.increment(a, b, k) : Increment values from 'a' to 'b' by 'k'. After m operations, we need to calculate the maximum of the values in the array.Examples:Input : n = 5 m = 3 a = 0, b
10 min read
Maximize sum of array elements removed by performing the given operations Given two arrays arr[] and min[] consisting of N integers and an integer K. For each index i, arr[i] can be reduced to at most min[i]. Consider a variable, say S(initially 0). The task is to find the maximum value of S that can be obtained by performing the following operations: Choose an index i an
8 min read
Maximum subarray sum possible after removing at most K array elements Given an array arr[] of size N and an integer K, the task is to find the maximum subarray sum by removing at most K elements from the array. Examples: Input: arr[] = { -2, 1, 3, -2, 4, -7, 20 }, K = 1 Output: 26 Explanation: Removing arr[5] from the array modifies arr[] to { -2, 1, 3, -2, 4, 20 } Su
15+ min read
Maximize first array element by performing given operations at most K times Given an array arr[] of size N an integer K, the task is to find the maximize the first element of the array by performing the following operations at most K times: Choose a pair of indices i and j (0 ? i, j ? N-1) such that |i ? j| = 1 and arri > 0.Set arri = arri ? 1 and arrj = arrj + 1 on thos
7 min read
Maximize length of subarray of equal elements by performing at most K increment operations Given an array A[] consisting of N integers and an integer K, the task is to maximize the length of the subarray having equal elements after performing at most K increments by 1 on array elements. Note: Same array element can be incremented more than once. Examples: Input: A[] = {2, 4, 8, 5, 9, 6},
8 min read
Maximize Array sum by replacing any K elements by its modulo with any positive integer Given an array of positive integer arr[], and a number K. the task is to maximize the sum of the array by replacing any K elements of the array by taking modulus with any positive integer which is less than arr[i] i.e, (arr[i] = arr[i]%X where X ⤠arr[i]). Examples: Input: arr[] = {5, 7, 18, 12, 11,
5 min read
Largest element after K operations on Array Given array A[] (1 <= A[i] <= 108) of size N (2 <= N <= 103) and integer K (1 <= K <= 108), the task is to find the largest element of the array after performing the given operation at most K times, in one operation choose i from 1 to N - 1 such that A[i] <= A[i + 1] and then in
13 min read
Reduce given Array to 0 by maximising sum of chosen elements Given an array arr[] containing N positive integers, the task is to maximize the array sum when after every sum operation all the remaining array elements decrease by 1. Note: The value of an array element does not go below 0. Examples: Input: arr[] = {6, 2, 4, 5} Output: 12 Explanation: Add 6 initi
9 min read