Find Good Days to Rob the Bank - Problem

You and a gang of thieves are planning on robbing a bank. You are given a 0-indexed integer array security, where security[i] is the number of guards on duty on the ith day. The days are numbered starting from 0.

You are also given an integer time.

The ith day is a good day to rob the bank if:

  • There are at least time days before and after the ith day,
  • The number of guards at the bank for the time days before i are non-increasing, and
  • The number of guards at the bank for the time days after i are non-decreasing.

More formally, this means day i is a good day to rob the bank if and only if security[i - time] >= security[i - time + 1] >= ... >= security[i] <= ... <= security[i + time - 1] <= security[i + time].

Return a list of all days (0-indexed) that are good days to rob the bank. The order that the days are returned in does not matter.

Input & Output

Example 1 — Basic Case
$ Input: security = [5,3,3,3,5,6,2], time = 2
Output: [2,3]
💡 Note: Day 2: security[0:3] = [5,3,3] is non-increasing, security[2:5] = [3,3,5] is non-decreasing. Day 3: security[1:4] = [3,3,3] is non-increasing, security[3:6] = [3,5,6] is non-decreasing.
Example 2 — No Valid Days
$ Input: security = [1,1,1,1,1], time = 0
Output: [0,1,2,3,4]
💡 Note: When time=0, we only need 0 days before and after, so every day is valid since there are no additional constraints to check.
Example 3 — Array Too Small
$ Input: security = [1,2,3,4,5,6], time = 2
Output: []
💡 Note: For time=2, we need at least 2+1+2=5 days, but checking each position: no position satisfies both non-increasing before and non-decreasing after conditions.

Constraints

  • 1 ≤ security.length ≤ 105
  • 0 ≤ time ≤ security.length
  • 1 ≤ security[i] ≤ 105

Visualization

Tap to expand
Find Good Days to Rob the Banksecurity = [5,3,3,3,5,6,2], time = 253335620123456Good days: non-increasing before ↘ AND non-decreasing after ↗Day 2: [5,3] → [3,3,5] ✓Day 3: [3,3] → [3,5,6] ✓Result: [2,3]
Understanding the Visualization
1
Input
Security array [5,3,3,3,5,6,2] with time=2
2
Check Pattern
Find days with non-increasing before and non-decreasing after
3
Output
Return indices [2,3] of valid robbery days
Key Takeaway
🎯 Key Insight: Use prefix arrays to pre-compute sequence patterns for O(1) validation per day
Asked in
Amazon 15 Facebook 8
18.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen