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
- Ways to write N as sum of two or more positive integers | Set-2
- Find n positive integers that satisfy the given equations
- Number of arrays of size N whose elements are positive integers and sum is K
- Count positive integers with 0 as a digit and maximum 'd' digits
- Number of sub arrays with negative product
- Maximum GCD of N integers with given product
- Find N integers with given difference between product and sum
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Find a pair with maximum product in array of Integers
- Count 'd' digit positive integers with 0 as a digit
- Check if a triangle of positive area is possible with the given angles
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.