• + 0 comments

    Can't figure out why it is not working

    import java.io.*;
    import java.math.*;
    import java.text.*;
    import java.util.*;
    import java.util.regex.*;
    import java.lang.*;
    
    public class Solution 
    {
        static int table[] = new int[256];
        static void initialize()
        {
            table[0]=0;
            for (int i = 1 ; i < 256; i++ )
            {
                table[i] = (i&1)+table[i/2];
            }
        }
        static int countBit(int n)
        {
            return table[n & 0xff ] + table[(n>>8) & 0xff ] + table[(n>>16) & 0xff ] + table[(n>>24) & 0xff ];
        }
        static int twosCompliment(int a, int b) 
        {
            int ret = 0;
            if ( a >=0 && b>=0 || a <=0 && b <= 0)
            {
                for (int i = a ; i <= b ;i++)
                {
                ret+= countBit(i);
                }
            }
            else
            {
                int hashMap[] = new int[b-a+1];
                for (int i = a ; i <= b ; i++ )
                {
                    // System.out.println(i);
                    if (i == 0)
                    {
                        ret+=0;
                    }
                    else if (i < 0)
                    {
                        int temp = countBit(i);
                        hashMap[i*-1 -1 ]= temp;
                        ret+=temp;
                    }
                    else if (i > 0 && hashMap[i] != 0)
                    {
                        ret+=countBit(i);
                    }
                    else if (i > 0 && hashMap[i] ==0 )
                    {
                        ret+=countBit(i);
                    }
                }
            }
            return ret;
        }
    
        private static final Scanner scanner = new Scanner(System.in);
    
        public static void main(String[] args) throws IOException {
            
            int t = Integer.parseInt(scanner.nextLine().trim());
            initialize();
            for (int tItr = 0; tItr < t; tItr++) {
                // String[] ab = scanner.nextLine().split(" ");
    
                // int a = Integer.parseInt(ab[0].trim());
    
                // int b = Integer.parseInt(ab[1].trim());
                int a = scanner.nextInt();
                int b = scanner.nextInt();
    
                // int result =0 ;
    
                System.out.println(twosCompliment(a, b));
                // bufferedWriter.write(String.valueOf(result));
                // bufferedWriter.newLine();
            }
    
            // bufferedWriter.close();
        }
    }