Friday, September 5, 2025
HomeData Modelling & AICheck whether product of integers from a to b is positive ,...

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: 
 

  1. If a > 0 and b > 0 then the resultant product will be positive.
  2. If a < 0 and b > 0 then the result will be zero as a * (a + 1) * … * 0 * … (b – 1) * b = 0.
  3. 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++




// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function to check whether the product
// of integers of the range [a, b]
// is positive, negative or zero
void solve(long long int a, long long int b)
{
 
    // If both a and b are positive then
    // the product will be positive
    if (a > 0 && b > 0) {
        cout << "Positive";
    }
 
    // If a is negative and b is positive then
    // the product will be zero
    else if (a <= 0 && b >= 0) {
        cout << "Zero" << endl;
    }
 
    // If both a and b are negative then
    // we have to find the count of integers
    // in the range
    else {
 
        // Total integers in the range
        long long int n = abs(a - b) + 1;
 
        // If n is even then the resultant
        // product is positive
        if (n % 2 == 0) {
            cout << "Positive" << endl;
        }
        // If n is odd then the resultant
        // product is negative
        else {
            cout << "Negative" << endl;
        }
    }
}
 
// Driver code
int main()
{
    int a = -10, b = -2;
 
    solve(a, b);
 
    return 0;
}


Java




// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
// Function to check whether the product
// of integers of the range [a, b]
// is positive, negative or zero
static void solve(long a, long b)
{
 
    // If both a and b are positive then
    // the product will be positive
    if (a > 0 && b > 0)
    {
        System.out.println( "Positive");
    }
 
    // If a is negative and b is positive then
    // the product will be zero
    else if (a <= 0 && b >= 0)
    {
        System.out.println( "Zero" );
    }
 
    // If both a and b are negative then
    // we have to find the count of integers
    // in the range
    else
    {
 
        // Total integers in the range
        long n = Math.abs(a - b) + 1;
 
        // If n is even then the resultant
        // product is positive
        if (n % 2 == 0)
        {
            System.out.println( "Positive");
        }
         
        // If n is odd then the resultant
        // product is negative
        else
        {
            System.out.println( "Negative");
        }
    }
}
 
    // Driver code
    public static void main (String[] args)
    {
        int a = -10, b = -2;
     
        solve(a, b);
    }
}
 
// This code is contributed by anuj_67..


Python3




# Python 3 implementation of the approach
 
# Function to check whether the product
# of integers of the range [a, b]
# is positive, negative or zero
def solve(a,b):
     
    # If both a and b are positive then
    # the product will be positive
    if (a > 0 and b > 0):
        print("Positive")
 
    # If a is negative and b is positive then
    # the product will be zero
    elif (a <= 0 and b >= 0):
        print("Zero")
 
    # If both a and b are negative then
    # we have to find the count of integers
    # in the range
    else:
         
        # Total integers in the range
        n = abs(a - b) + 1
 
        # If n is even then the resultant
        # product is positive
        if (n % 2 == 0):
            print("Positive")
             
        # If n is odd then the resultant
        # product is negative
        else:
            print("Negative")
 
# Driver code
if __name__ == '__main__':
    a = -10
    b = -2
 
    solve(a, b)
     
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to check whether the product
    // of integers of the range [a, b]
    // is positive, negative or zero
    static void solve(long a, long b)
    {
     
        // If both a and b are positive then
        // the product will be positive
        if (a > 0 && b > 0)
        {
            Console.WriteLine( "Positive");
        }
     
        // If a is negative and b is positive then
        // the product will be zero
        else if (a <= 0 && b >= 0)
        {
            Console.WriteLine( "Zero" );
        }
     
        // If both a and b are negative then
        // we have to find the count of integers
        // in the range
        else
        {
     
            // Total integers in the range
            long n = Math.Abs(a - b) + 1;
     
            // If n is even then the resultant
            // product is positive
            if (n % 2 == 0)
            {
                Console.WriteLine( "Positive");
            }
             
            // If n is odd then the resultant
            // product is negative
            else
            {
                Console.WriteLine( "Negative");
            }
        }
    }
     
    // Driver code
    public static void Main ()
    {
        int a = -10, b = -2;
     
        solve(a, b);
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
// Javascript  implementation of the approach
 
// Function to check whether the product
// of integers of the range [a, b]
// is positive, negative or zero
function  solve( a,  b)
{
 
    // If both a and b are positive then
    // the product will be positive
    if (a > 0 && b > 0)
    {
        document.write( "Positive");
    }
 
    // If a is negative and b is positive then
    // the product will be zero
    else if (a <= 0 && b >= 0)
    {
        document.write( "Zero" );
    }
 
    // If both a and b are negative then
    // we have to find the count of integers
    // in the range
    else
    {
 
        // Total integers in the range
        let  n = Math.abs(a - b) + 1;
 
        // If n is even then the resultant
        // product is positive
        if (n % 2 == 0)
        {
            document.write( "Positive");
        }
         
        // If n is odd then the resultant
        // product is negative
        else
        {
            document.write( "Negative");
        }
    }
}
 
    // Driver code
     
        let a = -10;
        let b = -2;
     
        solve(a, b);
 
 
// This code is contributed by Bobby
 
</script>


Output: 

Negative

 

Time Complexity: O(1), as there is no loop.
Auxiliary Space: O(1), as no extra space is required.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6637 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS