Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices.
Your task is to test each device i in order from 0 to n - 1, by performing the following test operations:
batteryPercentages[i] is greater than 0:
j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e, batteryPercentages[j] = max(0, batteryPercentages[j] - 1).Return an integer denoting the number of devices that will be tested after performing the test operations in order.
Example 1:
Input: batteryPercentages = [1,1,2,1,3] Output: 3 Explanation: Performing the test operations in order starting from device 0: At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2]. At device 1, batteryPercentages[1] == 0, so we move to the next device without testing. At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1]. At device 3, batteryPercentages[3] == 0, so we move to the next device without testing. At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same. So, the answer is 3.
Example 2:
Input: batteryPercentages = [0,1,2] Output: 2 Explanation: Performing the test operations in order starting from device 0: At device 0, batteryPercentages[0] == 0, so we move to the next device without testing. At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1]. At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same. So, the answer is 2.
Constraints:
1 <= n == batteryPercentages.length <= 100 0 <= batteryPercentages[i] <= 100Problem summary: You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices. Your task is to test each device i in order from 0 to n - 1, by performing the following test operations: If batteryPercentages[i] is greater than 0: Increment the count of tested devices. Decrease the battery percentage of all devices with indices j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e, batteryPercentages[j] = max(0, batteryPercentages[j] - 1). Move to the next device. Otherwise, move to the next device without performing any test. Return an integer denoting the number of devices that will be tested after performing the test operations in order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,1,2,1,3]
[0,1,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2960: Count Tested Devices After Test Operations
class Solution {
public int countTestedDevices(int[] batteryPercentages) {
int ans = 0;
for (int x : batteryPercentages) {
ans += x > ans ? 1 : 0;
}
return ans;
}
}
// Accepted solution for LeetCode #2960: Count Tested Devices After Test Operations
func countTestedDevices(batteryPercentages []int) (ans int) {
for _, x := range batteryPercentages {
if x > ans {
ans++
}
}
return
}
# Accepted solution for LeetCode #2960: Count Tested Devices After Test Operations
class Solution:
def countTestedDevices(self, batteryPercentages: List[int]) -> int:
ans = 0
for x in batteryPercentages:
ans += x > ans
return ans
// Accepted solution for LeetCode #2960: Count Tested Devices After Test Operations
impl Solution {
pub fn count_tested_devices(battery_percentages: Vec<i32>) -> i32 {
let mut ans = 0;
for x in battery_percentages {
ans += if x > ans { 1 } else { 0 };
}
ans
}
}
// Accepted solution for LeetCode #2960: Count Tested Devices After Test Operations
function countTestedDevices(batteryPercentages: number[]): number {
let ans = 0;
for (const x of batteryPercentages) {
ans += x > ans ? 1 : 0;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.