# Check whether the product of every subsequence is a perfect square or not

Given an array **arr[]** consisting of **N** positive integers, the task is to check if the product of elements of every subsequence of the given array **arr[] **is a perfect square or not. If found to be true, then print **Yes**. Otherwise, print **No**.

**Examples:**

Input:arr[] = {1, 4, 100}Output:YesExplanation:

Following are the subsequences of the given array arr[]:

- {1}, the product is equal to 1, and is a perfect square.
- {1, 4}, the product is equal to 4, and is a perfect square.
- {1, 100}, the product is equal to 100 and is a perfect square.
- {1, 4, 100}, the product is equal to 400 and is a perfect square.
- {4}, the product is equal to 4 and is a perfect square.
- {4, 100}, the product is equal to 400 and is a perfect square.
- {100}, the product is equal to 100 and is a perfect square.
Therefore, print “Yes”.

Input:arr[] = {1, 3}Output:No

**Naive Approach:** The simplest approach to solve the given problem is to generate all possible subsequences of the given array and if the product elements of every subsequence is a perfect square, then print **Yes**. Otherwise, print **No**.

**Time Complexity:** O(N*2^{N})**Auxiliary Space:** O(1)

**Efficient Approach:** The above approach can also be optimized using the fact that the product of two perfect square numbers will also be a perfect square. Therefore, to check if the individual product of elements of all subsequences is a perfect square or not, the idea is to check if all the array elements are perfect squares or not. If found to be true, then print **Yes**. Otherwise, print **No**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check if the product of` `// every subsequence of the array is a` `// perfect square or not` `string perfectSquare(` `int` `arr[], ` `int` `N)` `{` ` ` `// Traverse the given array` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// If arr[i] is a perfect` ` ` `// square or not` ` ` `int` `p = ` `sqrt` `(arr[i]);` ` ` `if` `(p * p != arr[i]) {` ` ` `return` `"No"` `;` ` ` `}` ` ` `}` ` ` `// Return "Yes"` ` ` `return` `"Yes"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 1, 4, 100 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << perfectSquare(arr, N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `class` `GFG{` ` ` `// Function to check if the product of` `// every subsequence of the array is a` `// perfect square or not` `static` `String perfectSquare(` `int` `arr[], ` `int` `N)` `{` ` ` ` ` `// Traverse the given array` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `{` ` ` ` ` `// If arr[i] is a perfect` ` ` `// square or not` ` ` `int` `p = (` `int` `)Math.sqrt(arr[i]);` ` ` `if` `(p * p != arr[i])` ` ` `{` ` ` `return` `"No"` `;` ` ` `}` ` ` `}` ` ` ` ` `// Return "Yes"` ` ` `return` `"Yes"` `;` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `arr[] = { ` `1` `, ` `4` `, ` `100` `};` ` ` `int` `N = arr.length;` ` ` ` ` `System.out.println(perfectSquare(arr, N));` `}` `}` `// This code is contributed by Potta Lokesh` |

## Python3

`# Python 3 program for the above approach` `from` `math ` `import` `sqrt` `# Function to check if the product of` `# every subsequence of the array is a` `# perfect square or not` `def` `perfectSquare(arr, N):` ` ` ` ` `# Traverse the given array` ` ` `for` `i ` `in` `range` `(N):` ` ` ` ` `# If arr[i] is a perfect` ` ` `# square or not` ` ` `p ` `=` `sqrt(arr[i])` ` ` `if` `(p ` `*` `p !` `=` `arr[i]):` ` ` `return` `"No"` ` ` `# Return "Yes"` ` ` `return` `"Yes"` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `arr ` `=` `[` `1` `, ` `4` `, ` `100` `]` ` ` `N ` `=` `len` `(arr)` ` ` `print` `(perfectSquare(arr, N))` ` ` `# This code is contributed by ipg2016107.` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to check if the product of` `// every subsequence of the array is a` `// perfect square or not` `static` `String perfectSquare(` `int` `[] arr, ` `int` `N)` `{` ` ` ` ` `// Traverse the given array` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` ` ` `// If arr[i] is a perfect` ` ` `// square or not` ` ` `int` `p = (` `int` `)Math.Sqrt(arr[i]);` ` ` `if` `(p * p != arr[i])` ` ` `{` ` ` `return` `"No"` `;` ` ` `}` ` ` `}` ` ` `// Return "Yes"` ` ` `return` `"Yes"` `;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `int` `[] arr = { 1, 4, 100 };` ` ` `int` `N = arr.Length;` ` ` `Console.WriteLine(perfectSquare(arr, N));` `}` `}` `// This code is contributed by subham348` |

## Javascript

`<script>` `// Javascript program for the above approach` `// Function to check if the product of` `// every subsequence of the array is a` `// perfect square or not` `function` `perfectSquare(arr, N)` `{` ` ` ` ` `// Traverse the given array` ` ` `for` `(let i = 0; i < N; i++)` ` ` `{` ` ` ` ` `// If arr[i] is a perfect` ` ` `// square or not` ` ` `let p = Math.sqrt(arr[i]);` ` ` `if` `(p * p != arr[i])` ` ` `{` ` ` `return` `"No"` `;` ` ` `}` ` ` `}` ` ` ` ` `// Return "Yes"` ` ` `return` `"Yes"` `;` `}` `// Driver Code` `let arr = [ 1, 4, 100 ];` `let N = arr.length;` `document.write(perfectSquare(arr, N));` `// This code is contributed by target_2` `</script>` |

**Output:**

Yes

**Time Complexity:** O(N)**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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.