Given an array arr[] consisting of N distinct elements, the task is to find the maximum number of pairs required to be decreased by 1 in each step, such that N – 1 array elements are reduced to 0 and the remaining array element is a non-negative integer.
Examples:
Input: arr[] = {1, 2, 3}
Output: 3
Explanation:
Decrease arr[1] and arr[2] by 1 modifies arr[] = {1, 1, 2}
Decrease arr[1] and arr[2] by 1 modifies arr[] = {1, 0, 1}
Decrease arr[0] and arr[2] by 1 modifies arr[] = {0, 0, 0}
Therefore, the maximum number of decrements required is 3.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 7
Approach: The problem can be solved Greedily. Follow the steps below to solve the problem:
- Initialize a variable, say cntOp, to store maximum count of steps required to make (N – 1) elements of the array equal to 0.
- Create a priority queue, say PQ, to store the array elements.
- Traverse the array and insert the array elements into PQ.
- Now repeatedly extract the top 2 elements from the priority queue, decrease the value of both the elements by 1, again insert both the elements in priority queue and increment the cntOp by 1. This process continues while (N – 1) element of the PQ becomes equal to 0.
- Finally, print the value of cntOp
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to count maximum number of steps// to make (N - 1) array elements to 0int cntMaxOperationToMakeN_1_0(int arr[], int N){ // Stores maximum count of steps to make // (N - 1) elements equal to 0 int cntOp = 0; // Stores array elements priority_queue<int> PQ; // Traverse the array for (int i = 0; i < N; i++) { // Insert arr[i] into PQ PQ.push(arr[i]); } // Extract top 2 elements from the array // while (N - 1) array elements become 0 while (PQ.size() > 1) { // Stores top element // of PQ int X = PQ.top(); // Pop the top element // of PQ. PQ.pop(); // Stores top element // of PQ int Y = PQ.top(); // Pop the top element // of PQ. PQ.pop(); // Update X X--; // Update Y Y--; // If X is not equal to 0 if (X != 0) { // Insert X into PQ PQ.push(X); } // if Y is not equal // to 0 if (Y != 0) { // Insert Y // into PQ PQ.push(Y); } // Update cntOp cntOp += 1; } return cntOp;}// Driver Codeint main(){ int arr[] = { 1, 2, 3, 4, 5 }; int N = sizeof(arr) / sizeof(arr[0]); cout << cntMaxOperationToMakeN_1_0(arr, N); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{ // Function to count maximum number of steps// to make (N - 1) array elements to 0static int cntMaxOperationToMakeN_1_0(int[] arr, int N){ // Stores maximum count of steps to make // (N - 1) elements equal to 0 int cntOp = 0; // Stores array elements PriorityQueue<Integer> PQ = new PriorityQueue<Integer>((a, b) -> b - a); // Traverse the array for(int i = 0; i < N; i++) { // Insert arr[i] into PQ PQ.add(arr[i]); } // Extract top 2 elements from the array // while (N - 1) array elements become 0 while (PQ.size() > 1) { // Stores top element // of PQ int X = PQ.peek(); // Pop the top element // of PQ. PQ.remove(); // Stores top element // of PQ int Y = PQ.peek(); // Pop the top element // of PQ. PQ.remove(); // Update X X--; // Update Y Y--; // If X is not equal to 0 if (X != 0) { // Insert X into PQ PQ.add(X); } // if Y is not equal // to 0 if (Y != 0) { // Insert Y // into PQ PQ.add(Y); } // Update cntOp cntOp += 1; } return cntOp;}// Driver codepublic static void main(String[] args){ int[] arr = { 1, 2, 3, 4, 5 }; int N = arr.length; System.out.print(cntMaxOperationToMakeN_1_0(arr, N));}}// This code is contributed by susmitakundugoaldanga |
Python3
# Python3 program to implement# the above approach# Function to count maximum number of steps# to make (N - 1) array elements to 0def cntMaxOperationToMakeN_1_0(arr, N): # Stores maximum count of steps to make # (N - 1) elements equal to 0 cntOp = 0 # Stores array elements PQ = [] # Traverse the array for i in range(N): # Insert arr[i] into PQ PQ.append(arr[i]) PQ = sorted(PQ) # Extract top 2 elements from the array # while (N - 1) array elements become 0 while (len(PQ) > 1): # Stores top element # of PQ X = PQ[-1] # Pop the top element # of PQ. del PQ[-1] # Stores top element # of PQ Y = PQ[-1] # Pop the top element # of PQ. del PQ[-1] # Update X X -= 1 # Update Y Y -= 1 # If X is not equal to 0 if (X != 0): # Insert X into PQ PQ.append(X) # if Y is not equal # to 0 if (Y != 0): # Insert Y # into PQ PQ.append(Y) # Update cntOp cntOp += 1 PQ = sorted(PQ) return cntOp# Driver Codeif __name__ == '__main__': arr = [1, 2, 3, 4, 5] N = len(arr) print (cntMaxOperationToMakeN_1_0(arr, N)) # This code is contributed by mohit kumar 29. |
C#
// C# program to implement// the above approachusing System;using System.Collections.Generic;class GFG { // Function to count maximum number of steps // to make (N - 1) array elements to 0 static int cntMaxOperationToMakeN_1_0(int[] arr, int N) { // Stores maximum count of steps to make // (N - 1) elements equal to 0 int cntOp = 0; // Stores array elements List<int> PQ = new List<int>(); // Traverse the array for (int i = 0; i < N; i++) { // Insert arr[i] into PQ PQ.Add(arr[i]); } PQ.Sort(); PQ.Reverse(); // Extract top 2 elements from the array // while (N - 1) array elements become 0 while (PQ.Count > 1) { // Stores top element // of PQ int X = PQ[0]; // Pop the top element // of PQ. PQ.RemoveAt(0); // Stores top element // of PQ int Y = PQ[0]; // Pop the top element // of PQ. PQ.RemoveAt(0); // Update X X--; // Update Y Y--; // If X is not equal to 0 if (X != 0) { // Insert X into PQ PQ.Add(X); PQ.Sort(); PQ.Reverse(); } // if Y is not equal // to 0 if (Y != 0) { // Insert Y // into PQ PQ.Add(Y); PQ.Sort(); PQ.Reverse(); } // Update cntOp cntOp += 1; } return cntOp; } // Driver code static void Main() { int[] arr = { 1, 2, 3, 4, 5 }; int N = arr.Length; Console.WriteLine(cntMaxOperationToMakeN_1_0(arr, N)); }}// This code is contributed by divyesh072019 |
Javascript
<script> // Javascript program to implement the above approach // Function to count maximum number of steps // to make (N - 1) array elements to 0 function cntMaxOperationToMakeN_1_0(arr, N) { // Stores maximum count of steps to make // (N - 1) elements equal to 0 let cntOp = 0; // Stores array elements let PQ = []; // Traverse the array for (let i = 0; i < N; i++) { // Insert arr[i] into PQ PQ.push(arr[i]); } PQ.sort(function(a, b){return a - b}); PQ.reverse(); // Extract top 2 elements from the array // while (N - 1) array elements become 0 while (PQ.length > 1) { // Stores top element // of PQ let X = PQ[0]; // Pop the top element // of PQ. PQ.shift(); // Stores top element // of PQ let Y = PQ[0]; // Pop the top element // of PQ. PQ.shift(); // Update X X--; // Update Y Y--; // If X is not equal to 0 if (X != 0) { // Insert X into PQ PQ.push(X); PQ.sort(function(a, b){return a - b}); PQ.reverse(); } // if Y is not equal // to 0 if (Y != 0) { // Insert Y // into PQ PQ.push(Y); PQ.sort(function(a, b){return a - b}); PQ.reverse(); } // Update cntOp cntOp += 1; } return cntOp; } let arr = [ 1, 2, 3, 4, 5 ]; let N = arr.length; document.write(cntMaxOperationToMakeN_1_0(arr, N)); // This code is contributed by mukesh07.</script> |
7
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


