Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:40:40+00:00 2026-05-27T06:40:40+00:00

Hi all I was wondering if there is a way to implement this method

  • 0

Hi all I was wondering if there is a way to implement this method without casting to a wider data type (e.g. long, double, etc)?

CanTimes(int a, int b){
    returns true if a * b is within the range of -2^31 to 2^31-1, else false;
}

For example, we could implement one for the method CanAdd (without casts) as such:

    public static boolean CanPlus(int a, int b) {
        if (b >= 0) {
            return a <= Integer.MAX_VALUE - b
        } else {
            return a >= Integer.MIN_VALUE - b
        }
    }

Implementation language is Java, though of course this is more of a language-agnostic problem.

I was thinking if there’s some logic we can employ to decide if a * b fits the range of an integer without casting it to a wider data type?

Solution ! based on Strelok’s comment:

public static boolean CanTimes(int a, int b) {
    if (a == 0 || b == 0) {
        return true;
    }
    if (a > 0) {
        if (b > 0) {
            return a <= Integer.MAX_VALUE / b;
        } else {
            return a <= Integer.MIN_VALUE / b;
        }
    } else {
        if (b > 0) {
            return b <= Integer.MIN_VALUE / a;
        } else {
            return a <= -Integer.MAX_VALUE / b;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:40:41+00:00Added an answer on May 27, 2026 at 6:40 am

    As per my comment, here is the adapted version, with some unit tests:

    public static int mulAndCheck( int a, int b )
    {
        int ret;
        String msg = "overflow: multiply";
        if ( a > b )
        {
            // use symmetry to reduce boundry cases
            ret = mulAndCheck( b, a );
        }
        else
        {
            if ( a < 0 )
            {
                if ( b < 0 )
                {
                    // check for positive overflow with negative a, negative b
                    if ( a >= Integer.MAX_VALUE / b )
                    {
                        ret = a * b;
                    }
                    else
                    {
                        throw new ArithmeticException( msg );
                    }
                }
                else if ( b > 0 )
                {
                    // check for negative overflow with negative a, positive b
                    if ( Integer.MIN_VALUE / b <= a )
                    {
                        ret = a * b;
                    }
                    else
                    {
                        throw new ArithmeticException( msg );
    
                    }
                }
                else
                {
                    // assert b == 0
                    ret = 0;
                }
            }
            else if ( a > 0 )
            {
                // assert a > 0
                // assert b > 0
    
                // check for positive overflow with positive a, positive b
                if ( a <= Integer.MAX_VALUE / b )
                {
                    ret = a * b;
                }
                else
                {
                    throw new ArithmeticException( msg );
                }
            }
            else
            {
                // assert a == 0
                ret = 0;
            }
        }
        return ret;
    }
    
    @Test( expected = ArithmeticException.class )
    public void testOverflow()
    {
        mulAndCheck( Integer.MAX_VALUE, Integer.MAX_VALUE );
    }
    
    @Test( expected = ArithmeticException.class )
    public void testOverflow1()
    {
        mulAndCheck( Integer.MIN_VALUE, Integer.MAX_VALUE );
    }
    
    @Test
    public void testTimesMinus1()
    {
        Assert.assertEquals( Integer.MIN_VALUE + 1, mulAndCheck( Integer.MAX_VALUE, -1 ) );
        Assert.assertEquals( Integer.MAX_VALUE, mulAndCheck( Integer.MIN_VALUE + 1, -1 ) );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if there is any way to list all the calls to
I was wondering if there's a way to watch all RoutedEvents that are raised
I'm wondering if there are any simple ways to get a list of all
Hullo all, Wondering if there are any Java hackers who can clue me in
I was wondering what is the best way (if at all possible) to determine
Hey all, I've been reading up on the best way to implement the GetHashCode()
I have multiple interfaces that my objects can implement. I am wondering if there
I was wondering what is the best way to implement a renderer in JavaScript.
Is there a way in C# to mark a method as being part of
I have an interesting situation and I'm wondering if there is a better way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.