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
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Replace all elements by difference of sums of positive and negative numbers after that element
- Minimum number of changes such that elements are first Negative and then Positive
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Find N distinct integers with zero sum
- Minimize cost to convert given two integers to zero using given operations
- Random list of M non-negative integers whose sum is N
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Check whether the given integers a, b, c and d are in proportion
- Count 'd' digit positive integers with 0 as a digit
- 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
- 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 ways in which N can be represented as the sum of two positive integers
- Maximum number of distinct positive integers that can be used to represent N
- Find the number of positive integers less than or equal to N that have an odd number of digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.