HackerRank Coding Interview 8. Maximum Or-Sum
Time to complete — 30 minutes
Question Type = Medium
Asked in = Netflix, Amazon, Uber, Lyft, Bloomberg, Morgan Stanley, Apple, and Google, Synchrony.
Skills: arrays, bit manipulation, prefix array
Insights — It should be read very carefully read and you must ask questions in between.
Question —
For an array of n integers, arr[n], perform the following operation up to some integer k times.
- Choose an index i such that 1 ≤ i ≤ n.
- Replace arr[i] with arr[i]×2.
The or-sum is the bitwise-or of all elements in the final array after the operations. Return the maximum or-sum possible.
# HINT is below
# bitwise operators
a = 10
b = 4
# Print bitwise AND operation
print("a & b =", a & b)
# Print bitwise OR operation
print("a | b =", a | b)
# Print bitwise NOT operation
print("~a =", ~a)
# print bitwise XOR operation
print("a ^ b =", a ^ b)
HINT
.....for Bitwise OR operation with integer 2
.....K-times, is nothing but RIGHT shift the number K times.
Interger_value << k ==== K-times [Interger_value (bitwise-OR) 2 ]
Example
arr = [12, 9]
k = 1
These outcomes are possible after 1 operation.
- Select i = 1 :