Sum of elements in an array with frequencies greater than or equal to that element
Last Updated :
19 Apr, 2023
Given an array arr[] of N integers. The task is to find the sum of the elements which have frequencies greater than or equal to that element in the array.
Examples:
Input: arr[] = {2, 1, 1, 2, 1, 6}
Output: 3
The elements in the array are {2, 1, 6}
Where,
2 appear 2 times which is greater than equal to 2 itself.
1 appear 3 times which is greater than 1 itself.
But 6 appears 1 time which is not greater than or equals to 6.
So, sum = 2 + 1 = 3.
Input: arr[] = {1, 2, 3, 3, 2, 3, 2, 3, 3}
Output: 6
Approach:
- Traverse the array and store the frequencies of all the elements in an unordered_map in C++ or equivalent data structure in any other programming language.
- Calculate the sum of elements having frequencies greater than or equal to that element.
Below is the implementation of the above approach:
C++
// C++ program to find sum of elements
// in an array having frequency greater
// than or equal to that element
#include <bits/stdc++.h>
using namespace std;
// Function to return the sum of elements
// in an array having frequency greater
// than or equal to that element
int sumOfElements(int arr[], int n)
{
bool prime[n + 1];
int i, j;
// Map is used to store
// element frequencies
unordered_map<int, int> m;
for (i = 0; i < n; i++)
m[arr[i]]++;
int sum = 0;
// Traverse the map using iterators
for (auto it = m.begin(); it != m.end(); it++) {
// Calculate the sum of elements
// having frequencies greater than
// or equal to the element itself
if ((it->second) >= (it->first)) {
sum += (it->first);
}
}
return sum;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 3, 2, 3, 2, 3, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << sumOfElements(arr, n);
return 0;
}
Java
// Java program to find sum of elements
// in an array having frequency greater
// than or equal to that element
import java.util.*;
class Solution
{
// Function to return the sum of elements
// in an array having frequency greater
// than or equal to that element
static int sumOfElements(int arr[], int n)
{
boolean prime[] = new boolean[n + 1];
int i, j;
// Map is used to store
// element frequencies
HashMap<Integer, Integer> m= new HashMap<Integer,Integer>();
for (i = 0; i < n; i++)
{
if(m.get(arr[i])==null)
m.put(arr[i],1);
else
m.put(arr[i],m.get(arr[i])+1);
}
int sum = 0;
// Getting an iterator
Iterator hmIterator = m.entrySet().iterator();
// Traverse the map using iterators
while (hmIterator.hasNext()) {
Map.Entry mapElement = (Map.Entry)hmIterator.next();
// Calculate the sum of elements
// having frequencies greater than
// or equal to the element itself
if (((int)mapElement.getValue()) >= ((int)mapElement.getKey())) {
sum += ((int)mapElement.getKey());
}
}
return sum;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 2, 3, 3, 2, 3, 2, 3, 3 };
int n = arr.length;
System.out.println(sumOfElements(arr, n));
}
}
//contributed by Arnab Kundu
Python3
# Python3 program to find sum of elements
# in an array having frequency greater
# than or equal to that element
# Function to return the sum of elements
# in an array having frequency greater
# than or equal to that element
def sumOfElements(arr, n) :
# dictionary is used to store
# element frequencies
m = dict.fromkeys(arr, 0)
for i in range(n) :
m[arr[i]] += 1
sum = 0
# traverse the dictionary
for key,value in m.items() :
# Calculate the sum of elements
# having frequencies greater than
# or equal to the element itself
if value >= key :
sum += key
return sum
# Driver code
if __name__ == "__main__" :
arr = [1, 2, 3, 3, 2, 3, 2, 3, 3]
n = len(arr)
print(sumOfElements(arr, n))
# This code is contributed by Ryuga
C#
// C# program to find sum of elements
// in an array having frequency greater
// than or equal to that element
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the sum of elements
// in an array having frequency greater
// than or equal to that element
static int sumOfElements(int []arr, int n)
{
bool []prime = new bool[n + 1];
int i;
// Map is used to store
// element frequencies
Dictionary<int, int> m= new Dictionary<int,int>();
for (i = 0; i < n; i++)
{
if(!m.ContainsKey(arr[i]))
m.Add(arr[i],1);
else
{
var val = m[arr[i]];
m.Remove(arr[i]);
m.Add(arr[i], val + 1);
}
}
int sum = 0;
// Calculate the sum of elements
// having frequencies greater than
// or equal to the element itself
foreach(KeyValuePair<int, int> entry in m)
{
if(entry.Value >= entry.Key)
{
sum+=entry.Key;
}
}
return sum;
}
// Driver code
public static void Main(String []args)
{
int []arr = { 1, 2, 3, 3, 2, 3, 2, 3, 3 };
int n = arr.Length;
Console.WriteLine(sumOfElements(arr, n));
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// javascript program to find sum of elements
// in an array having frequency greater
// than or equal to that element
// Function to return the sum of elements
// in an array having frequency greater
// than or equal to that element
function sumOfElements(arr, n) {
let prime = new Array(n + 1);
let i, j;
// Map is used to store
// element frequencies
let m = new Map();
for (i = 0; i < n; i++) {
if (m.has(arr[i])) {
m.set(arr[i], m.get(arr[i]) + 1)
} else[
m.set(arr[i], 1)
]
}
let sum = 0;
// Traverse the map using iterators
for (let it of m) {
// Calculate the sum of elements
// having frequencies greater than
// or equal to the element itself
if ((it[1]) >= (it[0])) {
sum += (it[0]);
}
}
return sum;
}
// Driver code
let arr = [1, 2, 3, 3, 2, 3, 2, 3, 3];
let n = arr.length;
document.write(sumOfElements(arr, n));
// This code is contributed by gfgking.
</script>
Time complexity: O(n)
Auxiliary Space: O(n)
Method #2:Using Built in python functions:
Approach:
- Calculate the frequencies using Counter() function
- Calculate the sum of elements having frequencies greater than or equal to that element.
C++
#include <iostream>
#include <map>
using namespace std;
int sumOfElements(int arr[], int n){
// Map is used to calculate frequency of elements of array
map<int, int> m;
for(int i = 0; i < n; i++){
if(m.find(arr[i]) != m.end()){
m[arr[i]]++;
} else {
m[arr[i]] = 1;
}
}
int sum = 0;
// Traverse the map
for(auto it = m.begin(); it != m.end(); ++it){
// Calculate the sum of elements
// having frequencies greater than
// or equal to the element itself
if(it->second >= it->first){
sum += it->first;
}
}
return sum;
}
int main(){
int arr[] = {1, 2, 3, 3, 2, 3, 2, 3, 3};
int n = sizeof(arr)/sizeof(arr[0]);
cout << sumOfElements(arr, n) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.HashMap;
class Main {
// Function to return the sum of elements
// in an array having frequency greater
// than or equal to that element
public static int sumOfElements(int[] arr, int n)
{
// HashMap is used to calculate frequency of
// elements of array
HashMap<Integer, Integer> m
= new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
if (m.containsKey(arr[i])) {
m.put(arr[i], m.get(arr[i]) + 1);
}
else {
m.put(arr[i], 1);
}
}
int sum = 0;
// Traverse the HashMap
for (Integer key : m.keySet()) {
// Calculate the sum of elements
// having frequencies greater than
// or equal to the element itself
if (m.get(key) >= key) {
sum += key;
}
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 3, 2, 3, 2, 3, 3 };
int n = arr.length;
System.out.println(sumOfElements(arr, n));
}
}
// This code is contributed by phasing17
Python3
# Python program for the above approach
from collections import Counter
# Function to return the sum of elements
# in an array having frequency greater
# than or equal to that element
def sumOfElements(arr, n):
# Counter function is used to
# calculate frequency of elements of array
m = Counter(arr)
sum = 0
# traverse the dictionary
for key, value in m.items():
# Calculate the sum of elements
# having frequencies greater than
# or equal to the element itself
if value >= key:
sum += key
return sum
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 3, 2, 3, 2, 3, 3]
n = len(arr)
print(sumOfElements(arr, n))
# This code is contributed by vikkycirus
C#
using System;
using System.Collections.Generic;
class MainClass
{
// Function to return the sum of elements
// in an array having frequency greater
// than or equal to that element
public static int SumOfElements(int[] arr, int n)
{
// Dictionary is used to calculate frequency of
// elements of array
Dictionary<int, int> m = new Dictionary<int, int>();
for (int i = 0; i < n; i++) {
if (m.ContainsKey(arr[i])) {
m[arr[i]]++;
} else {
m.Add(arr[i], 1);
}
}
int sum = 0;
// Traverse the Dictionary
foreach (KeyValuePair<int, int> kvp in m) {
// Calculate the sum of elements
// having frequencies greater than
// or equal to the element itself
if (kvp.Value >= kvp.Key) {
sum += kvp.Key;
}
}
return sum;
}
// Driver code
public static void Main() {
int[] arr = { 1, 2, 3, 3, 2, 3, 2, 3, 3 };
int n = arr.Length;
Console.WriteLine(SumOfElements(arr, n));
}
}
JavaScript
function sumOfElements(arr, n){
// Map function is used to calculate frequency of elements of array
let m = new Map();
for(let i = 0; i < n; i++){
if(m.has(arr[i])){
m.set(arr[i], m.get(arr[i])+1);
} else {
m.set(arr[i], 1);
}
}
let sum = 0;
// traverse the Map
for(let [key, value] of m){
// Calculate the sum of elements
// having frequencies greater than
// or equal to the element itself
if(value >= key){
sum += key;
}
}
return sum;
}
// Driver code
let arr = [1, 2, 3, 3, 2, 3, 2, 3, 3];
let n = arr.length;
console.log(sumOfElements(arr, n));
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Smallest subarray having an element with frequency greater than that of other elements Given an array arr of positive integers, the task is to find the smallest length subarray of length more than 1 having an element occurring more times than any other element. Examples: Input: arr[] = {2, 3, 2, 4, 5} Output: 2 3 2 Explanation: The subarray {2, 3, 2} has an element 2 which occurs more
15+ min read
Find element in a sorted array whose frequency is greater than or equal to n/2. Given a sorted array of length n, find the number in array that appears more than or equal to n/2 times. It is given that such element always exists. Examples: Input : 2 3 3 4 Output : 3 Input : 3 4 5 5 5 Output : 5 Input : 1 1 1 2 3 Output : 1 To find that number, we traverse the array and check th
3 min read
Check if the array has an element which is equal to sum of all the remaining elements Given an array of N elements, the task is to check if the array has an element that is equal to the sum of all the remaining elements. Examples: Input: a[] = {5, 1, 2, 2} Output: Yes we can write 5=(1+2+2) Input: a[] = {2, 1, 2, 4, 3} Output: No Approach: Suppose that the total elements in the array
10 min read
Check if the sum of K least and most frequent array elements are equal or not Given an array arr[] consisting of N integers, the task is to check if the sum of K most frequent array elements and the sum of K least frequent array elements in the array arr[] are equal or not. If found to be true, then print Yes. Otherwise, print No. Examples: Input: arr[] = { 3, 2, 1, 2, 3, 3,
10 min read
Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum Given two unsorted arrays arr1[] and arr2[], the task is to find the sum of elements of arr1[] such that the number of elements less than or equal to them in arr2[] is maximum. Examples: Input: arr1[] = {1, 2, 3, 4, 7, 9}, arr2[] = {0, 1, 2, 1, 1, 4} Output: 20 Below table shows the count of element
15 min read