检查每个子序列的乘积是否是完全平方
给定一个由N个正整数组成的数组arr[] ,任务是检查给定数组arr[]的每个子序列的元素的乘积是否是完全平方。如果发现是真的,则打印Yes 。否则,打印No 。
例子:
Input: arr[] = {1, 4, 100}
Output: Yes
Explanation:
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
朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能子序列,如果每个子序列的乘积元素都是完美正方形,则打印Yes 。否则,打印No 。
时间复杂度: O(N*2 N )
辅助空间: O(1)
有效方法:上述方法也可以利用两个完美平方数的乘积也将是一个完美平方这一事实进行优化。因此,要检查所有子序列的元素的单个乘积是否是完全平方,其思想是检查所有数组元素是否是完全平方。如果发现是真的,则打印Yes 。否则,打印No 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)