Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • 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
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank AND Product problem solution

YASH PAL, 31 July 2024

In this HackerRank AND Product problem solution, we have given N pairs of long integers A[i] and B[i] and we need to computer and print the bitwise AND of all-natural numbers in the inclusive range between A[i] and B[i].

HackerRank AND Product problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

def and_product(start, end):
    result = start
    steps = end - start 
    i = 0               
    while (start >> i) > 0:
        # If the number of steps causes the ith bit to flip, then
        # the result of that bit should be 0.
        if lmask(start, i + 1) + steps > lmask(-1, i + 1):
            result = bitmask(result, i)
        i += 1
    return result

def lmask(n, k):
    
    return n & ((1 << k) - 1)

def bitmask(n, k):
    
    return n & ((-1 << (k + 1)) | lmask(-1, k))

def main():
    k = int(input())
    for _ in range(k):
        start, end = input().split()
        print(and_product(int(start), int(end)))

main()

Problem 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 sc = new Scanner(System.in);
        int size = sc.nextInt();
        for(int ii = 0; ii < size; ii++){
            long start = sc.nextLong();
            long end = sc.nextLong();
            
            long moveFactor = 1;
        while(start != end){
            start >>= 1;
            end >>= 1;
            moveFactor <<= 1;
        }
        System.out.println( start * moveFactor);
       
        }
    }
}

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    
    int t;
    for(cin >> t;t;t--) {
        unsigned int a, b;
        cin >> a >> b;
        for(int i = 0;i < sizeof(a) * 8;i++) {
            if(a >> i == b >> i){
                cout << (a >> i << i) << endl;
                break;
            }
        }
    }
    
    return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>

uint32_t msb(uint32_t i);
uint32_t and(uint32_t a, uint32_t b);

uint32_t msb(uint32_t i) {
    i |= i >> 1;
    i |= i >> 2;
    i |= i >> 4;
    i |= i >> 8;
    i |= i >> 16;
    
    return i ^ (i >> 1);
}

uint32_t and(uint32_t a, uint32_t b) {
    uint32_t msb_a = msb(a), msb_b = msb(b), max = 0;
    
    if (msb_a == msb_b) {
        do {
            max |= (msb_a & a);
        } while ((msb_a >>= 1) && (a & msb_a) == (b & msb_a));
    }
    
    return max;
}

int main() {
    int t, i;
    uint32_t a, b;
    
    scanf("%d", &t);
    
    for (i = 0; i < t; ++i) {
        scanf("%u %u", &a, &b);
        printf("%un", and(a, b));
    }
    
    return 0;
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

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