Given an array A consisting of positive integers. You have to convert each integer number in the array to some prime number.
In each step, you can select any index of the array and decrease or increase the array element present at the selected index by 1.
Find the minimum number of steps needed to convert each element of the array to some prime number.
Complete the solve function. The function takes the following 2 parameters and returns the minimum number of steps required to convert all array elements to prime numbers:
Note: Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code.
Print an integer representing the minimum number of steps to convert each element of the array to some prime number.
4 9 5 5 6
3
In sample, two 5s are already primes. You don't need to change them, for 9, you can either make 7 or 11, both takes 2 steps. And, if you can change 6 to either 5 or 7, both takes 1 step. So, as a whole it takes 3 steps to make all elements prime.
Hackerrank • Pending