Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tatsuyafujisaki/hackerrank-10-days-of-javascript-solutions
Solutions to 10 Days of JavaScript on HackerRank
https://github.com/tatsuyafujisaki/hackerrank-10-days-of-javascript-solutions
10-days-of-javascript hackerrank javascript
Last synced: about 1 month ago
JSON representation
Solutions to 10 Days of JavaScript on HackerRank
- Host: GitHub
- URL: https://github.com/tatsuyafujisaki/hackerrank-10-days-of-javascript-solutions
- Owner: tatsuyafujisaki
- Created: 2020-02-08T07:42:56.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-07T13:36:15.000Z (about 4 years ago)
- Last Synced: 2024-12-08T21:55:44.232Z (about 2 months ago)
- Topics: 10-days-of-javascript, hackerrank, javascript
- Language: JavaScript
- Homepage: https://www.hackerrank.com/domains/tutorials/10-days-of-javascript
- Size: 57.6 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Explanation of `Day 6: Bitwise Operators`
* `a & b` is at least `k - 2` because:
* If `k - 2` is even, the least significant bit (LSB) of `k - 2` is `0`. Assign `a = k - 2`.
* If `k - 1` is even, LSB of `k - 1` is 0. Assign `a = k - 1`.
* Either way, LSB of `a + 1` is `1`. Assign `b = a + 1`. So, `a & b == a`.
* `a & b` can be `k - 1` in the following case:
* Given the constraints `a < b` and `a & b < k`, for `a & b == k - 1` to hold, `a = k - 1`.
* For `((k - 1) & b) === k - 1` to hold, `b` is the same as `k - 1` except one bit, which is positioned at `a`'s any bit of 0, is 1.
* To be precise, the exception bit of `b` must be positioned at `a`'s LSB of `0` to minimize the chance of `b` exceeding `n`. Such `b` is `(k - 1) | k`.
* In conclusion, `a & b = k - 1` when `a = k - 1` and `b = ((k - 1) | k) <= n`. Otherwise, `a & b = k - 2`.A naive implementation of the above logic is:
```js
function getMaxLessThanK(n, k) {
let a = k - 1;
let b = (k - 1) | k;if (n < b) {
a = k - 2;
b = a + 1;
}return a & b;
}
```The shorter implementation is:
```js
function getMaxLessThanK(n, k) {
return ((k - 1) | k) <= n ? (k - 1) : (k - 2);
}
```