VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-perfect-square-fractions-from-given-array/

⇱ Count perfect square fractions from given array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count perfect square fractions from given array

Last Updated : 23 Jul, 2025

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


Output: 
2

 

Time Complexity: O(N * log(M)), where M is the maximum element from both the arrays. 
Auxiliary Space: O(1)

Comment
Article Tags: