Given an array **arr[]** of **N** positive integers, the task is to count the number of array elements which can be expressed as the product of two distinct array elements or as a square of any array element.

**Examples:**

Input:N = 5, arr[] = {3, 2, 6, 18, 4}

Output:3

Explanation:

3 elements from the given array 6, 18 and 4 can be expressed as {2 * 3, 6 * 3, 2 * 2} respectively.

Input:N = 6, arr[] = {5, 15, 3, 7, 10, 17}

Output:1

Explanation:

Only 15 can be expressed as 5 * 3.

**Naive Approach:**

The simpest approach is to traverse the array in the range **[0, N-1]** and for each number having index value **i** within the given range, run a nested loop over the same array to find out the two values whose product is arr[i]. If a pair of numbers is found, print 1 else print 0 against the corresponding index.

**Time Complexity:** O(N^{3})

**Efficient Approach:**

To solve the problem, we need to find all factors of each element and for every element, check if any pair of factors of that element is present in the array or not. Follow the steps below to solve the problem:

- Sort the given array in increasing order.
- Now, traverse the array and for each element, find all pairs of factors of that element and check if any of the pairs exist in the array or not using Binary Search.
- Sorting the array allows us to perform Binary Search while searching for factors, thus, reducing the computational complexity to
*O(logN)*. - If any such pair is found, increase the
**count**and move to the next element and repeat the same process. - Print the final
**count**of all such numbers in the array.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement the ` `// above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Stores all factors a number ` `vector<` `int` `> v[100000]; ` ` ` `// Function to calculate and ` `// store in a vector ` `void` `div` `(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 2; i <= ` `sqrt` `(n); ` ` ` `i++) { ` ` ` ` ` `if` `(n % i == 0) { ` ` ` `v[n].push_back(i); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the count of ` `// array elements which are a ` `// product of two array elements ` `int` `prodof2elements(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `arr2[n]; ` ` ` ` ` `// Copy elements into a ` ` ` `// a duplicate array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `arr2[i] = arr[i]; ` ` ` `} ` ` ` ` ` `// Sort the duplicate array ` ` ` `sort(arr2, arr2 + n); ` ` ` ` ` `// Store the count of elements ` ` ` `int` `ans = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If the factors are not ` ` ` `// calculated already ` ` ` `if` `(v[arr[i]].size() == 0) ` ` ` `div` `(arr[i]); ` ` ` ` ` `// Traverse its factors ` ` ` `for` `(` `auto` `j : v[arr[i]]) { ` ` ` ` ` `// If a pair of ` ` ` `// factors is found ` ` ` `if` `(binary_search( ` ` ` `arr2, arr2 + n, j) ` ` ` `and binary_search( ` ` ` `arr2, arr2 + n, ` ` ` `arr[i] / j)) { ` ` ` `ans++; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 1, 8, 4, 32, 18 }; ` ` ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << prodof2elements(arr, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** *O(N ^{3/2}*log N)*

**Auxillary Space:**

*O(N)*

## Recommended Posts:

- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Rearrange an array to minimize sum of product of consecutive pair elements
- Count of pairs in an array whose product is a perfect square
- Count of subsequences whose product is a difference of square of two integers
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Count of elements which cannot form any pair whose sum is power of 2
- Replace every element of the array by product of all other elements
- Check if the array has an element which is equal to product of remaining elements
- Minimize count of array elements to be removed to maximize difference between any pair up to K
- Pair with given product | Set 1 (Find if any pair exists)
- Count arrays of length K whose product of elements is same as that of given array
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Count elements whose sum with K is greater than max element
- Count of triplets from the given Array such that sum of any two elements is the third element
- Count of greater elements for each element in the Array
- Maximum count of elements divisible on the left for any element
- Count of elements on the left which are divisible by current element
- Count of elements on the left which are divisible by current element | Set 2
- Count of only repeated element in a sorted array of consecutive elements
- Find the difference of count of equal elements on the right and the left for each element

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.