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[2], arr[] = {4, 0, 4, 6}, in 2 operations.
Remove 2 from arr[3], 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[0], 1 from arr[1] and 2 from arr[2]. 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++
#include <bits/stdc++.h>
using namespace std;
int minimumRemoval( int arr[], int n)
{
int total_sum = 0;
int minimum_operation = INT_MAX;
sort(arr, arr + n);
for ( int index = 0; index < n; index++) {
total_sum += arr[index];
}
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;
}
int main()
{
int arr[4] = { 4, 1, 6, 6 };
int N = 4;
cout << minimumRemoval(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minimumRemoval( int arr[], int n)
{
int total_sum = 0 ;
int minimum_operation = Integer.MAX_VALUE;
Arrays.sort(arr);
for ( int index = 0 ; index < n; index++) {
total_sum += arr[index];
}
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;
}
public static void main(String[] args)
{
int arr[] = { 4 , 1 , 6 , 6 };
int N = 4 ;
System.out.print(minimumRemoval(arr, N));
}
}
|
Python3
import sys
def minimumRemoval(arr, n) :
total_sum = 0
minimum_operation = sys.maxsize
arr.sort()
for index in range ( 0 , n) :
total_sum + = arr[index]
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
arr = [ 4 , 1 , 6 , 6 ]
N = 4
print (minimumRemoval(arr, N))
|
C#
using System;
class GFG {
static int minimumRemoval( int [] arr, int n)
{
int total_sum = 0;
int minimum_operation = Int32.MaxValue;
Array.Sort(arr);
for ( int index = 0; index < n; index++) {
total_sum += arr[index];
}
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;
}
public static void Main()
{
int [] arr = { 4, 1, 6, 6 };
int N = 4;
Console.Write(minimumRemoval(arr, N));
}
}
|
Javascript
<script>
const INT_MAX = 2147483647;
const minimumRemoval = (arr, n) => {
let total_sum = 0;
let minimum_operation = INT_MAX;
arr.sort();
for (let index = 0; index < n; index++) {
total_sum += arr[index];
}
for (let index = 0; index < n; index++) {
let curr_operation
= total_sum
- ((n - index) * arr[index]);
if (curr_operation < minimum_operation) {
minimum_operation = curr_operation;
}
}
return minimum_operation;
}
let arr = [4, 1, 6, 6];
let N = 4;
document.write(minimumRemoval(arr, N));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...