In this HackerRank Day 28: RegEx, Patterns, and Intro to Databases problem solution we have 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.


HackerRank Day 29: Bitwise AND problem solution

Problem solution in Python programming.

#!/bin/python3

import sys


t = int(input().strip())
for a0 in range(t):
    n,k = input().strip().split(' ')
    n,k = [int(n),int(k)]
    a = k-1 
    b = ~a & -(~a)
    if (a | b) > n:
        print(k-2)
    else:
        print(k-1)



Problem solution in Java programming.

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

public class Solution {
    
    static int bitwise(int n, int k) {
        int max = 0;
        for (int a = 1; a < n; a++) {
            for (int b = (a+1); b <= n; b++) {
                int m = a&b;
                if (m > max && m < k){
                    max = m;
                }    
            }
        }
        return max;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            int n = in.nextInt();
            int k = in.nextInt();
            System.out.println(bitwise(n, k));
        }
    }
}




Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int t;
    cin >> t;
    for(int a0 = 0; a0 < t; a0++){
		int n;
		int k;
		cin >> n >> k;
		int a, b;
		a = k - 1;
		b = ~a & -(~a);
		if ((a | b) > n)
			cout << a - 1 << endl;
		else
			cout << a << endl;
    }
    return 0;
}



Problem solution in C programming.

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

int main(){
    int t,i,j,b,l=-1; 
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        int n; 
        int k; 
        scanf("%d %d",&n,&k);
        l=-1;
        for(i=1;i<n;i++)
            {
            for(j=i+1;j<=n;j++)
                {
                b=i&j;
                if(b<k)
                    {
                    if(b>l)
                        {
                        l=b;
                    }
                }
            }
        }
        printf("%d\n",l);
    }
    return 0;
}



Problem solution in Java programming.

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]);
        var max = 0;
        var result;
        for (var i = 1; i <= n - 1; i++) {
            for (var j = i + 1; j <= n; j++) {
                if ((i&j) > max) {
                    max = i&j;
                }
                if (max < k) {
                    result = max;
                }
            }
        }
        console.log(result);
    }

}