Ctrl K

Shave Primes Into Order

Medium
You are given an array of positive integers values. Each element may be "shaved" at most once: pick a prime p strictly less than that element and subtract it. Can you shave some of the elements (possibly none) so that the array becomes **strictly increasing**? Return True or False. A prime is greater than 1 and divisible only by 1 and itself.
Example 1
values=[10, 6, 9, 7] → True

Subtract 7 from 10 → 3, 2 from 6 → 4, 3 from 9 → 6, and keep 7: [3, 4, 6, 7].

Example 2
values=[4, 4, 2] → False

The last element is at most 2, so the middle one would have to become 1 and the first one less than 1, which is impossible.

Example 3
values=[1, 3, 8] → True

The array is already strictly increasing, so nothing needs to be subtracted.

Constraints
  • 1 ≤ len(values) ≤ 1000
  • 1 ≤ values[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPrices That Fade With Each Sale MediumEver-Growing Teams MediumChunks With a Common Divisor MediumEqual Window Sums on a Ring MediumLevel Out to a Palindrome Medium
loading Python…
Input
values=[10, 6, 9, 7]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 160 tests on the server, hidden ones included.