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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum sum after deletion
int minSum(int A[], int N)
{
// Stores frequency of
// array elements
map<int, int> mp;
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Calculate sum
sum += A[i];
// Update frequency of
// the current element
mp[A[i]]++;
}
// Stores the minimum
// sum required
int minSum = INT_MAX;
// Traverse map
for (auto it : mp) {
// Find the minimum sum obtained
minSum = min(
minSum, sum - (it.first * it.second));
}
// Return minimum sum
return minSum;
}
// Driver code
int main()
{
// Input array
int arr[] = { 4, 5, 6, 6 };
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
cout << minSum(arr, N) << "\n";
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find minimum sum after deletion
static int minSum(int A[], int N)
{
// Stores frequency of
// array elements
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// Calculate sum
sum += A[i];
// Update frequency of
// the current element
if(mp.containsKey(A[i]))
{
mp.put(A[i], mp.get(A[i]) + 1);
}
else
{
mp.put(A[i], 1);
}
}
// Stores the minimum
// sum required
int minSum = Integer.MAX_VALUE;
// Traverse map
for (Map.Entry<Integer,Integer> it : mp.entrySet())
{
// Find the minimum sum obtained
minSum = Math.min(
minSum, sum - (it.getKey() * it.getValue()));
}
// Return minimum sum
return minSum;
}
// Driver code
public static void main(String[] args)
{
// Input array
int arr[] = { 4, 5, 6, 6 };
// Size of array
int N = arr.length;
System.out.print(minSum(arr, N)+ "\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find minimum sum after deletion
def minSum(A, N):
# Stores frequency of
# array elements
mp = {}
sum = 0
# Traverse the array
for i in range(N):
# Calculate sum
sum += A[i]
# Update frequency of
# the current element
if A[i] in mp:
mp[A[i]] += 1
else:
mp[A[i]] = 1
# Stores the minimum
# sum required
minSum = float('inf')
# Traverse map
for it in mp:
# Find the minimum sum obtained
minSum = min(minSum, sum - (it * mp[it]))
# Return minimum sum
return minSum
# Driver code
# Input array
arr = [ 4, 5, 6, 6 ]
# Size of array
N = len(arr)
print(minSum(arr, N))
# This code is contributed by rohitsingh07052.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find minimum sum after deletion
static int minSum(int []A, int N)
{
// Stores frequency of
// array elements
Dictionary<int,int> mp = new Dictionary<int,int>();
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// Calculate sum
sum += A[i];
// Update frequency of
// the current element
if(mp.ContainsKey(A[i]))
{
mp[A[i]] = mp[A[i]] + 1;
}
else
{
mp.Add(A[i], 1);
}
}
// Stores the minimum
// sum required
int minSum = int.MaxValue;
// Traverse map
foreach (KeyValuePair<int,int> it in mp)
{
// Find the minimum sum obtained
minSum = Math.Min(
minSum, sum - (it.Key * it.Value));
}
// Return minimum sum
return minSum;
}
// Driver code
public static void Main(String[] args)
{
// Input array
int []arr = { 4, 5, 6, 6 };
// Size of array
int N = arr.Length;
Console.Write(minSum(arr, N)+ "\n");
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program for the above approach
// Function to find minimum sum after deletion
function minSum(A, N)
{
// Stores frequency of
// array elements
let mp = new Map();
let sum = 0;
// Traverse the array
for(let i = 0; i < N; i++)
{
// Calculate sum
sum += A[i];
// Update frequency of
// the current element
mp[A[i]]++;
if (mp.has(A[i]))
{
mp.set(A[i], mp.get(A[i]) + 1)
}
else
{
mp.set(A[i], 1)
}
}
// Stores the minimum
// sum required
let minSum = Number.MAX_SAFE_INTEGER;
// Traverse map
for(let it of mp)
{
// Find the minimum sum obtained
minSum = Math.min(minSum,
sum - (it[0] * it[1]));
}
// Return minimum sum
return minSum;
}
// Driver code
// Input array
let arr = [ 4, 5, 6, 6 ];
// Size of array
let N = arr.length
document.write(minSum(arr, N) + "<br>");
// This code is contributed by gfgking
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Remove all occurrences of any element for maximum array sum Given an array of positive integers, remove all the occurrences of the element to get the maximum sum of the remaining array. Examples: Input : arr = {1, 1, 3} Output : 3 On removing 1 from the array, we get {3}. The total value is 3 Input : arr = {1, 1, 3, 3, 2, 2, 1, 1, 1} Output : 11 On removing
6 min read
Remove All Occurrences of an Element in an Array Given an integer array arr[] and an integer ele the task is to the remove all occurrences of ele from arr[] in-place and return the number of elements which are not equal to ele. If there are k number of elements which are not equal to ele then the input array arr[] should be modified such that the
6 min read
Minimize sum of given array by removing all occurrences of a single digit Given an array arr[] of size N, the task is to minimize the sum by removing all the occurrences of a single digit. Examples: Input: arr[] = {34, 23, 85, 93}Output: 100Explanation: Removing the occurrences of the digit 3 from each element of the array modifies arr[] to {4, 2, 85, 9}. Therefore, minim
6 min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Minimum length of X[] after removing at most K sub-arrays Given an array X[] of length N and an integer K. Then your task is to output minimum length of X[] after removing at most K number of sub-arrays, which satisfies the following conditions: Size of the sub-array must be greater than 1.First and last element of sub-array must be same.Note: The sub-arra
10 min read
Find minimum possible size of array with given rules for removing elements Given an array of numbers and a constant k, minimize size of array with following rules for removing elements. Exactly three elements can be removed at one go.The removed three elements must be adjacent in array, i.e., arr[i], arr[i+1], arr[i+2]. And the second element must be k greater than first a
11 min read