Count of elements which is product of a pair or an element square
Last Updated :
10 May, 2022
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 simplest 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:
- 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++
#include <bits/stdc++.h>
using namespace std;
vector< int > v[100000];
void div ( int n)
{
for ( int i = 2; i <= sqrt (n);
i++) {
if (n % i == 0) {
v[n].push_back(i);
}
}
}
int prodof2elements( int arr[], int n)
{
int arr2[n];
for ( int i = 0; i < n; i++) {
arr2[i] = arr[i];
}
sort(arr2, arr2 + n);
int ans = 0;
for ( int i = 0; i < n; i++) {
if (v[arr[i]].size() == 0)
div (arr[i]);
for ( auto j : v[arr[i]]) {
if (binary_search(
arr2, arr2 + n, j)
and binary_search(
arr2, arr2 + n,
arr[i] / j)) {
ans++;
break ;
}
}
}
return ans;
}
int main()
{
int arr[] = { 2, 1, 8, 4, 32, 18 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << prodof2elements(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer>[] v =
new Vector[ 100000 ];
static void div( int n)
{
for ( int i = 2 ;
i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
v[n].add(i);
}
}
}
static int prodof2elements( int arr[],
int n)
{
int []arr2 = new int [n];
for ( int i = 0 ; i < n; i++)
{
arr2[i] = arr[i];
}
Arrays.sort(arr2);
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (v[arr[i]].size() == 0 )
div(arr[i]);
for ( int j : v[arr[i]])
{
if (Arrays.binarySearch(arr2, j) >= 0 &&
Arrays.binarySearch(arr2,
( int )arr[i] / j) >= 0 )
{
ans++;
break ;
}
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 8 , 4 , 32 , 18 };
int N = arr.length;
for ( int i = 0 ; i < v.length; i++)
v[i] = new Vector<Integer>();
System.out.print(prodof2elements(arr, N));
}
}
|
Python3
import math
v = [[] for i in range ( 100000 )]
def div(n):
global v
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
if (n % i = = 0 ):
v[n].append(i)
def prodof2elements(arr, n):
arr2 = arr.copy()
arr2.sort()
ans = 0
for i in range (n):
if ( len (v[arr[i]]) = = 0 ):
div(arr[i])
for j in v[arr[i]]:
if j in arr2:
if int (arr[i] / j) in arr2:
ans + = 1
break
return ans
arr = [ 2 , 1 , 8 , 4 , 32 , 18 ]
N = len (arr)
print (prodof2elements(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int >[] v =
new List< int >[100000];
static void div( int n)
{
for ( int i = 2;
i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
v[n].Add(i);
}
}
}
static int prodof2elements( int []arr,
int n)
{
int []arr2 = new int [n];
for ( int i = 0; i < n; i++)
{
arr2[i] = arr[i];
}
Array.Sort(arr2);
int ans = 0;
for ( int i = 0; i < n; i++)
{
if (v[arr[i]].Count == 0)
div(arr[i]);
foreach ( int j in v[arr[i]])
{
if (Array.BinarySearch(arr2, j) >= 0 &&
Array.BinarySearch(arr2,
( int )arr[i] / j) >= 0)
{
ans++;
break ;
}
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = {2, 1, 8, 4, 32, 18};
int N = arr.Length;
for ( int i = 0; i < v.Length; i++)
v[i] = new List< int >();
Console.Write(prodof2elements(arr, N));
}
}
|
Javascript
<script>
let v = new Array(100000).fill(0).map(()=>[]);
function div(n)
{
for (let i = 2; i <= Math.sqrt(n);i++) {
if (n % i == 0) {
v[n].push(i);
}
}
}
function prodof2elements(arr, n)
{
let arr2 = arr.slice(0,)
arr2.sort((a,b)=>a-b);
let ans = 0;
for (let i = 0; i < n; i++) {
if (v[arr[i]].length == 0)
div(arr[i]);
for (let j of v[arr[i]]) {
if (arr2.includes(j) && arr2.includes(Math.floor(arr[i]/j))){
ans++;
break ;
}
}
}
return ans;
}
let arr = [ 2, 1, 8, 4, 32, 18 ];
let N = arr.length;
document.write(prodof2elements(arr, N), "</br>" );
</script>
|
Time Complexity: O(N3/2*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...