Count of elements which is product of a pair or an element square

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(N3)



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:

  1. Sort the given array in increasing order.
  2. 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.
  3. Sorting the array allows us to perform Binary Search while searching for factors, thus, reducing the computational complexity to O(logN).
  4. If any such pair is found, increase the count and move to the next element and repeat the same process.
  5. Print the final count of all such numbers in the array.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

3

Time Complexity: O(N3/2*log N)
Auxillary Space: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.