
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if product of array containing prime numbers is a perfect square in Python
Suppose we have an array nums with all prime numbers. We have to check whether the product of all numbers present in nums is a perfect square or not.
So, if the input is like nums = [3,3,7,7], then the output will be True as product of all elements in nums is 441 which is a perfect square as 21^2 = 441.
To solve this, we will follow these steps −
- m := a map containing all elements in nums and their frequencies
- for each key in nums, do
- if m[key] is odd, then
- return False
- if m[key] is odd, then
- return True
Example
Let us see the following implementation to get better understanding −
from collections import defaultdict def solve(nums) : m = defaultdict(int) for key in nums : m[key] += 1 for key in nums : if m[key] % 2 == 1 : return False return True nums = [3,3,7,7] print(solve(nums))
Input
[3,3,7,7]
Output
True
- Related Articles
- Check if given number is perfect square in Python
- Check if a number in a list is perfect square using Python
- Check if a number is perfect square without finding square root in C++
- Check whether the number formed by concatenating two numbers is a perfect square or not in Python
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Product of all prime numbers in an Array in C++
- Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?
- Check if N is a Factorial Prime in Python
- Check if LCM of array elements is divisible by a prime number or not in Python
- Check if N is Strong Prime in Python
- Check if product of first N natural numbers is divisible by their sum in Python
- Check if all sub-numbers have distinct Digit product in Python
- Check for perfect square in JavaScript
- Python Program to Check if a Number is a Perfect Number

Advertisements