Flipping bits – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
1 Month Preparation Kit Solutions – HackerRank
C++ Flipping bits HackerRank Solution
#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;
}
Java Flipping bits HackerRank Solution
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);
}
}
}
Python 3 Flipping bits HackerRank Solution
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))
JavaScript Flipping bits HackerRank Solution
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);
});
C Flipping bits HackerRank Solution
#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;
}
Leave a comment below