Prime Subtraction Operation - Problem

You are given a 0-indexed integer array nums of length n.

You can perform the following operation as many times as you want:

  • Pick an index i that you haven't picked before, and pick a prime p strictly less than nums[i], then subtract p from nums[i].

Return true if you can make nums a strictly increasing array using the above operation and false otherwise.

A strictly increasing array is an array whose each element is strictly greater than its preceding element.

Input & Output

Example 1 — Possible Case
$ Input: nums = [4,9,6,10]
Output: true
💡 Note: We can subtract prime 2 from 4 to get [2,9,6,10], then subtract prime 7 from 9 to get [2,2,6,10], but 2=2 violates strictly increasing. Actually, we need different approach: subtract 2 from 4→2, subtract 5 from 6→1, but then 9>1 so subtract more from 9. The greedy approach working right-to-left would determine feasibility.
Example 2 — Already Increasing
$ Input: nums = [6,8,11,12]
Output: true
💡 Note: Array is already strictly increasing: 6 < 8 < 11 < 12, so no operations needed.
Example 3 — Impossible Case
$ Input: nums = [5,8,3]
Output: false
💡 Note: We need 5 < something < 3, but after subtracting any prime from 8, we cannot make 8 less than 3 while keeping 5 less than the result.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Prime Subtraction Operation: Make Array Strictly IncreasingInput: [4, 9, 6, 10] - Can we subtract primes to make it strictly increasing?49610Problem: 9 > 6Available primes to subtract: 2, 3, 5, 7, 11, ...Strategy: Work right-to-left, minimize each element6 - 5 = 19 must be < 1?Impossible!Result: false - Cannot make strictly increasing
Understanding the Visualization
1
Input Array
[4,9,6,10] - not strictly increasing
2
Prime Operations
Subtract primes to fix ordering: work right-to-left
3
Result
Determine if strictly increasing arrangement possible
Key Takeaway
🎯 Key Insight: Work backwards through the array, making each element as small as possible while maintaining order
Asked in
Google 15 Amazon 12 Microsoft 8
12.4K Views
Medium Frequency
~25 min Avg. Time
287 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