Skip to content
Programming101
Programming101

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
Programming101

Learn everything about programming

HackerRank The Great XOR problem solution

YASH PAL, 31 July 2024

In this HackerRank The Great XOR problem solution we have given Q queries and each query is in the form of a long integer denoting X. for each query we need to print the total number of values of A satisfying the conditions given below.

A XOR X > X

0 < A < X

HackerRank The Great XOR problem solution

Problem solution in Python.

#!/bin/python3

import sys


q = int(input().strip())
for a0 in range(q):
    x = int(input().strip())
    binary = list(bin(x))[2:]
    binary.reverse()
    summ = 0
    for idx, item in enumerate(binary):
        if item == "0":
            summ += 2**idx

    print(summ)

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[] powersOf2 = new long[64];
        powersOf2[0] = 1;
        for (int i = 1; i < 64; i++) {
            powersOf2[i] = powersOf2[i - 1] << 1;
        }
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            long x = in.nextLong();

            long count = 0;
            int position = 0;
            while (x > 1) {
                if ((x & 1) == 0) {
                    count += powersOf2[position];
                }
                x >>= 1;
                position++;
            }
            System.out.println(count);
        }
    }

}

Problem solution in C++.

#include <iostream>

using namespace std;

long long n, q, ans;
int main(){
    cin >> q;
    while(q --){
        cin >> n;
        ans = 1;
        while(ans <= n){
            ans *= 2;
        }
        ans /= 2;
        cout << ans - 1 - (n - ans) << endl;
    }
}

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>

long fun3(long x) {
    long l;
    for(l=1;l<x;) {
        l *= 2; l++;
    }

    return l^x;
}

int main(){
    int q; 
    scanf("%d",&q);
    for(int a0 = 0; a0 < q; a0++){
        long x; 
        scanf("%ld",&x);
        printf("%ldn", fun3(x));
    }
    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
©2025 Programming101 | WordPress Theme by SuperbThemes