Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

HackerRank Day 29 Bitwise AND 30 days of code solution

YASH PAL, 31 July 2024

In this HackerRank Day 29 Bitwise AND 30 days of code problem set, we have given a set S in which we need to find two integers A and B. such that the value of A and B is maximum possible and also less than a given integer K.

HackerRank Day 29 Bitwise AND 30 days of code solution

Problem solution in Python 2 programming.

#!/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

Problem solution in Python 3 programming.

#!/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))

Problem solution in java programming.

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

Problem solution in c++ programming.

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

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 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("%dn", maxvalue(n, k));
    }
    return 0;
}

Problem solution in Javascript 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]);
        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;});
    }
}

30 days of code coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes