Problem Solving :: Is it Perfect Square? From toph.co
First time reading the problem i thought it is easy problem. But soon found it is not an easy problem.
Statement Says:Each test case contains an integer N (1 ≤ N ≤ 10^5), denoting how many numbers to compute. And below that an array A (1 ≤ Ai ≤ 100) consisting of N numbers will be given.
In naive approach all the numbers are multiplied then find it is perfect square or not. That will cause a memory overflow.
After trying some time, I found that only perfect square has the property that every prime factor appeared even number time in factorization.
Example:: 16=2*2*2*, 25=5*5, 225=5*5*3*3.
So i factored each Ai and store the counter of the factor in map. Then finally count if a factor is present with odd count. If there was any odd count then answer false. Or else answer is Yes.
Indeed it is a quite nice problem.
Statement Says:Each test case contains an integer N (1 ≤ N ≤ 10^5), denoting how many numbers to compute. And below that an array A (1 ≤ Ai ≤ 100) consisting of N numbers will be given.
In naive approach all the numbers are multiplied then find it is perfect square or not. That will cause a memory overflow.
After trying some time, I found that only perfect square has the property that every prime factor appeared even number time in factorization.
Example:: 16=2*2*2*, 25=5*5, 225=5*5*3*3.
So i factored each Ai and store the counter of the factor in map. Then finally count if a factor is present with odd count. If there was any odd count then answer false. Or else answer is Yes.
Indeed it is a quite nice problem.
Comments
Post a Comment