- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count pairs whose products exist in array in C++

We are given an array of integer type elements and the task is to form the pairs from the given array and calculate the product of elements in the pair and check whether the given product is present in the given array or not.

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

**Output** − Count of pairs whose products exist in same array are − 7

**Explanation** − The pairs that can be formed from the given array are: (6, 2), (6, 3), (6, 1), (6, 5), (6, 10), (2, 3), (2, 1), (2, 5), (2, 10), (3, 1), (3, 5), (3, 10), (1, 5), (1, 10), (5, 10). So the pairs with the given product value in the same array are (2, 3) as 6, (6, 1) as 6, (3, 1) as 3, (2, 5) as 10, (1, 5) as 5, (2, 1) as 2, (1, 10 ) as 10.

**Input** − int arr[] = {2, 4, 8, 5, 10}

**Output** − Count of pairs whose products exist in same array are − 2

**Explanation** − The pairs that can be formed from the given array are: (2, 4), (2, 8), (2, 5), (2, 10), (4, 8), (4, 5), (4, 10), (8, 5), (8, 10), (5, 10). So the pairs with the given product value in the same array are (2, 4) as 8, (5, 2) as 10.

## Approach used in the below program is as follows

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the naive approach.

Input an array of integer elements and calculate the size of an array and pass the data to the function

Declare a temporary variable count to store the count of pairs with the product value to be available in the given array.

Start loop FOR from i to 0 till the size of an array

Inside the loop, start another loop FOR from j to i + 1 till the size of an array

Inside the loop calculate the product as arr[i] * arr[j]

Start another loop FOR from k to 0 till the size of an array

Inside the K loop, check IF product = arr[k] then increment the count by 1

Return the count

Print result.

## Efficient approach

Input an array of integer elements and calculate the size of an array and pass the data to the function

Declare a temporary variable count to store the count of pairs with the product value to be available in the given array.

Create a variable of type STL set as pro

Start loop FOR from i to 0 till the size of an array

Inside the loop, insert arr[i] in the set variable pro

Start another loop FOR from i to 0 till the size of an array

Inside the loop, start another loop FOR from j to i + 1 till the size of an array

Set product as arr[i] * arr[j]

Check IF pro.find(product) != pro.end() then increment the count by 1

Return the count

Print the result.

## Example (naive approach)

#include <bits/stdc++.h> using namespace std; int product_pair(int arr[], int size){ int product = 1; int count = 0; for(int i = 0 ; i<size ; i++){ for(int j = i+1;j<size;j++){ product = arr[i] * arr[j]; for(int pro = 0 ; pro < size; pro++){ if(product == arr[pro]){ count++; } } } } return count; } int main(){ int arr[] = {6, 2, 3, 1, 5, 10}; int size = sizeof(arr)/sizeof(arr[0]); cout<<"Count of pairs whose products exist in same array are: "<<product_pair(arr,size); return 0; }

## Output

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

Count of pairs whose products exist in same array are: 7

## Example (Efficient Approach)

#include<bits/stdc++.h> using namespace std; int product_pair(int arr[], int size){ set< int > pro; int count = 0; int product = 1; for (int i = 0 ; i < size; i++){ pro.insert(arr[i]); } for (int i = 0 ; i < size; i++){ for (int j = i + 1; j < size ; j++){ product = arr[i] * arr[j]; if(pro.find(product) != pro.end()){ count++; } } } return count; } int main(){ int arr[] = {6, 2, 3, 1, 5, 10}; int size = sizeof(arr)/sizeof(arr[0]); cout<<"Count of pairs whose products exist in same array are: "<<product_pair(arr,size); return 0; }

## Output

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

Count of pairs whose products exist in same array are: 7

- Related Articles
- Find pairs in array whose sums already exist in array in C++
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Count of pairs in an array whose sum is a perfect square in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count pairs in a sorted array whose product is less than k in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- Count divisible pairs in an array in C++
- Count pairs from two arrays whose modulo operation yields K in C++
- Count valid pairs in the array satisfying given conditions in C++
- Count pairs with average present in the same array in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Count of index pairs with equal elements in an array in C++
- Count passing car pairs in C++
- Count all pairs of an array which differ in K bits in C++