# Minimize decrements to make each Array elements same or 0

• Last Updated : 29 Mar, 2022

Given an array arr[] consisting of N positive integers. In one operation any number of the array can be decremented by 1. The task is to find the minimum number of operations required to make all of the elements equal or 0.

Examples:

Input: arr[] = {4, 1, 6, 6}
Output: 5
Explanation: Remove 1 from arr{1}, array becomes, {4, 0, 6, 6}, makes 1 operation.
Remove 2 from arr, arr[] = {4, 0, 4, 6}, in 2 operations.
Remove 2 from arr, arr[] = {4, 0, 4, 4} in 2 operations.
So, Total operations = 1 + 2 + 2 = 5.

Input: arr = {1, 1, 2, 10}
Output: 4
Explanation: Remove 1 from arr, 1 from arr and 2 from arr. Total operations = 1 + 1 + 2 = 4

Approach: The problem can be solved with help of greedy approach using below idea:

• If each element of the array is taken as the value that must be equal to the rest of the array elements,
• Then total removal for any index i would be the (sum_array – (N – i)*arr[i])
• So the approach would be just to find the above value for each index and return the minimum among them.

Follow the steps mentioned below to solve the problem:

• Sort the given array.
• Calculate the sum of all the array elements and store it in a variable, say, total_sum.
• Create a variable minimum_operation = INT_MAX, run a loop and update minimum_operation  = min(minimum_operation, total_sum  – (N – i)*arr[i])
• Return minimum_operation as the answer.

Below is the implementation of the above approach.

## C++

 `// C++ code to implement the approach`   `#include ` `using` `namespace` `std;`   `// Function to find minimum operation` `int` `minimumRemoval(``int` `arr[], ``int` `n)` `{`   `    ``// Variable to store sum of array` `    ``int` `total_sum = 0;`   `    ``// Variable to store answer` `    ``int` `minimum_operation = INT_MAX;`   `    ``// Sort the given array` `    ``sort(arr, arr + n);`   `    ``for` `(``int` `index = 0; index < n; index++) {` `        ``total_sum += arr[index];` `    ``}`   `    ``// Find minimum operation by keeping` `    ``// each array  value as the remaining` `    ``// elements value` `    ``for` `(``int` `index = 0; index < n; index++) {`   `        ``int` `curr_operation` `            ``= total_sum` `              ``- ((n - index) * arr[index]);`   `        ``if` `(curr_operation < minimum_operation) {` `            ``minimum_operation = curr_operation;` `        ``}` `    ``}`   `    ``// Return minimum_operation` `    ``return` `minimum_operation;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr = { 4, 1, 6, 6 };` `    ``int` `N = 4;`   `    ``// Function call` `    ``cout << minimumRemoval(arr, N);` `    ``return` `0;` `}`

## Java

 `// Java code to implement the approach` `import` `java.util.*;`   `class` `GFG{`   `// Function to find minimum operation` `static` `int` `minimumRemoval(``int` `arr[], ``int` `n)` `{`   `    ``// Variable to store sum of array` `    ``int` `total_sum = ``0``;`   `    ``// Variable to store answer` `    ``int` `minimum_operation = Integer.MAX_VALUE;`   `    ``// Sort the given array` `    ``Arrays.sort(arr);`   `    ``for` `(``int` `index = ``0``; index < n; index++) {` `        ``total_sum += arr[index];` `    ``}`   `    ``// Find minimum operation by keeping` `    ``// each array  value as the remaining` `    ``// elements value` `    ``for` `(``int` `index = ``0``; index < n; index++) {`   `        ``int` `curr_operation` `            ``= total_sum` `              ``- ((n - index) * arr[index]);`   `        ``if` `(curr_operation < minimum_operation) {` `            ``minimum_operation = curr_operation;` `        ``}` `    ``}`   `    ``// Return minimum_operation` `    ``return` `minimum_operation;` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `arr[] = { ``4``, ``1``, ``6``, ``6` `};` `    ``int` `N = ``4``;`   `    ``// Function call` `    ``System.out.print(minimumRemoval(arr, N));` `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# Python code to implement the approach` `import` `sys`   `# Function to find minimum operation` `def` `minimumRemoval(arr, n) :`   `    ``# Variable to store sum of array` `    ``total_sum ``=` `0`   `    ``# Variable to store answer` `    ``minimum_operation ``=` `sys.maxsize`   `    ``# Sort the given array` `    ``arr.sort()`   `    ``for` `index ``in` `range``(``0``, n) :` `        ``total_sum ``+``=` `arr[index]` `    `    `    ``# Find minimum operation by keeping` `    ``# each array value as the remaining` `    ``# elements value` `    ``for` `index ``in` `range``(``0``, n) :`   `        ``curr_operation ``=` `(total_sum` `            ``-` `((n ``-` `index) ``*` `arr[index]))`   `        ``if` `(curr_operation < minimum_operation) :` `            ``minimum_operation ``=` `curr_operation` `        `  `    `    `    ``# Return minimum_operation` `    ``return` `minimum_operation`     `# Driver code`   `arr ``=` `[ ``4``, ``1``, ``6``, ``6` `]` `N ``=` `4`   `# Function call` `print``(minimumRemoval(arr, N))`   `# This code is contributed by code_hunt.`

## C#

 `// C# code to implement the approach` `using` `System;` `class` `GFG {`   `  ``// Function to find minimum operation` `  ``static` `int` `minimumRemoval(``int``[] arr, ``int` `n)` `  ``{`   `    ``// Variable to store sum of array` `    ``int` `total_sum = 0;`   `    ``// Variable to store answer` `    ``int` `minimum_operation = Int32.MaxValue;`   `    ``// Sort the given array` `    ``Array.Sort(arr);`   `    ``for` `(``int` `index = 0; index < n; index++) {` `      ``total_sum += arr[index];` `    ``}`   `    ``// Find minimum operation by keeping` `    ``// each array  value as the remaining` `    ``// elements value` `    ``for` `(``int` `index = 0; index < n; index++) {`   `      ``int` `curr_operation` `        ``= total_sum - ((n - index) * arr[index]);`   `      ``if` `(curr_operation < minimum_operation) {` `        ``minimum_operation = curr_operation;` `      ``}` `    ``}`   `    ``// Return minimum_operation` `    ``return` `minimum_operation;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main()` `  ``{` `    ``int``[] arr = { 4, 1, 6, 6 };` `    ``int` `N = 4;`   `    ``// Function call` `    ``Console.Write(minimumRemoval(arr, N));` `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`5`

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :