Count perfect square fractions from given array
Last Updated :
28 Oct, 2021
Given two arrays arr1[] and arr2[] of length N which contains Numerator and Denominator of N fractions respectively, the task is to count the number of fractions from the array which is a perfect square of a fraction.
Examples:
Input: arr1[] = {3, 25, 49, 9}, arr2[] = {27, 64, 7, 3}
Output: 2
Explanation:
(arr1[0] / arr2[0]) = (3 / 27) = (1 / 9) = (1 / 3)2
(arr1[1] / arr2[1]) = (25 / 64) = (5 / 8) 2
(arr1[0] / arr2[0]) and (arr1[1] / arr2[1]) are perfect square fractions. Therefore, the required output is 2.
Input: arr1[] = {1, 11, 3, 9}, arr2[] = {9, 11, 5, 1}
Output: 3
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int GCD( int a, int b)
{
if (b ==0 ) {
return a;
}
return GCD(b,a%b);
}
bool isPerfectSq( int N)
{
int x = sqrt (N);
if (x * x == N) {
return 1;
}
return 0;
}
int cntPerSquNum( int arr1[], int arr2[],
int N)
{
int cntPerfNum = 0;
for ( int i = 0; i < N; i++) {
int gcd = GCD(arr1[i], arr2[i]);
if (isPerfectSq(arr1[i]/ gcd) &&
isPerfectSq(arr2[i]/ gcd)) {
cntPerfNum++;
}
}
return cntPerfNum;
}
int main() {
int arr1[]={ 3, 25, 49, 9 };
int arr2[]={ 27, 64, 7, 3 };
int N = sizeof (arr1) / sizeof (arr1[0]);
cout<<cntPerSquNum(arr1, arr2, N);
}
|
Java
import java.lang.Math;
class GFG{
public static int GCD( int a, int b)
{
if (b == 0 )
{
return a;
}
return GCD(b, a % b);
}
public static boolean isPerfectSq( int N)
{
int x = ( int )Math.sqrt(N);
if (x * x == N)
{
return true ;
}
return false ;
}
public static int cntPerSquNum( int arr1[],
int arr2[],
int N)
{
int cntPerfNum = 0 ;
for ( int i = 0 ; i < N; i++)
{
int gcd = GCD(arr1[i], arr2[i]);
if (isPerfectSq(arr1[i] / gcd) &&
isPerfectSq(arr2[i] / gcd))
{
cntPerfNum++;
}
}
return cntPerfNum;
}
public static void main(String[] args)
{
int arr1[] = { 3 , 25 , 49 , 9 };
int arr2[] = { 27 , 64 , 7 , 3 };
int N = arr1.length;
System.out.println(cntPerSquNum(arr1, arr2, N));
}
}
|
Python3
def GCD(a, b):
if (b = = 0 ):
return a
return GCD(b, a % b)
def isPerfectSq(N):
x = ( int )( pow (N, 1 / 2 ))
if (x * x = = N):
return True
return False
def cntPerSquNum(arr1, arr2, N):
cntPerfNum = 0
for i in range (N):
gcd = GCD(arr1[i], arr2[i])
if (isPerfectSq(arr1[i] / gcd) and
isPerfectSq(arr2[i] / gcd)):
cntPerfNum + = 1
return cntPerfNum
if __name__ = = '__main__' :
arr1 = [ 3 , 25 , 49 , 9 ]
arr2 = [ 27 , 64 , 7 , 3 ]
N = len (arr1)
print (cntPerSquNum(arr1, arr2, N))
|
C#
using System;
class GFG{
public static int GCD( int a,
int b)
{
if (b == 0)
{
return a;
}
return GCD(b, a % b);
}
public static bool isPerfectSq( int N)
{
int x = ( int )Math.Sqrt(N);
if (x * x == N)
{
return true ;
}
return false ;
}
public static int cntPerSquNum( int []arr1,
int []arr2,
int N)
{
int cntPerfNum = 0;
for ( int i = 0; i < N; i++)
{
int gcd = GCD(arr1[i], arr2[i]);
if (isPerfectSq(arr1[i] / gcd) &&
isPerfectSq(arr2[i] / gcd))
{
cntPerfNum++;
}
}
return cntPerfNum;
}
public static void Main(String[] args)
{
int []arr1 = {3, 25, 49, 9};
int []arr2 = {27, 64, 7, 3};
int N = arr1.Length;
Console.WriteLine(cntPerSquNum(arr1,
arr2, N));
}
}
|
Javascript
<script>
function GCD(a,b)
{
if (b ==0 ) {
return a;
}
return GCD(b,a%b);
}
function isPerfectSq( N)
{
var x = Math.sqrt(N);
if (x * x == N) {
return 1;
}
return 0;
}
function cntPerSquNum(arr1,arr2,N)
{
var cntPerfNum = 0;
for ( var i = 0; i < N; i++) {
var gcd = GCD(arr1[i], arr2[i]);
if (isPerfectSq(arr1[i]/ gcd) &&
isPerfectSq(arr2[i]/ gcd)) {
cntPerfNum++;
}
}
return cntPerfNum;
}
var arr1 = [ 3, 25, 49, 9 ];
var arr2 = [ 27, 64, 7, 3 ];
var N = 4;
document.write(cntPerSquNum(arr1, arr2, N));
</script>
|
Time Complexity: O(N * log(M)), where M is the maximum element from both the arrays.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...