Given an integer array arr[] consisting of N integers, the task is to find the nearest Prime Number in the array for every element in the array. If the array does not contain any prime number, then print -1.
Examples:
_Input: _arr[] = {1, 2, 3, 1, 6}
Output:_ 2 2 3 3 3_
Explanation:
For the subarray {1, 2}, the nearest prime number is 2.
For the subarray {3, 1, 6}, the nearest prime number is 3.
Input:_ arr[] = {8, 7, 12, 15, 3, 11}_
Output:_ 7 7 7 3 3 11_
Explanation:
For the subarray {8, 7, 12}, the nearest prime number is 7.
For the subarray {15, 3}, the nearest prime number is 3.
For the subarray {11}, the nearest prime number is 11 itself.
Approach:
Follow the steps below to solve the problem:
#arrays #competitive programming #mathematical #hash #prime number #sieve