Today we are going to solve HackerRank Day 29: Bitwise AND 30 days of code solutions in C, C++, Java, Javascript & Python.
Objective
Welcome to the last day! Today, we’re discussing bitwise operations.
Task
Given set S = {1, 2, 3, . . . ,N}. Find two integers, A and B (where A < B), from set S such that the value of A&B is the maximum possible and also less than a given integer, K. In this case, & represents the bitwise AND operator.
Function Description
Complete the bitwiseAnd function in the editor below.
bitwiseAnd has the following paramter(s):
– int N: the maximum integer to consider
– int K: the limit of the result, inclusive
Returns
– int: the maximum value of A&B within the limit.
Input Format
The first line contains an integer, T, the number of test cases.
Each of the T subsequent lines defines a test case as 2 space-separated integers, N and K, respectively.
Constraints
- 1 <= T <= 103
- 2 <= N <= 103
- 2 <= K <= N
Sample Input
STDIN Function
----- --------
3 T = 3
5 2 N = 5, K = 2
8 5 N = 8, K = 5
2 2 N = 8, K = 5
Sample Output
1
4
0
HackerRank Day 29: Bitwise AND 30 days of code solutions
Bitwise AND 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 maxvalue(int n, int k) {
int i, j;
int res = 0, max_res = 0;
for (i = 1; i <= n; i++) {
for (j = i+1; j <=n ; j++) {
int val = i &j;
if (val > max_res && val < k) {
max_res = val;
}
}
}
return max_res;
}
int main(){
int t;
int a0;
scanf("%d",&t);
for(a0 = 0; a0 < t; a0++){
int n;
int k;
scanf("%d %d",&n,&k);
printf("%d\n", maxvalue(n, k));
}
return 0;
}
Bitwise AND Solution in C++
#include <iostream>
#include <vector>
using namespace std;
int main(){
int ncases, n, k, max = 0, tmp = 0;
vector<int> range;
range.reserve(1000);
cin >> ncases;
for(int i = 0; i < ncases; ++i){
cin >> n >> k;
for(int j = 0; j < n; ++j)
range.push_back(j + 1);
for(int x = 0; x < range.size() - 1; ++x){
for(int y = x + 1; y < range.size(); ++y){
tmp = range[x] & range[y];
if(tmp < k)
max = (tmp > max ? tmp : max);
}
}
cout << max << '\n';
range.clear();
max = 0;
}
}
Bitwise AND Solution in Java
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int i = 0; i < q; i++) {
int n = in.nextInt();
int k = in.nextInt();
int maxed = 0;
for (int b = 2; b <= n; b++) {
for (int a = 1; a < b; a++) {
if (a == b) continue;
int ab = a&b;
if (ab > maxed && ab < k) maxed = ab;
}
}
System.out.println(maxed);
}
}
Bitwise AND Solution in Python 2
#!/bin/python
import sys
t = int(raw_input().strip())
for a0 in xrange(t):
n,k = raw_input().strip().split(' ')
n,k = [int(n),int(k)]
maxValue = 0
check = 0
for i in range(n-1,0,-1):
for j in range(n,i,-1):
tmp = i&j
if ((tmp > maxValue)&(tmp < k)):
maxValue = tmp
if (maxValue + 1 == k):
check = 1
break
if (check == 1):
break
print maxValue
Bitwise AND Solution in Python 3
#!/bin/python3
import math
import os
import random
import re
import sys
def max_bit(n,k):
maximum = 0
for i in range(1,n+1):
for j in range(1,i):
h = i & j
if maximum < h < k:
maximum = h
if maximum == k-1:
return maximum
return maximum
if __name__ == '__main__':
t = int(input())
for t_itr in range(t):
nk = input().split()
n = int(nk[0])
k = int(nk[1])
print(max_bit(n,k))
Bitwise AND Solution in Javascript
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var n_temp = readLine().split(' ');
var n = parseInt(n_temp[0]);
var k = parseInt(n_temp[1]);
function findMaxPoss(arr) {
var res = 0;
for(var i = 0; i < arr.length; i++){
for(var j = i + 1; j < arr.length; j++){
var ans = arr[i] & arr[j];
if((ans > res) && (ans < k)){
res = ans;
}
}
}
return res;
}
console.log(findMaxPoss(range(n)));
}
function range(n){
return Array.apply(null, Array(n)).map(function (_, i) {return i + 1;});
}
}
Read More: 30 Days of Code HackerRank Solutions List – Day 0 to Day 29