- Related Questions & Answers
- Python Program to Find the Product of two Numbers Using Recursion
- Program to find the winner of an array game using Python
- C++ program to find the first digit in product of an array of numbers
- Python Program to find the sum of array
- Maximum product subset of an array in C++ program
- Program to find the largest product of two distinct elements in Python
- Program to find maximum product of contiguous subarray in Python
- Finding product of an array using recursion in JavaScript
- Java program to find the sum of elements of an array
- C Program for product of array
- Python Program to find out the price of a product after a number of days
- Find the sum of array in Python Program
- Program to find out the dot product of two sparse vectors in Python
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- Program to find length of longest sign alternating subsequence from a list of numbers in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array called nums. We have to find sign of the multiplication result of all elements present in the array.

So, if the input is like nums = [-2,3,6,-9,2,-4], then the output will be Negative, as the multiplication result is -2592

To solve this, we will follow these steps −

zeroes := 0,negatives := 0

for each i in nums, do

if i is same as 0, then

zeroes := zeroes + 1

if i < 0, then

negatives := negatives + 1

if zeroes > 0 , then

return "Zero"

otherwise when negatives mod 2 is same as 0, then

return "Positive"

otherwise,

return "Negative"

Let us see the following implementation to get better understanding −

def solve(nums): zeroes,negatives = 0,0 for i in nums: if i == 0: zeroes+=1 if i < 0: negatives+=1 if zeroes > 0: return "Zero" elif negatives % 2 == 0: return "Positive" else: return "Negative" nums = [-2,3,6,-9,2,-4] print(solve(nums))

[-2,3,6,-9,2,-4]

Negative

Advertisements