Day 29: Bitwise AND | 30 Days Of Code | HackerRank Solution

Hello coders, today we are going to solve Day 29: Bitwise AND HackerRank Solution in C++, Java and Python.

Day 29: Bitwise AND

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

Solution – Day 29: Bitwise AND

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;
    }
}

Java

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

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int t = scanner.nextInt();

        for (int tItr = 0; tItr < t; tItr++) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int res=0;
            for(int i=0;i<n;i++){
                for(int j=i+1;j<=n;j++){
                    int and=i&j;
                    if(and<k && and > res){
                        res=and;
                    }
                }
            }
           System.out.println(res);
            }  
             scanner.close(); 
          }
       
    }

Python

if __name__ == '__main__':
    T = int(input())

    for t_itr in range(T):
        nk = input().split()

        n = int(nk[0])

        k = int(nk[1])

        count = 0

        for x in range(n, 1, -1):
            for y in range(x-1, 0, -1):
                kk = x & y
                if kk > count and kk < k:
                    count = kk
                if count == k - 1:
                    break
            if count == k - 1:
                break

        print(count)

Disclaimer: The above Problem (Day 29:Bitwise AND) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *