- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Count pairs in an array such that frequency of one is at least value of other in C++

We are given an array of positive integers. The goal is to find the count of pairs of elements of arr[] such that pairs have elements ( A, B ) where frequency of A is B times and frequency of B is A.

Let us understand with examples.

**Input** − int arr[] = { 3, 3, 3, 5, 5, 6, 6}

**Output** − Count of pairs in an array such that frequency of one is at least value of other are − 1

**Explanation** − The valid pairs in an array where A occurs B times and B occurs A times are (3, 3) as 3 is occurring 3 times in an array. So there is only one valid pair hence the count is 1.

**Input** − int arr[] = { 3, 3, 3, 3, 3, 5, 5, 5, 6, 6}

**Output** − Count of pairs in an array such that frequency of one is at least value of other are − 1

**Explanation** − The valid pairs in an array where A occurs B times and B occurs A times are (3, 3), (5, 5) and (3, 5) as 3 is occurring 5 times and 5 is occurring 3 times in an array. So there are three valid pairs hence the count is 3.

## Approach used in the below program is as follows

In the approach we would first create and populate an unordered map containing frequencies of elements of the array. Traverse the unordered_map using for loop. For each element and its frequency, if any frequency is found more, increment count of pairs.

Take an array arr[] of integers.

Function frequency_other_value(int arr[], int size) takes the array and its size and returns the count of pairs such that pairs are (A,B) where A occurs at-least B times and vice versa.

Take the initial count as 0.

Take unordered_map<int, int> um for elements of arr[] and their frequencies.

Traverse map using for and for each first value of pair start=it.second; (it=iterator), traverse map for frequencies >= start using for loop.

Increment count for such pairs.

Return count as result.

## Example

#include <bits/stdc++.h> using namespace std; int frequency_other_value(int arr[], int len){ int count = 0; unordered_map<int, int> um; for (int i = 0; i < len; ++i){ um[arr[i]]++; } for (auto it : um){ int start = it.first; int end = it.second; for (int j = 1; j <= end; j++){ if (um[j] >= start){ count++; } } } return count; } int main(){ int arr[] = { 3, 3, 3, 5, 5, 6, 6}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of pairs in an array such that frequency of one is at least value of other are: "<<frequency_other_value(arr, size); return 0; }

## Output

If we run the above code it will generate the following output −

Count of pairs in an array such that frequency of one is at least value of other are: 1

- Related Articles
- Count pairs in an array such that at least one element is prime in C++
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Print array elements that are divisible by at-least one other in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Count elements that are divisible by at-least one element in another array in C++
- Count pairs in an array such that both elements has equal set bits in C++
- Find document in MongoDB where at least one item from an array is not in the other?
- Count of pairs in an array that have consecutive numbers using JavaScript
- Find number of pairs in an array such that their XOR is 0 using C++.
- Count divisors of n that have at-least one digit common with n in Java
- Count pairs of parentheses sequences such that parentheses are balanced in C++
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count Triplets such that one of the numbers can be written as sum of the other two in C++