Given an array of positive integers, remove all the occurrences of element to get the maximum sum of the remaining array
Examples:
Input : arr = {1, 1, 3}
Output : 3
On removing 1 from array we get {3} total value is 3Input : arr = {1, 1, 3, 3, 2, 2, 1, 1, 1}
Output : 11
On removing 2 from array we get {1, 1, 3, 3, 1, 1, 1} total value is 11
Brute Force solution is to first find the sum of array after that find all the frequencies of element in the array. Find the value contributed by them to array sum. Select the minimum value among them. To get maximum sum of array after removing is equal difference of total value of sum and minimum value contributed by individual element total frequent value.
Time complexity: O(n2)
A better approach We first find the total sum of array and then sort the array, count the individual frequencies while traversing the array and get maximum value. After sorting, we can frequencies of all elements in O(n) tine,
Time complexity of this approach is O(n Log n)
An Efficient Approach is to use hashing to count the frequencies of elements while traversing the array.Find the minimum value using the frequencies stored in array
C++
#include <bits/stdc++.h> using namespace std; int maxSumArray(int arr[], int n) { // Find total sum and frequencies of elements int sum = 0; unordered_map<int, int> mp; for (int i = 0; i < n; i++) { sum += arr[i]; mp[arr[i]]++; } // Find minimum value to be subtracted. int minimum = INT_MAX; for (auto x : mp) minimum = min(minimum, x.second * x.first); // Find maximum sum after removal return (sum - minimum); } // Drivers code int main() { int arr[] = { 1, 1, 3, 3, 2, 2, 1, 1, 1 }; int n = sizeof(arr) / sizeof(int); cout << maxSumArray(arr, n); return 0; } |
Java
// Java program to convert fractional decimal // to binary number import java.util.*; class GFG { static int maxSumArray(int arr[], int n) { // Find total sum and frequencies of elements int sum = 0; Map<Integer,Integer> m = new HashMap<>(); for (int i = 0 ; i < n; i++) { sum += arr[i]; if(m.containsKey(arr[i])) { m.put(arr[i], m.get(arr[i])+1); } else { m.put(arr[i], 1); } } // Find minimum value to be subtracted. int minimum = Integer.MAX_VALUE; for (Map.Entry<Integer,Integer> x : m.entrySet()) minimum = Math.min(minimum, x.getValue() * x.getKey()); // Find maximum sum after removal return (sum - minimum); } // Drivers code public static void main(String[] args) { int arr[] = { 1, 1, 3, 3, 2, 2, 1, 1, 1 }; int n = arr.length; System.out.println(maxSumArray(arr, n)); } } // This code contributed by Rajput-Ji |
Python3
# Python3 program to convert # fractional decimal to binary number from sys import maxsize def maxSumArray(arr, n): # Find total sum and frequencies of elements sum1 = 0 mp = {i:0 for i in range(4)} for i in range(n): sum1 += arr[i] mp[arr[i]] += 1 # Find minimum value to be subtracted. minimum = maxsize for key, value in mp.items(): if(key == 0): continue minimum = min(minimum, value * key) # Find maximum sum after removal return (sum1 - minimum) # Driver Code if __name__ =='__main__': arr = [1, 1, 3, 3, 2, 2, 1, 1, 1] n = len(arr) print(maxSumArray(arr, n)) # This code is contributed by # Surendra_Gangwar |
C#
// C# program to convert fractional decimal // to binary number using System; using System.Collections.Generic; class GFG { static int maxSumArray(int []arr, int n) { // Find total sum and frequencies of elements int sum = 0; Dictionary<int,int> m = new Dictionary<int,int>(); for (int i = 0 ; i < n; i++) { sum += arr[i]; if(m.ContainsKey(arr[i])) { var val = m[arr[i]]; m.Remove(arr[i]); m.Add(arr[i], val + 1); } else { m.Add(arr[i], 1); } } // Find minimum value to be subtracted. int minimum = int.MaxValue; foreach(KeyValuePair<int, int> x in m) minimum = Math.Min(minimum, (x.Value * x.Key)); // Find maximum sum after removal return (sum - minimum); } // Driver code public static void Main(String[] args) { int []arr = { 1, 1, 3, 3, 2, 2, 1, 1, 1 }; int n = arr.Length; Console.WriteLine(maxSumArray(arr, n)); } } // This code is contributed by 29AjayKumar |
11
Time complexity: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Remove all occurrences of a character in a string
- Two player game in which a player can remove all occurrences of a number
- Maximum distance between two occurrences of same element in array
- Longest substring between any pair of occurrences ōf similar characters
- Array sum after replacing all occurrences of X by Y for Q queries
- Move all occurrences of an element to end in a linked list
- Smallest subarray with all occurrences of a most frequent element
- Remove duplicates from string keeping the order according to last occurrences
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P
- Sum of all distances between occurrences of same characters in a given string
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Check if each element of an Array is the Sum of any two elements of another Array
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Reduce array to a single element by repeatedly removing an element from any increasing pair
- Queries to find the XOR of an Array after replacing all occurrences of X by Y
- Maximum element in an array such that its previous and next element product is maximum
- Maximum number of multiples in an array before any element
- Minimum distance between two occurrences of maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

