
Battery Optimization Mode – A Custom Sliding Window Problem(Real World Example)
Problem Smart glasses run heavy processing tasks like real-time navigation or object detection.Each minute the processor consumes some power. We are given: An array power_drain each value represents power consumption per minute (mW).A power_budget which is the maximum safe power usage. Our goal: -> Find the maximum number of consecutive minutes the glasses can run without exceeding the power budget. Example Input power_drain = [3, 1, 2, 7, 4, 2, 1, 1, 5] power_budget = 8 Output 4 Explanation The longest valid subarray is: [4, 2, 1, 1] Sum = 8 Length = 4 minutes Approach — Variable Sliding Window We use two pointers: left → start of window right → end of window Steps : Expand the window by moving right Add the current power consumption If the total exceeds the budget, shrink from left Track the maximum window length This technique avoids recalculating sums repeatedly. C++ Implementation #include <iostream> #include <vector> using namespace std ; int main () { vector < int > power_drain
Continue reading on Dev.to Beginners
Opens in a new tab



