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
ithat you haven't picked before, and pick a primepstrictly less thannums[i], then subtractpfromnums[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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code