Skip to main content
edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

I'm in the process of improving my coding style and performance hence I decided to delve into bit manipulation on Hackerrank. I thought the question was straight forward but on large input, my solution's performance declines. How do I improve it? Any smart tricks to solve the below question is welcome as well.

Below is the question

Given an integer, \$n\$ , find each \$x\$ such that:

\$0 \leq x \leq n \$

\$ n + x = n \oplus x \$

where \$\oplus\$ denotes the bitwise XOR operator. Then print an integer denoting the total number of \$x\$ 's satisfying the criteria above.

Input Format

A single integer, \$n\$ .

Constraints

\$0 \leq n \leq 10 ^ {15}\$

Subtasks

\$0 \leq n \leq 100\$ for \$60\%\$ of the maximum score

Output Format

Print the total number of integer \$x\$ 's satisfying both of the conditions specified above.

Sample Input 0

5

Sample Output 0

2

Explanation 0

For \$n = 5\$ , the \$x\$ values \$0\$ and \$2\$ satisfy the conditions:

Thus, we print \$2\$ as our answer.

Input Format

A single integer, \$n\$ .

Constraints

\$0 \leq n \leq 10 ^ {15}\$

Subtasks

\$0 \leq n \leq 100\$ for \$60\%\$ of the maximum score

Output Format

Print the total number of integer \$x\$ 's satisfying both of the conditions specified above.

Sample Input 0

5

Sample Output 0

2

Explanation 0

For \$n = 5\$ , the \$x\$ values \$0\$ and \$2\$ satisfy the conditions:

Thus, we print \$2\$ as our answer.


Here is my code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
     static int SumVsXoR(long number)
    {
        int count =0;
        long j = 0;
        while (j <= number)
        {           
            if(j + number == (j^number)){               
                count++;                
            }
            j++;
        }
        return count;
    }

    static void Main(String[] args) {
        long n = Convert.ToInt64(Console.ReadLine());
        Console.WriteLine(SumVsXoR(n));
    }
}

I'm in the process of improving my coding style and performance hence I decided to delve into bit manipulation on Hackerrank. I thought the question was straight forward but on large input, my solution's performance declines. How do I improve it? Any smart tricks to solve the below question is welcome as well.

Below is the question

Given an integer, \$n\$ , find each \$x\$ such that:

\$0 \leq x \leq n \$

\$ n + x = n \oplus x \$

where \$\oplus\$ denotes the bitwise XOR operator. Then print an integer denoting the total number of \$x\$ 's satisfying the criteria above.

Input Format

A single integer, \$n\$ .

Constraints

\$0 \leq n \leq 10 ^ {15}\$

Subtasks

\$0 \leq n \leq 100\$ for \$60\%\$ of the maximum score

Output Format

Print the total number of integer \$x\$ 's satisfying both of the conditions specified above.

Sample Input 0

5

Sample Output 0

2

Explanation 0

For \$n = 5\$ , the \$x\$ values \$0\$ and \$2\$ satisfy the conditions:

Thus, we print \$2\$ as our answer.


Here is my code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
     static int SumVsXoR(long number)
    {
        int count =0;
        long j = 0;
        while (j <= number)
        {           
            if(j + number == (j^number)){               
                count++;                
            }
            j++;
        }
        return count;
    }

    static void Main(String[] args) {
        long n = Convert.ToInt64(Console.ReadLine());
        Console.WriteLine(SumVsXoR(n));
    }
}

I'm in the process of improving my coding style and performance hence I decided to delve into bit manipulation on Hackerrank. I thought the question was straight forward but on large input, my solution's performance declines. How do I improve it? Any smart tricks to solve the below question is welcome as well.

Below is the question

Given an integer, \$n\$ , find each \$x\$ such that:

\$0 \leq x \leq n \$

\$ n + x = n \oplus x \$

where \$\oplus\$ denotes the bitwise XOR operator. Then print an integer denoting the total number of \$x\$ 's satisfying the criteria above.

Input Format

A single integer, \$n\$ .

Constraints

\$0 \leq n \leq 10 ^ {15}\$

Subtasks

\$0 \leq n \leq 100\$ for \$60\%\$ of the maximum score

Output Format

Print the total number of integer \$x\$ 's satisfying both of the conditions specified above.

Sample Input 0

5

Sample Output 0

2

Explanation 0

For \$n = 5\$ , the \$x\$ values \$0\$ and \$2\$ satisfy the conditions:

Thus, we print \$2\$ as our answer.


Here is my code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
     static int SumVsXoR(long number)
    {
        int count =0;
        long j = 0;
        while (j <= number)
        {           
            if(j + number == (j^number)){               
                count++;                
            }
            j++;
        }
        return count;
    }

    static void Main(String[] args) {
        long n = Convert.ToInt64(Console.ReadLine());
        Console.WriteLine(SumVsXoR(n));
    }
}
Source Link
Tolani
  • 2.5k
  • 7
  • 31
  • 49

Hackerrank Sum vs XoR

I'm in the process of improving my coding style and performance hence I decided to delve into bit manipulation on Hackerrank. I thought the question was straight forward but on large input, my solution's performance declines. How do I improve it? Any smart tricks to solve the below question is welcome as well.

Below is the question

Given an integer, \$n\$ , find each \$x\$ such that:

\$0 \leq x \leq n \$

\$ n + x = n \oplus x \$

where \$\oplus\$ denotes the bitwise XOR operator. Then print an integer denoting the total number of \$x\$ 's satisfying the criteria above.

Input Format

A single integer, \$n\$ .

Constraints

\$0 \leq n \leq 10 ^ {15}\$

Subtasks

\$0 \leq n \leq 100\$ for \$60\%\$ of the maximum score

Output Format

Print the total number of integer \$x\$ 's satisfying both of the conditions specified above.

Sample Input 0

5

Sample Output 0

2

Explanation 0

For \$n = 5\$ , the \$x\$ values \$0\$ and \$2\$ satisfy the conditions:

Thus, we print \$2\$ as our answer.


Here is my code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
     static int SumVsXoR(long number)
    {
        int count =0;
        long j = 0;
        while (j <= number)
        {           
            if(j + number == (j^number)){               
                count++;                
            }
            j++;
        }
        return count;
    }

    static void Main(String[] args) {
        long n = Convert.ToInt64(Console.ReadLine());
        Console.WriteLine(SumVsXoR(n));
    }
}