Minimum Operations to Transform Array into Alternating Prime
Real-world version: Nudging Readings onto Prime ValuesThe statement here is our own: the same inputs, outputs and technique.
A gauge log is given as nums. One nudge raises or lowers a single reading by one, and a reading may be nudged as often as needed but must stay at least 2.
The log is clean when every reading is a prime number. Return the fewest nudges that leave the log clean.
Examples
Example 1
- Input
- nums = [8, 9, 10]
- Output
- 4
The reading 8 sits one away from 7, the reading 9 sits two away from 7 and from 11, and the reading 10 sits one away from 11, giving 1 + 2 + 1 nudges.
Example 2
- Input
- nums = [1]
- Output
- 1
A reading may not drop below 2, and 1 is not prime, so the single nudge raises it to 2.
Example 3
- Input
- nums = [24, 25, 26, 27, 28]
- Output
- 9
The nearest primes are 23 for both 24 and 25, then 29 for 28, and 26 and 27 each sit three away from the nearer of 23 and 29.
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 100000
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def nudges_to_clean(nums: list[int]) -> int:public long nudgesToClean(int[] nums)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Sum of GCD of Formed PairsReal-world version: Total Mesh Cycle of the Gear BinMedium
- Intersection of Two ArraysReal-world version: Shared Scan CodesEasy
- Missing NumberReal-world version: Unclaimed Badge NumberEasy
- Earliest Finish Time for Land and Water Rides IReal-world version: Forge and Kiln BookingEasy