In this HackerRank Day 6: Bitwise Operators 10 days of javascript problem solution We define S to be a sequence of distinct sequential integers from 1 to n; in other words, S = {1,2,3,...,n}. We want to know the maximum bitwise AND value of any two integers, a and b (where a < b), in sequence S that is also less than a given integer, k.

Complete the function in the editor so that given n and k, it returns the maximum a & b < k.

HackerRank Day 6: Bitwise Operators problem solution


Problem solution in JavaScript programming.

/*
*	Return the largest value of any a & b < k in S (where a < b).
* 	
*	n: Set S is a sequence of distinct integers from 1 to n (i.e., {1, 2, ..., n}).
*	k: An integer.
*/
function getMaxLessThanK(n, k) {
    var max = 0;
    
    for (let i = 1; i <= n; ++i)
        for (let j = i + 1; j <= n; ++j) {
            let and = i & j;
            if (and < k && and > max)
                max = and;
        }
    return max;
}