Check whether product of integers from a to b is positive , negative or zero
Given two integers a and b, the task is to check whether the product of integers from the range v[a, b] i.e. a * (a + 1) * (a + 2) * … * b is positive, negative or zero.
Examples:
Input: a = -10, b = -2
Output: Negative
Input: a = -10, b = 2
Output: Zero
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++
#include <iostream>
using namespace std;
void solve( long long int a, long long int b)
{
if (a > 0 && b > 0) {
cout << "Positive" ;
}
else if (a <= 0 && b >= 0) {
cout << "Zero" << endl;
}
else {
long long int n = abs (a - b) + 1;
if (n % 2 == 0) {
cout << "Positive" << endl;
}
else {
cout << "Negative" << endl;
}
}
}
int main()
{
int a = -10, b = -2;
solve(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void solve( long a, long b)
{
if (a > 0 && b > 0 )
{
System.out.println( "Positive" );
}
else if (a <= 0 && b >= 0 )
{
System.out.println( "Zero" );
}
else
{
long n = Math.abs(a - b) + 1 ;
if (n % 2 == 0 )
{
System.out.println( "Positive" );
}
else
{
System.out.println( "Negative" );
}
}
}
public static void main (String[] args)
{
int a = - 10 , b = - 2 ;
solve(a, b);
}
}
|
Python3
def solve(a,b):
if (a > 0 and b > 0 ):
print ( "Positive" )
elif (a < = 0 and b > = 0 ):
print ( "Zero" )
else :
n = abs (a - b) + 1
if (n % 2 = = 0 ):
print ( "Positive" )
else :
print ( "Negative" )
if __name__ = = '__main__' :
a = - 10
b = - 2
solve(a, b)
|
C#
using System;
class GFG
{
static void solve( long a, long b)
{
if (a > 0 && b > 0)
{
Console.WriteLine( "Positive" );
}
else if (a <= 0 && b >= 0)
{
Console.WriteLine( "Zero" );
}
else
{
long n = Math.Abs(a - b) + 1;
if (n % 2 == 0)
{
Console.WriteLine( "Positive" );
}
else
{
Console.WriteLine( "Negative" );
}
}
}
public static void Main ()
{
int a = -10, b = -2;
solve(a, b);
}
}
|
Javascript
<script>
function solve( a, b)
{
if (a > 0 && b > 0)
{
document.write( "Positive" );
}
else if (a <= 0 && b >= 0)
{
document.write( "Zero" );
}
else
{
let n = Math.abs(a - b) + 1;
if (n % 2 == 0)
{
document.write( "Positive" );
}
else
{
document.write( "Negative" );
}
}
}
let a = -10;
let b = -2;
solve(a, b);
</script>
|
Time Complexity: O(1), as there is no loop.
Auxiliary Space: O(1), as no extra space is required.
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...