Header Ad

HackerRank Flipping bits problem solution

In this HackerRank Flipping Bits Interview preparation kit problem You will be given a list of 32-bit unsigned integers. Flip all the bits (1 -> 0 and 0 -> 1) and return the result as an unsigned integer.


HackerRank Flipping bits Interview preparation kit solution


Problem solution in Python programming.

import sys

t = int(sys.stdin.readline().strip())
for tests in range(t):
    n = int(sys.stdin.readline().strip())
    s = ['0' for x in range(32)]
    s_n = "{0:b}".format(n)
    for x in range(len(s_n)):
        s[31-x] = s_n[len(s_n) - 1 - x]
    for x in range(32):
        if s[x] == '0':
            s[x] = '1'
        else:
            s[x] = '0'
    print(int(''.join(s), 2))




Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        long max = Integer.MAX_VALUE;
        max = max * 2 + 1;
        for(int i = 0; i < n; i++) {
            System.out.println(~in.nextLong() & max);
        }
    }
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int t;
unsigned int x;

int main() {
    cin >> t;
    while (t--) {
        cin >> x;
        cout << (~x) << endl;
    }
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

	int count;
		    
	scanf("%d\n", &count);
	for (; count > 0; count--) {
		unsigned int num;
		scanf("%u\n", &num);
		printf("%u\n", ~num);
	}
	return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    var inputs = input.split('\n');
    inputs.shift();
    inputs.forEach(function(i){
        console.log(~parseInt(i,10)>>>0);
    });
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments