Sort by

recency

|

39 Discussions

|

  • + 0 comments

    Here is 2"s complement problem solution in Python Java C++ and c programming- https://programs.programmingoneonone.com/2021/07/hackerrank-2s-complement-problem-solution.html

  • + 0 comments

    Python3 solution

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    def count1s(n,i):
        if i == 1:
            return n & 1
        mask = 1 << i - 1
        remainder = (0xFFFFFFFF >> (32-i+1))&n
        return (n & mask > 0) * ((mask * (i - 1) >> 1) + 1 + remainder) + count1s(remainder, i - 1)
    
    def startCount():
        iterations = int(input())
        lis = []
        while iterations > 0:
            n1, n2 = [int(x) for x in input().split()] 
            lis.append([n1, n2])
            iterations -= 1
        for element in lis:
            if (element[0] > 0) or (element[1] < 0):
                print(count1s(element[1], 32) - count1s(element[0] - 1, 32))
            else:
                print((1 << 36) - count1s(element[0] - 1, 32) + count1s(element[1], 32))
    
    startCount()
    
  • + 1 comment

    https://www.youtube.com/watch?v=4qH4unVtJkE If someone is interested

  • + 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();
        }
    }
    
  • + 0 comments

    long twosCompliment(long a, long b) { /* * Write your code here. */ scanf("%ld %ld",&a,&b); return(0); }

    Just by doing this i get segmentation fault..any idea to fix this.. i experienced the same with many other t