
- 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 (p, q) such that p occurs in array at least q times and q occurs at least p times 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 ( p, q ) where p occurs in array for at least q times and q occurs in array for at-least p times.
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 p occurs q times and q occurs p 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 p occurs q times and q occurs p 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
Input an array of integer elements and calculate the size of an array and pass the data to the function for further processing
Declare a temporary variable count to store the occurrences of p and q
Create a variable vec of type vector and um of type unordered_map
Start loop FOR from 0 till the size of an array
Inside the loop, set um[arr[i]] by 1 and check IF the um is 1 then push arr[i] in the vector
Start another loop FOR from 0 till the size of a vector and check IF um[vec[i] < vec[i] then continue, ELSE IF they are equal then increment the count by 1 ELSE increment the count by 1. Start another loop j FOR from j to vec[i] + 1 till um[vec[i]].
Inside the loop j check IF um[j] >= vec[i] then increment the count by 1
Return the count
Print the result.
Example
#include <bits/stdc++.h> using namespace std; int pair_count(int arr[], int len){ int count = 0; vector<int> vec; unordered_map<int, int> um; for (int i = 0; i < len; i++){ um[arr[i]]++; if (um[arr[i]] == 1){ vec.push_back(arr[i]); } } for (int i = 0; i < vec.size(); i++){ if (um[vec[i]] < vec[i]){ continue; } else if (um[vec[i]] == vec[i]){ count++;; } else{ count++; for (int j = vec[i] + 1; j <= um[vec[i]]; j++){ if (um[j] >= vec[i]){ count++; } } } } return count; } int main(){ int arr[] = { 1, 1, 1, 5, 5, 1}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of pairs (p, q) such that p occurs in array at least q times and q occurs at least p times are: "<<pair_count(arr, size); return 0; }
Output
If we run the above code it will generate the following output −
Count of pairs (p, q) such that p occurs in array at least q times and q occurs at least p times are: 1
- Related Articles
- $0.5 \times 0.05 \times \sqrt{q}=\sqrt{0.5 \times 0.05 \times p} ; \frac{p}{q}=?$
- Find $p$ and $q$ such that: $2p,\ 2p+q,\ p+4q,\ 35$ are in A.P.
- Given that \( \frac{4 p+9 q}{p}=\frac{5 q}{p-q} \) and \( p \) and \( q \) are both positive. The value of $\frac{p}{q}$ is
- Subtract \( 4 p^{2} q-3 p q+5 p q^{2}-8 p+7 q-10 \) from \( 18-3 p-11 q+5 p q-2 p q^{2}+5 p^{2} q \).
- In \( \Delta P Q R \), right-angled at \( Q, P Q=3 \mathrm{~cm} \) and \( P R=6 \mathrm{~cm} \). Determine \( \angle P \) and \( \angle R \).
- If $P = 2^3 \times 3^{10} \times 5$ and $Q = 2 \times 3 \times 7$, then find he LCM of P and Q.
- Solve \( 2 p^{2} q^{2}-3 p q+4,5+7 p q-3 p^{2} q^{2} \).
- If p, q are real and p≠q, then show that the roots of the equation $(p-q)x^2+5(p+q)x-2(p-q)=0$ are real and unequal.
- In the figure, \( O Q: P Q=3: 4 \) and perimeter of \( \Delta P O Q=60 \mathrm{~cm} \). Determine \( P Q, Q R \) and \( O P \)."\n
- If $p,\ q,\ r$ are in A.P., then show that $p^2( p+r),\ q^2( r+p),\ r^2( p+q)$ are also in A.P.
- If p is a prime number and q is a positive integer such that $p + q = 1696$. If p and q are co prime numbers and their LCM is 21879, then find p and q.
- Simplify: $\frac{(q+\frac{1}{p})^m(q-\frac{1}{p})^m}{(p+\frac{1}{q})^m(p-\frac{1}{q})^m}$
- Prove that $\sqrt{p} + \sqrt{q}$ is irrational, where $p$ and $q$ are primes.
- In the figure \( P O \perp Q 0 \). The tangents to the circle at \( P \) and \( Q \) intersect at a point \( T \). Prove that \( P Q \) and \( O T \) are right bisectors of each other."\n
- In \( \triangle A B C, \angle A \) is obtuse, \( P B \perp A C, \) and \( Q C \perp A B \). Prove that \( A B \times A Q=A C \times A P \).
