Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Given an array of integers arr[] of dimension N and an integer Ok, the duty is to search out the smallest prime such that it offers the rest Ok when it’s divided by any of the weather of the array.
Word: The prime quantity must be within the vary [1, 106]
Examples:
Enter: arr[]= {3, 4, 5}, Ok = 1
Output: 61
Rationalization: The smallest prime that satisfies the situation is 61.
61percent3 = 1, 61percent4 = 1 and 61percent5 =1.Enter: arr[] = {2, 4, 5}, Ok = 3
Output: -1
Rationalization: The output shouldn’t be doable as a result of
no quantity can have the rest 3 when divided by 2.Enter: arr[] = {10, 4, 5}, Ok = 3
Output: 23
Rationalization: The smallest prime that satisfies the situation is 23.
23percent10 = 3, 23percent4 = 3 and 23percent5 = 3
Method: The thought to unravel the issue is talked about beneath:
The LCM of numbers is the smallest nummber divisble by all the opposite numbers. So discover the LCM of the array and examine for every a number of of the LCM whether or not LCM + Ok is prime or to not get the smallest prime which provides the rest as Ok.
If any component of the array is smaller than Ok then resolution isn’t doable. As a result of no quantity can have a reaminder increased than itself when dividing a quantity.
Comply with the beneath steps for the above:
Under is the implementation of the above strategy:
|
Time Complexity: O(N * logD + (M/lcm)*sqrt(M)) the place D is max of array, M is higher doable restrict of prime, lcm is LCM of all array components
Auxiliary Area: O(1)
[ad_2]