Given two integers a and b, the task is to check whether the product of integers from the rage v[a, b] i.e. a * (a + 1) * (a + 2) * … * b is positive, negative or zero.
Input: a = -10, b = -2
Input: a = -10, b = 2
Naive approach: We can run a loop from a to b and multiply all the numbers starting from a to b and check whether the product is positive negative or zero. This solution will fail for large values of a and b and will result in overflow.
Efficient approach: There are three possible case:
- If a > 0 and b > 0 then the resultant product will be positive.
- If a < 0 and b > 0 then the result will be zero as a * (a + 1) * … * 0 * … (b – 1) * b = 0.
- If a < 0 and b < 0 then the result will depend on the count of numbers (as all the numbers are negative)
- If the count of negative numbers is even then the result will be positive.
- Else the result will be negative.
Below is the implementation of the above approach:
- C program to Check Whether a Number is Positive or Negative or Zero
- Check whether a number can be represented as sum of K distinct positive integers
- Minimum number of changes such that elements are first Negative and then Positive
- Replace all elements by difference of sums of positive and negative numbers after that element
- Random list of M non-negative integers whose sum is N
- Ways to write N as sum of two or more positive integers | Set-2
- Number of ways in which N can be represented as the sum of two positive integers
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Find n positive integers that satisfy the given equations
- Maximum number of distinct positive integers that can be used to represent N
- Count positive integers with 0 as a digit and maximum 'd' digits
- Number of arrays of size N whose elements are positive integers and sum is K
- Number of sub arrays with negative product
- Number of Subarrays with positive product
- Maximum GCD of N integers with given product
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.