Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Sum vs XOR problem solution

YASH PAL, 31 July 2024

In this HackerRank Sum vs XOR problem solution, we have given an integer N and we need to find each X such that:

0 <= X <= N

N + X = N XOR X

and then return the number of x that satisfying the criteria.

HackerRank Sum vs XOR problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

#!/bin/python3

import sys

def bitLen(int_type):
    length = 0
    while (int_type):
        int_type >>= 1
        length += 1
    return(length)

n = int(input().strip())
st = '{0:b}'.format(n)
elev = 0
#print(st)
for v in st:
    if(v == '0'):
        elev += 1
if(n == 0):
    elev -= 1
print(1<<elev)
        

Problem solution in Java.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        
        if (n == 0) {
            System.out.println(1);
            return;
        }
        
        int MSB = 0;
        for (int i = 63; i >= 0; i--)
            if ( ((1L << i) & n) != 0) {
                MSB = i + 1;
                break;
            }
        long mask = 1;
        for (int i = 1; i < MSB; i++)
            mask |= mask << 1;
        long NOTn = -n - 1L;
        NOTn &= mask;
        int numBits = Long.bitCount(NOTn);
        
        long count = 1L << numBits;
                
        
        System.out.println(count);
    }
}

Problem solution in C++.

#include <bits/stdc++.h>
using namespace std;

int const N = 100 * 1000 + 16;

int main() {
  long long n;
  scanf("%lld", &n);
  auto ones = __builtin_popcountll(n);
  auto leading = __builtin_clzll(n);
  auto sz = sizeof(n)*8LL;
  auto ans = (sz-leading-ones);
  if(n==0)
    puts("1");
  else
    printf("%lldn", (ans==0)?0:(1LL<<ans));
}

Problem solution in C.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    long n,sum1,sum2,count=0; 
    scanf("%ld",&n);
    while(n)
        {
        sum1+=n%2?0:1;
        n/=2;
    }
    count=pow(2,sum1);
    printf("%ld",count);
    return 0;
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes