Day 6: Bitwise Operators Solution
Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 6: Bitwise Operators Solution.
Also visit this link:ย Day 5: Arrow Functions Solution
Day 6: Bitwise Operators Solution
Objective
Today, weโre practicingย bitwise operations.
Task
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ย .
Note:ย Theย &ย symbol represents theย bitwise ANDย operator.
Input Format
The first line contains an integer,ย q, denoting the number of function calls.
Each of theย qย subsequent lines defines a dataset for a function call in the form of two space-separated integers describing the respective values ofย nย andย k.
Constraints
- 1 <=ย qย <= 10^3
- 2 <=ย nย <= 10^3
- 2 <=ย kย <=ย n
Output Format
Return the maximum possible value ofย aย &ย bย <ย kย for anyย aย <ย bย in sequenceย S.
Sample Input 0
3
5 2
8 5
2 2
Sample Output 0
1
4
0
Explanation 0
We perform the followingย qย = 3 function calls:
- Whenย nย = 5ย andย kย = 2, we have the following possibleย aย andย bย values in setย Sย = {1, 2, 3, 4, 5}:The maximum of anyย a & bย that is alsoย <ย kย isย 1, so we returnย 1.
- Whenย nย = 8ย andย kย = 5, the maximum of anyย a & bย <ย kย in setย Sย = {1, 2, 3, 4, 5, 6, 7, 8}ย isย 4, so we returnย 4.
- Whenย nย = 2ย andย kย = 2, the maximum of anyย a & bย <ย kย in setย Sย =ย {1, 2}ย isย 0ย (see table above), so we returnย 0.
Sample Input 1
2
9 2
8 3
Sample Output 1
1
2
Explanation 1
We perform the followingย qย = 2ย function calls:
- Whenย nย = 9ย andย kย = 2, the maximum of anyย a & b < kย in setย Sย = {1, 2, 3, 4, 5, 6, 7, 8, 9}ย isย 1, so we returnย 1.
- Whenย ย nย = 8ย andย kย = 3, the maximum of anyย a & b < kย in setย Sย = {1, 2, 3, 4, 5, 6, 7, 8}ย isย 2, so we returnย 2.
Solution โ Day 6: Bitwise Operators
'useย strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); letย inputStringย =ย ''; letย currentLineย =ย 0; process.stdin.on('data',ย inputStdinย =>ย { ย ย ย ย inputStringย +=ย inputStdin; }); process.stdin.on('end',ย _ย =>ย { ย ย ย ย inputStringย =ย inputString.trim().split('\n').map(stringย =>ย { ย ย ย ย ย ย ย ย returnย string.trim(); ย ย ย ย }); ย ย ย ย ย ย ย ย main();ย ย ย ย }); functionย readLine()ย { ย ย ย ย returnย inputString[currentLine++]; } functionย getMaxLessThanK(n,ย k){ ย ย ย ย varย maxiย =ย 0; ย ย ย ย varย currย =ย -1; ย ย ย ย ย ย ย ย forย (varย iย =ย 0;ย iย <ย nย ;ย i++){ ย ย ย ย ย ย ย ย forย (varย jย =ย iย +ย 1;ย jย <=ย n;ย j++){ ย ย ย ย ย ย ย ย ย ย ย ย currย =ย iย &ย jย ; ย ย ย ย ย ย ย ย ย ย ย ย ifย (currย <ย kย &&ย currย >ย maxi){ ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย maxiย =ย curr; ย ย ย ย ย ย ย ย ย ย ย ย } ย ย ย ย ย ย ย ย } ย ย ย ย } ย ย ย ย returnย maxi; } functionย main()ย { ย ย ย ย constย qย =ย +(readLine()); ย ย ย ย ย ย ย ย forย (letย iย =ย 0;ย iย <ย q;ย i++)ย { ย ย ย ย ย ย ย ย constย [n,ย k]ย =ย readLine().split('ย ').map(Number); ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย console.log(getMaxLessThanK(n,ย k)); ย ย ย ย } }