Problem Description
Given a 0-indexed integer array nums, you may perform the following operation at most once per index: choose an index i (that you haven’t picked before) and subtract from nums[i] any prime number p that is strictly less than nums[i]. Determine if it is possible to perform these operations (in any order) so that the resulting array is strictly increasing.
Key Insights
- For each element, the set of possible new values is {nums[i]} ∪ {nums[i] - p for every prime p < nums[i]}.
- We need to form a strictly increasing sequence; hence, if we denote the chosen value for index i as a_i, then a0 < a1 < ... < a_(n-1).
- A greedy strategy works: Process the array from left to right. For each element, choose the smallest possible value (from its available options) that is strictly greater than the previously chosen value. This minimizes the current value and gives maximum flexibility for subsequent choices.
- Precompute the list of primes up to 1000 (since nums[i] <= 1000) so that for any nums[i] you can quickly enumerate valid subtracted outcomes.
Space and Time Complexity
Time Complexity: O(n * k) where n is the number of elements (up to 1000) and k is the number of primes up to 1000 (roughly up to 168).
Space Complexity: O(n + P) where P is the number of primes computed (negligible compared to n).
Solution
We first precompute all primes up to the maximum possible value (1000) using a sieve algorithm. For each number in nums, compute its candidate outcomes: either leave it unchanged or subtract any prime p (p < nums[i]). Then iterate from left to right with a variable (prev) that stores the last chosen value. For each nums[i], take its candidate set, sort it in increasing order, and choose the smallest candidate that is strictly greater than prev. If no such candidate exists, then it is impossible to construct a strictly increasing sequence. Otherwise, update prev and continue. Finally, if all elements are assigned a valid new value, return true.