Minimum sum possible by removing all occurrences of any array element
Last Updated :
15 Jun, 2021
Given an array arr[] consisting of N integers, the task is to find the minimum possible sum of the array by removing all occurrences of any single array element.
Examples:
Input: N = 4, arr[] = {4, 5, 6, 6}
Output: 9
Explanation:
All distinct array elements are {4, 5, 6}.
Removing all occurrences of 4 modifies arr[] to {5, 6, 6}
Sum of the array = 17.
Removing all occurrences of 5 modifies arr[] to {4, 6, 6}
Sum of the array = 16.
Removing all occurrences of 6 modifies arr[] to {4, 5}
Sum of the array = 9.
Therefore, the minimum sum possible is 9, which is attained by deleting all occurrences of 6.
Input: N = 3, arr[] = {2, 2, 2}
Output: 0
Approach: The idea to solve this problem is to first find the frequency of each element in the array and the sum of the array. Then for each unique element, find the minimum sum by finding the difference between the sum and product of the array element and its frequency.
Follow the steps below to solve the problem:
- Initialize a map, say mp, to store the frequency of array elements and a variable, say minSum, to store the minimum sum obtained after removing all occurrences of any array element.
- Traverse the array arr[] to count the frequency of each array element and store it in a Map and calculate the sum of all array elements and store it in sum.
- Traverse the map and for each key-value pair, perform the following operations:
- Subtract the product of the element and its occurrences from the sum and store the minimum sum obtained in minSum.
- Return minSum as the minimum sum obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minSum( int A[], int N)
{
map< int , int > mp;
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += A[i];
mp[A[i]]++;
}
int minSum = INT_MAX;
for ( auto it : mp) {
minSum = min(
minSum, sum - (it.first * it.second));
}
return minSum;
}
int main()
{
int arr[] = { 4, 5, 6, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minSum(arr, N) << "\n" ;
}
|
Java
import java.util.*;
class GFG
{
static int minSum( int A[], int N)
{
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += A[i];
if (mp.containsKey(A[i]))
{
mp.put(A[i], mp.get(A[i]) + 1 );
}
else
{
mp.put(A[i], 1 );
}
}
int minSum = Integer.MAX_VALUE;
for (Map.Entry<Integer,Integer> it : mp.entrySet())
{
minSum = Math.min(
minSum, sum - (it.getKey() * it.getValue()));
}
return minSum;
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 6 , 6 };
int N = arr.length;
System.out.print(minSum(arr, N)+ "\n" );
}
}
|
Python3
def minSum(A, N):
mp = {}
sum = 0
for i in range (N):
sum + = A[i]
if A[i] in mp:
mp[A[i]] + = 1
else :
mp[A[i]] = 1
minSum = float ( 'inf' )
for it in mp:
minSum = min (minSum, sum - (it * mp[it]))
return minSum
arr = [ 4 , 5 , 6 , 6 ]
N = len (arr)
print (minSum(arr, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int minSum( int []A, int N)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += A[i];
if (mp.ContainsKey(A[i]))
{
mp[A[i]] = mp[A[i]] + 1;
}
else
{
mp.Add(A[i], 1);
}
}
int minSum = int .MaxValue;
foreach (KeyValuePair< int , int > it in mp)
{
minSum = Math.Min(
minSum, sum - (it.Key * it.Value));
}
return minSum;
}
public static void Main(String[] args)
{
int []arr = { 4, 5, 6, 6 };
int N = arr.Length;
Console.Write(minSum(arr, N)+ "\n" );
}
}
|
Javascript
<script>
function minSum(A, N)
{
let mp = new Map();
let sum = 0;
for (let i = 0; i < N; i++)
{
sum += A[i];
mp[A[i]]++;
if (mp.has(A[i]))
{
mp.set(A[i], mp.get(A[i]) + 1)
}
else
{
mp.set(A[i], 1)
}
}
let minSum = Number.MAX_SAFE_INTEGER;
for (let it of mp)
{
minSum = Math.min(minSum,
sum - (it[0] * it[1]));
}
return minSum;
}
let arr = [ 4, 5, 6, 6 ];
let N = arr.length
document.write(minSum(arr, N) + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...