Skip to content
Related Articles
Open in App
Not now

Related Articles

Minimum operations to make Array elements 0 by decrementing pair or single element

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 21 Sep, 2022
Improve Article
Save Article
Like Article

Given an array arr[] of size N, the task is to find the minimum number of operations required to reduce all three elements of the array to zero. Following operations are allowed:

  • Reduce 2 different array elements by one.
  • Reduce a single array element by one.

Example:

Input: arr[] = {1, 2, 3}, N = 3
Output: 3
Explanation : Operation 1: reduce 3 and 2 to get {1, 1, 2}
Operation 2: reduce 1 and 2 to get {1, 0, 1}
Operation 3: reduce both 1s to get {0, 0, 0}

Input: arr[] = {5, 1, 2, 9, 8}, N = 5
Output: 13

Approach:

This problem can be solved using greedy approach. The idea is to reduce the 2 largest elements at a time or (if not possible) 1 at a time.  As we need the largest elements in each step, we can use a max heap.

The following steps can be taken to solve this approach:

  • Initiate a count variable as 0.
  • Insert all the elements in a max heap.
    • Reduce the two largest elements.
    • Insert the reduced values again into the heap.
  • Repeat above mentioned steps until all array elements become zero and increase the count at each iteration.
  • Stop when all array elements are zero.

Below is the implementation of the above approach:

C++

#include <bits/stdc++.h>
using namespace std;

// C++ program to reduce array to zero in minimum steps
int reduceArray(int arr[], int N)
    {
         int count = 0;
        priority_queue<int>pq;
  
        for (int i = 0; i < N; i++) {
            pq.push(arr[i] * -1);
        }
        while (pq.size() > 1) {
            
            int temp1 = pq.top();
            pq.pop();
            
            int temp2 = pq.top();
          pq.pop();
            count++;
            temp1++;
            temp2++;
            if (temp1 != 0)
                pq.push(temp1);
            if (temp2 != 0)
                pq.push(temp2);
        }
        if (pq.size() > 0){
          
            count -= pq.top();
          pq.pop();
        }
        return count-1;
    }

 // Driver Code
int main() {
    int arr[] = { 1, 2, 3 };
        int N = 3;
        int count = reduceArray(arr, N);
        cout << count;
    return 0;
}

// This code is contributed by satwik4409. 

Java

// Java program to reduce array to zero in minimum steps
import java.util.*;

class GFG {

    public static int reduceArray(int arr[], int N)
    {

        int count = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>();

        for (int i = 0; i < N; i++) {
            pq.add(arr[i] * -1);
        }
        while (pq.size() > 1) {
            int temp1 = pq.poll();
            int temp2 = pq.poll();
            count++;
            temp1++;
            temp2++;
            if (temp1 != 0)
                pq.add(temp1);
            if (temp2 != 0)
                pq.add(temp2);
        }
        if (pq.size() > 0)
            count -= pq.poll();
        return count;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3 };
        int N = 3;
        int count = reduceArray(arr, N);
        System.out.println(count);
    }
}

Python3

# Python program to reduce array to zero in minimum steps
from queue import PriorityQueue

def reduceArray(arr, N):
    count = 0
    pq = PriorityQueue()

    for i in range(N):
        pq.put(arr[i] * -1)

    while (pq.qsize() > 1):
        temp1 = pq.get()
        temp2 = pq.get()
        count += 1
        temp1 += 1
        temp2 += 1
        if (temp1 != 0):
            pq.put(temp1)
        if (temp2 != 0):
            pq.put(temp2)

    if (pq.qsize() > 0):
        count -= pq.get()
    return count

# Driver Code
arr = [1, 2, 3]
N = 3
count = reduceArray(arr, N)
print(count)

# This code is contributed by hrithikgarg03188.

C#

// C# implementation of above approach
using System;
using System.Collections.Generic;

class GFG {

   public static int reduceArray(int[] arr, int N)
    {
 
        int count = 0;
        List<int> pq = new List<int>();
 
        for (int i = 0; i < N; i++) {
            pq.Add(arr[i] * -1);
        }
        while (pq.Count > 1) {
            
            int temp1 = pq[0];
            pq.RemoveAt(0);
            int temp2 = pq[0];
            pq.RemoveAt(0);
            count++;
            temp1++;
            temp2++;
            if (temp1 != 0)
                pq.Add(temp1);
            if (temp2 != 0)
                pq.Add(temp2);
        }
        if (pq.Count > 0)
            count -= pq[0];
            pq.RemoveAt(0);
        return count-1;
    }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 1, 2, 3 };
    int N = 3;
    int count = reduceArray(arr, N);
    Console.Write(count);
  }
}

// This code is contributed by sanjoy_62.

Javascript

<script>

// Javascript Priority Queue implementation
function PriorityQueue () {
    let collection = [];
    this.printCollection = function() {
      (console.log(collection));
    };
    this.enqueue = function(element){
        if (this.isEmpty()){ 
            collection.push(element);
        } else {
            let added = false;
            for (let i=0; i<collection.length; i++){
                 if (element[1] < collection[i][1]){ //checking priorities
                    collection.splice(i,0,element);
                    added = true;
                    break;
                }
            }
            if (!added){
                collection.push(element);
            }
        }
    };
    this.dequeue = function() {
        let value = collection.shift();
        return value[0];
    };
    this.top = function() {
        return collection[0];
    };
    this.size = function() {
        return collection.length; 
    };
    this.isEmpty = function() {
        return (collection.length === 0); 
    };
}

// Javascript program to reduce array to zero in minimum steps

function reduceArray(arr, N)
    {
         let count = 0;
        let pq = new PriorityQueue();
  
        for (let i = 0; i < N; i++) {
            pq.enqueue(arr[i] * -1);
        }
        while (pq.size() > 1) {
            
            let temp1 = pq.top();
            pq.dequeue();
            
            let temp2 = pq.top();
          pq.dequeue();
            count++;
            temp1++;
            temp2++;
            if (temp1 != 0)
                pq.enqueue(temp1);
            if (temp2 != 0)
                pq.enqueue(temp2);
        }
        if (pq.size() > 0){
          
            count -= pq.top();
          pq.dequeue();
        }
        return count-1;
    }

 // Driver Code
    let arr = [ 1, 2, 3 ];
    let N = 3;
    let count = reduceArray(arr, N);
    console.log(count);
    
    // This code is contributed by akashish__

</script>
Output

3

Time Complexity: O(N * logN)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!