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:
- Initialize a variable cntPerfNum to store the count of perfect square fractions.
- Traverse both the arrays and for each array elements, check if the value of (arr1[i] / GCD(arr1[i], arr2[i])) and (arr2[i] / GCD(arr1[i], arr2[i])) is a prefect square or not. If found to be true then increment the value of cntPerfNum by 1.
- Finally, print the value of cntPerfNum.
Below is the implementation of the above approach:
C++
//C++ implementation of the //above approachB #include <bits/stdc++.h> using namespace std; // Function to find the GCD // of two numbers int GCD(int a,int b) { // If b is 0 if (b ==0 ) { return a; } return GCD(b,a%b); } // Function to check if N // is perfect square bool isPerfectSq(int N) { // Stores square root // of N int x = sqrt(N); // Check if N is a // perfect square if (x * x == N) { return 1; } return 0; } // Function to count perfect square fractions int cntPerSquNum(int arr1[], int arr2[], int N) { // Stores count of perfect square // fractions in both arrays int cntPerfNum = 0; // Traverse both the arrays for (int i = 0; i < N; i++) { // Stores gcd of (arr1[i], arr2[i]) int gcd = GCD(arr1[i], arr2[i]); // If numerator and denomerator of a // reduced fraction is a perfect square if (isPerfectSq(arr1[i]/ gcd) && isPerfectSq(arr2[i]/ gcd)) { // Update cntPerfNum cntPerfNum++; } } return cntPerfNum; } //Driver Code 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); } |
2
Time Complexity: O(N * log(M)), where M is the maximum element from both the arrays.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is a perfect square having all its digits as a perfect square
- Count numbers upto N which are both perfect square and perfect cube
- Count Pandigital Fractions pairs in given Array
- Check if a number is perfect square without finding square root
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Product of given N fractions in reduced form
- Sum of given N fractions in reduced form
- Find N fractions that sum upto a given fraction N/D
- HCF of array of fractions (or rational numbers)
- Count of pairs in an array whose sum is a perfect square
- Count of pairs in an array whose product is a perfect square
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Maximum subsequence sum from a given array which is a perfect square
- Program to add two fractions
- Program to compare two fractions
- LCM and HCF of fractions
- Print all proper fractions with denominators less than equal to N
- Count of subarrays having exactly K perfect square numbers
- Count of subarrays whose sum is a perfect square
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.

