Top ad position

I am getting output -294967296

Started 2 years ago by M R Alam in Programming Language, Java

In leetcode there is a test case [1000000000,1000000000,1000000000,1000000000] and for some reason the addition of these is equal to -294967296

Body

So in leetcode 4 sum there is a test case [1000000000,1000000000,1000000000,1000000000] and for some reason the addition of these is equal to -294967296 and i do not understand how exactly this works i assumed it was because the actual answer is larger than max val of an int but i tried that and it just threw an error instead so why does that not happen here

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        if(target>Integer.MAX_VALUE||target<Integer.MIN_VALUE){
            return new ArrayList();
        }
        int lb =0;
        int ub  = nums.length-1;
        int sum = 0;
        HashSet<List<Integer>> ans = new HashSet();
        Arrays.sort(nums);
        for(int i = 0;i<nums.length;i++){
        <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> i+<span class="hljs-number">1</span>;j&lt;nums.length;j++){
            lb = j+<span class="hljs-number">1</span>;
            ub = nums.length-<span class="hljs-number">1</span>;
            <span class="hljs-keyword">while</span>(lb&lt;ub){

                sum = nums[i]+nums[j]+nums[lb]+nums[ub];
                <span class="hljs-keyword">if</span>(sum==-<span class="hljs-number">294967296</span>){
                    System.out.println(sum);
                    System.out.println(nums[i]);
                    System.out.println(nums[j]);
                    System.out.println(nums[lb]);
                    System.out.println(nums[ub]);
                }
                <span class="hljs-keyword">if</span>(sum==target){
                    ans.add(Arrays.asList(nums[i],nums[j],nums[lb],nums[ub]));
                    lb++;
                    ub--;
                }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(sum&lt;target){
                    lb++;
                }<span class="hljs-keyword">else</span>{
                    ub--;
                }

            }
        }
    }
    List&lt;List&lt;Integer&gt;&gt; ret = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>(ans);
    <span class="hljs-keyword">return</span> ret;
}

}

this is the code i wrote and i dont think there is an issue here

1 Replies

  • Replied 2 years ago

    Report

    First of all...

    if(target>Integer.MAX_VALUE||target<Integer.MIN_VALUE){
        return new ArrayList();
    }
    

    This if statement will never trigger because an int by definition cannot be smaller than Integer.MIN_VALUE or larger than Integer.MAX_VALUE. In other words, target > Integer.MAX_VALUE || target < Integer.MIN_VALUE will always evaluate to false.

    The maximum value an int variable can hold is 2^31 - 1, or 2,147,483,647. Your sum is exactly 4 billion which causes your integer variable to overflow.

    You really should use long variables to operate on values of this magnitude.

    P.S. What does integer overflow mean? byteshortint and long numbers in Java are stored using the 2's complement system. The first bit of the value stores the sign. If it's equal to 0, the number is positive; if it's 1, the number is negative. Thus the largest positive value an int can hold is 01111111111111111111111111111111 in binary. This number happens to be 2,147,483,647 in decimal. If you add 1 to it, it becomes 10000000000000000000000000000000, which is -2,147,483,648 in decimal (it's negative because the first bit is now 1).

Bottom ad position