LeetCode Entry
3314. Construct the Minimum Bitwise Array I
Reverse x | (x+1) operation on prime numbers
3314. Construct the Minimum Bitwise Array I easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1243
Problem TLDR
| Reverse x | (x+1) operation on prime numbers #easy |
Intuition
The op: x|(x+1) does set rightmost 0 to 1. 100: 100 | 101 = 101 Reversing it: set first suffix 1 bit to 0. 101: 100
// 111 +1
//1000
//
// 101 100 or 101
// 111 11 or 100
// 10 -
// 11 1 or 10
//
// 1011 1001 or 1010
// 1101 1100 or 1
// 11111 1111 or 10000
Approach
- do a brute-force
- learn from others
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
// 7ms
fun minBitwiseArray(n: List<Int>)=n.map {
if (it == 2) -1 else it xor it.inv().takeLowestOneBit()/2
}
// 0ms
pub fn min_bitwise_array(n: Vec<i32>) -> Vec<i32> {
n.iter().map(|&n| if n == 2 {-1} else {n^((n+1)&!n)/2}).collect()
}