Maximize Partitions
Updated
Problem statement
There are n components. The performance level of component i is performance[i], for i from 0 to n - 1.
A partition (l, r) is the group of components from index l to index r. Its bottleneck level is the bitwise AND of all its values:
f(l, r) = performance[l] & performance[l+1] & ... & performance[r].
The engineers split all components into partitions. Each partition holds components that sit next to each other, and each component belongs to exactly one partition. They want two things, in this order:
- The total bottleneck level (the sum of
f(l, r)over all partitions) must be as small as possible. - Among all splits with that smallest total, the number of partitions must be as large as possible.
For example, [1, 2, 3] could be split into [1, 2] and [3], with levels 0 and 3, for a total of 3. Keeping it whole gives 1 & 2 & 3 = 0, which is smaller. So the answer is 1.
Return the largest number of partitions.
Function
maximizePartitions(performance: int[]) → int
Complete the function maximizePartitions in the editor.
maximizePartitions has the following parameter:
int performance[n]: the performance level of each component
Returns
int: the largest number of partitions with the smallest total bottleneck level
Examples
Example 1
performance = [5, 2, 6, 1, 1, 4]return = 3Split into (0, 1), (2, 3) and (4, 5). Their levels are 5 & 2 = 0, 6 & 1 = 0 and 1 & 4 = 0. The total is 0, the smallest any split can have. Every value is at least 1, so a partition with only one value has a level of at least 1. Four or more partitions of six values would need such a partition, and the total would no longer be 0. The answer is 3.
Constraints
1 <= n <= 10^5, wherenis the length ofperformance1 <= performance[i] <= 10^6
More Amazon interview questions
- Lexicographically Largest IdentifierOA · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fill One Exact-Length Binary WindowOA · Seen Nov 2026
Similar questions at other companies
- Minimize the Maximum Array ElementChubb · OAChubb interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Server SelectionPoint72 · OAPoint72 interview questions
- Minimum Servers for an Exact LoadJP Morgan Chase · OAJP Morgan Chase interview questions
- Get Max Or SumMicrosoft · OAMicrosoft interview questions
- Fewest Steps to Build an Array from ZerosExpedia · OAExpedia interview questions
- Maximum XOR Elimination ScoreRippling · OARippling interview questions
- Minimum Cost for a Target WeightPace Stock Broking Services · PHONE SCREEN
- Minimum Row Deletions to Prevent a Target ORInMobi · ONSITE INTERVIEWInMobi interview questions
- Earn the Most 🐝Capital One · OACapital One interview questions