You are given an integer array arr. We define two concepts:
- The goodness of a sequence is the bitwise-OR of its elements. The goodness of an empty sequence is
0. - A strictly increasing subsequence is a subsequence where each element is strictly greater than the previous one.
- An empty sequence is also considered to be strictly increasing.
Your task is to find all possible distinct goodness values that can be obtained from any strictly increasing subsequence of the given arr. The final result should be an array of these goodness values, sorted in non-decreasing order.
Constraints:
- 1 ≤
arr.length≤ 104 - 0 ≤
arr[i]< 1024
Example 1:
Input: arr = [4, 2, 4, 1] Output: [0, 1, 2, 4, 6] Explanation: The distinct goodness values are derived from various strictly increasing subsequences:
- [] → goodness = 0
- [1] → goodness = 1
- [2] → goodness = 2
- [4] → goodness = 4
- [2, 4] → goodness = 2 | 4 = 6, because 010 | 100 = 110.
Example 2:
Input: arr = [3, 2, 4, 6] Output: [0, 2, 3, 4, 6, 7]
Example 3:
Input: arr = [5] Output: [0, 5]
Accepted
1.6k/2.9k
Acceptance Rate56.9%
Ask AI Assistant

Ask AI Assistant
Ask me anything about this coding question!
Test Cases
Input
arr =
Output
Expected