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++
#include <bits/stdc++.h>
using namespace std;
int sumOfElements( int arr[], int n)
{
bool prime[n + 1];
int i, j;
unordered_map< int , int > m;
for (i = 0; i < n; i++)
m[arr[i]]++;
int sum = 0;
for ( auto it = m.begin(); it != m.end(); it++) {
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);
return 0;
}
|
Java
import java.util.*;
class Solution
{
static int sumOfElements( int arr[], int n)
{
boolean prime[] = new boolean [n + 1 ];
int i, j;
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 ;
Iterator hmIterator = m.entrySet().iterator();
while (hmIterator.hasNext()) {
Map.Entry mapElement = (Map.Entry)hmIterator.next();
if ((( int )mapElement.getValue()) >= (( int )mapElement.getKey())) {
sum += (( int )mapElement.getKey());
}
}
return sum;
}
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));
}
}
|
Python3
def sumOfElements(arr, n) :
m = dict .fromkeys(arr, 0 )
for i in range (n) :
m[arr[i]] + = 1
sum = 0
for key,value in m.items() :
if value > = key :
sum + = key
return sum
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 3 , 2 , 3 , 2 , 3 , 3 ]
n = len (arr)
print (sumOfElements(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int sumOfElements( int []arr, int n)
{
bool []prime = new bool [n + 1];
int i;
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;
foreach (KeyValuePair< int , int > entry in m)
{
if (entry.Value >= entry.Key)
{
sum+=entry.Key;
}
}
return sum;
}
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));
}
}
|
Javascript
<script>
function sumOfElements(arr, n) {
let prime = new Array(n + 1);
let i, j;
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;
for (let it of m) {
if ((it[1]) >= (it[0])) {
sum += (it[0]);
}
}
return sum;
}
let arr = [1, 2, 3, 3, 2, 3, 2, 3, 3];
let n = arr.length;
document.write(sumOfElements(arr, n));
</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< 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;
for ( auto it = m.begin(); it != m.end(); ++it){
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
import java.util.HashMap;
class Main {
public static int sumOfElements( int [] arr, int n)
{
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 ;
for (Integer key : m.keySet()) {
if (m.get(key) >= key) {
sum += key;
}
}
return sum;
}
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));
}
}
|
Python3
from collections import Counter
def sumOfElements(arr, n):
m = Counter(arr)
sum = 0
for key, value in m.items():
if value > = key:
sum + = key
return sum
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 3 , 2 , 3 , 2 , 3 , 3 ]
n = len (arr)
print (sumOfElements(arr, n))
|
C#
using System;
using System.Collections.Generic;
class MainClass
{
public static int SumOfElements( int [] arr, int n)
{
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;
foreach (KeyValuePair< int , int > kvp in m) {
if (kvp.Value >= kvp.Key) {
sum += kvp.Key;
}
}
return sum;
}
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){
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;
for (let [key, value] of m){
if (value >= key){
sum += key;
}
}
return sum;
}
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)
Share your thoughts in the comments
Please Login to comment...