#K49887. Perfect Square Product Pair
Perfect Square Product Pair
Perfect Square Product Pair
Given an array of integers, determine whether there exist two distinct indices i and j such that the product of arr[i]
and arr[j]
is a perfect square. A perfect square is an integer that can be expressed as n2 for some integer n. If such a pair exists, output YES
; otherwise, output NO
.
Note: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout). The first line of input contains an integer n indicating the number of elements in the array. The second line contains n space-separated integers representing the elements of the array.
Examples:
- Input:
5
followed by2 3 4 6 8
yields outputYES
since, for example, 4 and 9 (if computed from product 3*? check) but actually in this example, 2*8 = 16 which is a perfect square. - Input:
4
followed by11 13 17 19
yields outputNO
since no pair product forms a perfect square.
inputFormat
The first line contains an integer n (the number of elements in the array). The second line contains n space-separated integers.
outputFormat
Output a single line containing either YES
if there exist two indices such that the product of their corresponding elements is a perfect square, or NO
if no such pair exists.
5
2 3 4 6 8
YES