#2960
Easy Algorithms Count tested devices after test operations
Array Simulation Counting
78.8% acceptance
Feb 25, 2026
176
18
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.
Return an integer denoting the number of devices that will be tested.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn count_tested_devices(battery_percentages: Vec<i32>) -> i32 {
// Each device i is tested if batteryPercentages[i] > (number of tested devices before i)
let mut tested = 0i32;
for &b in &battery_percentages {
if b > tested {
tested += 1;
}
}
tested
}
}