Prime Subtraction Operation - Problem

You are given a 0-indexed integer array nums of length n. Your goal is to transform this array into a strictly increasing array using a special operation.

The Operation: You can pick any index i (that you haven't picked before) and subtract a prime number p from nums[i], where p < nums[i]. You can perform this operation as many times as needed.

Your Task: Return true if you can make the array strictly increasing, false otherwise.

A strictly increasing array means each element is greater than the previous element: nums[0] < nums[1] < nums[2] < ... < nums[n-1]

Example: For array [4, 9, 6, 10], you could subtract prime 2 from nums[1] to get [4, 7, 6, 10], then subtract prime 3 from nums[2] to get [4, 7, 3, 10]. But this won't work since 7 > 3. The key insight is working backwards!

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [4, 9, 6, 10]
โ€บ Output: true
๐Ÿ’ก Note: Work backwards: 6 โ‰ฅ 10? No. 9 โ‰ฅ 6? Yes, subtract prime 7: 9-7=2. 4 โ‰ฅ 2? Yes, but no prime < 4 makes 4-p < 2, but since 4 already needs to be < 2 which is impossible... Actually, let's be more careful: after making 9 โ†’ 2, we have [4,2,6,10]. Now 4 โ‰ฅ 2, so we need 4-p < 2, so p > 2. We can use p=3, giving us [1,2,6,10] which is strictly increasing.
example_2.py โ€” Already Increasing
$ Input: nums = [1, 3, 5, 7]
โ€บ Output: true
๐Ÿ’ก Note: The array is already strictly increasing, so no operations are needed. We can return true immediately.
example_3.py โ€” Impossible Case
$ Input: nums = [2, 2]
โ€บ Output: false
๐Ÿ’ก Note: We have 2 โ‰ฅ 2, so we need to subtract a prime from the first element. The only prime less than 2 is... none! There are no primes less than 2, so we cannot make the array strictly increasing.

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 1 โ‰ค nums[i] โ‰ค 1000
  • You can use each index at most once
  • Prime p must be strictly less than nums[i]

Visualization

Tap to expand
Tower Height Adjustment Strategy49610Work Backwards36-3=3 < 10 โœ“29-7=2 < 3 โœ“๐ŸŽฏ Key Insights:โ€ข Work backwards for optimal decision makingโ€ข Greedy: use largest valid prime reductionโ€ข Smaller elements give more future flexibilityโ€ข Single pass = O(n) time
Understanding the Visualization
1
Scan Right to Left
Start from the rightmost tower and work backwards
2
Check Height Constraint
If current tower is too tall (โ‰ฅ next tower), it needs adjustment
3
Apply Maximum Prime Reduction
Subtract the largest prime possible while keeping tower shorter than next
4
Verify Feasibility
If no prime reduction works, the task is impossible
Key Takeaway
๐ŸŽฏ Key Insight: Working backwards with greedy prime selection ensures each element becomes as small as possible, maximizing flexibility for subsequent decisions while maintaining the strictly increasing property.
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
31.2K Views
Medium Frequency
~25 min Avg. Time
847 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