Count of elements to be multiplied with integers to make each pair of Array a perfect square
Given an array arr[] containing positive integers, the task is to find the minimum number of operations to be done on the array to make every number of the array a superpower.
In each operation, we can multiply any element of the array with an integer.
A superpower is defined as a number in the array which when multiplied by any other number in the array apart from itself forms a perfect square.
Examples:
Input: arr[] = {2, 2, 2}
Output: 0
Explanation:
We don’t need to perform any operation on the array because on selecting any element (2) and multiplying it with any element in some other index (2), we get a perfect square (4).
Input: arr[] = {2, 4, 6}
Output: 2
Explanation:
We need to perform the following two operations:
First, we multiply the element at index 1 with integer 2. The array becomes {2, 8, 6}.
Second, we multiply the element at index 2 with integer 3. The array becomes {2, 8, 18}.
Now, all the elements have become a superpower. The multiplication of any two numbers from the array gives a perfect square.
That is, 2 * 8 = 16, 2 * 16 = 36 and 8 * 18 = 144.
Approach: Since we need to check the prime factors for all the numbers, the idea is to first precompute the unique prime factors of all the numbers and store it in a hashmap. Then, we create a variable to store the number of times that the prime factor appears in every single element of the array. We also need to observe that we need to find the minimum number of steps to convert the array. Therefore, we calculate the number of odd in the vector and the number of even prime factors, and whichever is the minimum, that will be the answer. The following steps can be computed to find the answer:
- We first need to calculate the spf[] array. This is the array in which the smallest prime factor for all the elements is stored.
- Then, we need to find the unique prime factors and store it in the hashmap.
- Now, traverse the hash map to find the unique prime factors of a number and iterate over the elements in the given array to calculate the frequency of the prime factors.
- Now, two variables are initialized which stores the frequency of the prime number whose occurrence is even and another store the frequency of the prime numbers which is odd.
- Now, we need to add the minimum of the two variables in our final variable which is the minimum operation to attain superpower for that prime number.
- Repeat the above steps for all the numbers in the array.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
void spf_array( int spf[])
{
spf[1] = 1;
for ( int i = 2; i < 1000; i++)
spf[i] = i;
for ( int i = 4; i < 1000; i += 2)
spf[i] = 2;
for ( int i = 3; i * i < 1000; i++) {
if (spf[i] == i) {
for ( int j = i * i; j < 1000; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
int minimum_operation( int b[], int d,
int spf[])
{
unordered_map< int , int > m;
int i = 0;
int c = 0;
for (i = 0; i < d; i++) {
int x = b[i];
while (x != 1) {
x = x / spf[x];
if (m[spf[x]] == 0) {
m[spf[x]] = 1;
}
}
}
m.erase(1);
for ( auto x : m) {
int e = 0, o = 0;
int j = x.first;
for (i = 0; i < d; i++) {
if (b[i] % j == 0) {
int h = 0;
int g = b[i];
while (g != 0) {
if (g % j != 0) {
break ;
}
g = g / j;
h = h + 1;
}
if (h % 2 == 0) {
e = e + 1;
}
else {
o = o + 1;
}
}
else {
e = e + 1;
}
}
c = c + min(o, e);
}
return c;
}
int main()
{
int spf[1001];
int b[] = { 1, 4, 6 };
int d = sizeof (b) / sizeof (b[0]);
spf_array(spf);
cout << minimum_operation(b, d, spf)
<< endl;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void spf_array( int spf[])
{
spf[ 1 ] = 1 ;
for ( int i = 2 ; i < 1000 ; i++)
spf[i] = i;
for ( int i = 4 ; i < 1000 ; i += 2 )
spf[i] = 2 ;
for ( int i = 3 ; i * i < 1000 ; i++)
{
if (spf[i] == i)
{
for ( int j = i * i; j < 1000 ; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
static int minimum_operation( int b[], int d,
int spf[])
{
Map<Integer, Integer> m= new HashMap<>();
int i = 0 ;
int c = 0 ;
for (i = 0 ; i < d; i++)
{
int x = b[i];
while (x != 1 )
{
x = x / spf[x];
if (m.get(spf[x]) == null )
{
m.put(spf[x], 1 );
}
}
}
m.remove( 1 );
for (Map.Entry<Integer,
Integer> x : m.entrySet())
{
int e = 0 , o = 0 ;
int j = x.getKey();
for (i = 0 ; i < d; i++)
{
if (b[i] % j == 0 )
{
int h = 0 ;
int g = b[i];
while (g != 0 )
{
if (g % j != 0 )
{
break ;
}
g = g / j;
h = h + 1 ;
}
if (h % 2 == 0 )
{
e = e + 1 ;
}
else
{
o = o + 1 ;
}
}
else
{
e = e + 1 ;
}
}
c = c + Math.min(o, e);
}
return c;
}
public static void main (String[] args)
{
int [] spf = new int [ 1001 ];
int b[] = { 1 , 4 , 6 };
int d = b.length;
spf_array(spf);
System.out.print(minimum_operation(b, d, spf));
}
}
|
Python3
def spf_array(spf):
spf[ 1 ] = 1 ;
for i in range ( 2 , 1000 ):
spf[i] = i;
for i in range ( 4 , 1000 , 2 ):
spf[i] = 2 ;
for i in range ( 3 , ( int )( 1000 * * 0.5 ), 1 ):
if (spf[i] = = i):
for j in range (i * i, 1000 , i):
if (spf[j] = = j):
spf[j] = i;
def minimum_operation( b, d, spf) :
m = dict ();
i = 0 ;
c = 0 ;
for i in range (d):
x = b[i];
while (x ! = 1 ):
x = (x / / spf[x]);
if (spf[x] not in m):
m[spf[x]] = 1
m.pop( 1 );
for key in m.keys():
e = 0
o = 0
j = key;
for i in range (d):
if (b[i] % j = = 0 ):
h = 0 ;
g = b[i];
while (g ! = 0 ):
if (g % j ! = 0 ):
break ;
g = g / / j
h = h + 1 ;
if (h % 2 = = 0 ):
e = e + 1 ;
else :
o = o + 1 ;
else :
e = e + 1 ;
c = c + min (o, e);
return c;
spf = [ 0 ] * ( 1001 )
b = [ 1 , 4 , 6 ];
d = len (b);
spf_array(spf);
print ( minimum_operation(b, d, spf) );
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void spf_array( int []spf)
{
spf[1] = 1;
for ( int i = 2; i < 1000; i++)
spf[i] = i;
for ( int i = 4; i < 1000; i += 2)
spf[i] = 2;
for ( int i = 3; i * i < 1000; i++)
{
if (spf[i] == i)
{
for ( int j = i * i; j < 1000; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
static int minimum_operation( int []b,
int d, int []spf)
{
Dictionary< int ,
int > m = new Dictionary< int ,
int >();
int i = 0;
int c = 0;
for (i = 0; i < d; i++)
{
int x = b[i];
while (x != 1)
{
x = x / spf[x];
if (!m.ContainsKey(spf[x]))
{
m.Add(spf[x], 1);
}
}
}
m.Remove(1);
foreach (KeyValuePair< int ,
int > x in m)
{
int e = 0, o = 0;
int j = x.Key;
for (i = 0; i < d; i++)
{
if (b[i] % j == 0)
{
int h = 0;
int g = b[i];
while (g != 0)
{
if (g % j != 0)
{
break ;
}
g = g / j;
h = h + 1;
}
if (h % 2 == 0)
{
e = e + 1;
}
else
{
o = o + 1;
}
}
else
{
e = e + 1;
}
}
c = c + Math.Min(o, e);
}
return c;
}
public static void Main(String[] args)
{
int [] spf = new int [1001];
int []b = {1, 4, 6};
int d = b.Length;
spf_array(spf);
Console.Write(minimum_operation(b, d, spf));
}
}
|
Javascript
<script>
function spf_array(spf)
{
spf[1] = 1;
for ( var i = 2; i < 1000; i++)
spf[i] = i;
for ( var i = 4; i < 1000; i += 2)
spf[i] = 2;
for ( var i = 3; i * i < 1000; i++) {
if (spf[i] == i) {
for ( var j = i * i; j < 1000; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
function minimum_operation( b, d, spf)
{
var m = new Map();
var i = 0;
var c = 0;
for (i = 0; i < d; i++) {
var x = b[i];
while (x != 1) {
x = parseInt(x / spf[x]);
if (!m.has(spf[x])) {
m.set(spf[x], 1);
}
}
}
m. delete (1);
m.forEach((value, key) => {
var e = 0, o = 0;
var j = key;
for (i = 0; i < d; i++) {
if (b[i] % j == 0) {
var h = 0;
var g = b[i];
while (g != 0) {
if (g % j != 0) {
break ;
}
g = parseInt(g / j);
h = h + 1;
}
if (h % 2 == 0) {
e = e + 1;
}
else {
o = o + 1;
}
}
else {
e = e + 1;
}
}
c = c + Math.min(o, e);
});
return c;
}
var spf = Array(1001)
var b = [1, 4, 6 ];
var d = b.length;
spf_array(spf);
document.write( minimum_operation(b, d, spf) );
</script>
|
Time Complexity: O(N * log(N)), where N is the size of the input array.
Space Complexity: O(n)
The space complexity of the algorithm is O(n), where n is the length of the array. We create an array of size n to store the smallest prime factor of every element of the array.
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...