Input: -2147483648, -1 Output -2147483648 is not the Wrong Answer.


  • 1
    R

    in java, -2147483648/-1 returns exactly -2147483648, because of the overflow. Why we force the user to return 2147483647?

    public int divide(int dividend, int divisor) {
    	int res = Integer.MAX_VALUE;
    	if (divisor != 0) {
    		res = (int) div(Math.abs((long) dividend), Math.abs((long) divisor), 0);
    		if (dividend > 0 ^ divisor > 0) {
    			res = 0 - res;
    		}
    	}
    	return res;
    }
    
    private long div(long x, long y, int sum) {
    	long oy = y, res = sum, i = 1;
    	sum = 1;
    	while (x >= y) {
    		if (x < y << 1) {
    			return res + div(x - y, oy, sum);
    		} else {
    			y <<= 1;
    			sum = 1 << i++;
    		}
    	}
    	return res;
    }
    

    it will get following warning message:

    "Wrong Answer":
    Input:
    -2147483648
    -1
    Output:
    -2147483648
    Expected:
    2147483647

  • -2
    P

    because different compiler may return different result when the answer is overflow.
    then how can we judge the correctness of a program?
    Maybe one solution is to force every coder to use java


  • 1
    S

    So does python

    python
    -2147483648/-1
    2147483648L

    I think the answer 2147483647 is improper


  • 0
    H

    Same issue with javascript -- javascript has much higher overflow so need to code a workaround to get around that, which is ugly.


  • 0
    O

    I made a custom testcase that violated their overflow rules. it confused me greatly as i was trying to validate my edge cases. In the end i decided to ignore the custom testcase runner and just submit my solution. (which passed :) )

    p.s. took me a while to figure out i need to do long division to solve this fast enough


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.