AmazonOA #265
Maximize Partitions
Estimated MediumJudge-ready
Topics (AI-labelled)Bits & MathGreedy
Cut the whole non-negative performance array into non-empty contiguous segments. The cost of a segment is the bitwise AND of all its numbers, and the total cost is the sum of the segment costs. First minimize the total cost, then, under that condition, maximize the number of segments; output the number of segments.
Input
The first line contains n; the second line contains performance. The original text had no numeric bounds; this site uses 1≤n≤200000, 0≤performance[i]≤10^9.
Output
Output a single integer, the answer.
Example 1
Input:
3
1 2 3
Output:
1
Example 2
Input:
3
7 7 7
Output:
1
Example 3
Input:
3
0 0 0
Output:
3
Example notes
The answers are 1, 1 and 3. In the first example the AND of the first two items is already 0, and the final 3 must be merged into that segment; it cannot start a new segment with positive cost.
Editorial
Loading solution…