Snowflake

Online Assessment
Online Assessment//Distinct Score Values
Q1
Medium-Hard

Distinct Score Values

Dynamic ProgrammingBit ManipulationFenwick TreeSubsequences
Set 1 • 90 min
Problem Statement

A coding competition organized to recruit software developers includes an operation.

The score of a sequence is defined as the result of the bitwise-OR operation on all elements of the sequence.

Given an array arr of length n, identify all possible distinct scores that can be obtained from any strictly increasing subsequence from the array. Return the results sorted in ascending order.

Note:

• An empty subsequence is valid and yields a score of 0.

• A subsequence is strictly increasing if each element is strictly greater than the preceding element.

Input Format
  • ▸The first line contains an integer n, the number of elements in arr.
  • ▸The next n lines each contain an integer denoting arr[i].
Output Format
  • ▸Print all distinct score values sorted in ascending order, each on a new line (or return an array of integers sorted in ascending order).
Constraints
  • 1 <= n <= 10^4
  • 1 <= arr[i] < 1024
Sample Test Cases
Example 1
Input
4
4
2
4
1
Output
0
1
2
4
6
Explanation

Strictly increasing subsequences:

• Empty subsequence: score = 0

• [1]: score = 1

• [2]: score = 2

• [4]: score = 4

• [2, 4]: score = 2 | 4 = 6

Sorted distinct scores: [0, 1, 2, 4, 6].

Example 2
Input
4
3
2
4
6
Output
0
2
3
4
6
7
Explanation

Strictly increasing subsequences include empty (0), [2] (2), [3] (3), [4] (4), [6] (6), [2,4] (6), [3,4] (7), [3,6] (7), [3,4,6] (7), etc. Distinct scores: [0, 2, 3, 4, 6, 7].

Complexity
TimeO(N * log(MAX_V) * (MAX_S / 64)) using Bitset Fenwick Tree
SpaceO(MAX_V * MAX_S / 64) auxiliary space
Expected Approach

Maintain reachable bitwise-OR scores for subsequences ending at each element value. Use a Binary Indexed Tree (Fenwick tree) over values 1..1024 to query the union of all reachable scores ending at elements smaller than arr[i] in O(log M) time.

Snowflake Online Assessment Questions | SWE180 | SWE180