Smallest prime giving remainder K when divided by any Array element
Last Updated :
12 Jun, 2022
Given an array of integers arr[] of size N and an integer K, the task is to find the smallest prime such that it gives remainder K when it is divided by any of the elements of the array.
Note: The prime number should be in the range [1, 106]
Examples:
Input: arr[]= {3, 4, 5}, K = 1
Output: 61
Explanation: The smallest prime that satisfies the condition is 61.
61%3 = 1, 61%4 = 1 and 61%5 =1.
Input: arr[] = {2, 4, 5}, K = 3
Output: -1
Explanation: The output should not be possible because
no number can have remainder 3 when divided by 2.
Input: arr[] = {10, 4, 5}, K = 3
Output: 23
Explanation: The smallest prime that satisfies the condition is 23.
23%10 = 3, 23%4 = 3 and 23%5 = 3
Approach: The idea to solve the problem is mentioned below:
If any element of the array is smaller than K then solution is not possible. Because no number can have a remainder higher than itself when dividing a number.
The LCM of numbers is the smallest number divisible by all the other numbers. So find the LCM of the array and check for each multiple of the LCM whether LCM + K is prime or not to get the smallest prime which gives the remainder as K.
Follow the below steps for the above:
- First check,If minimum element of the array is less than k {ie; min(arr)>=K}, then solution is not possible, then return -1
- Find the lcm of all the elements of the array(say lcm).
- Iterate for each multiple of lcm which are less than or equal to 106:
- Check whether (lcm + K) is prime or not for each multiple of lcm
- If the condition holds true, return the sum of K and the current multiple of lcm as the smallest_prime.
- If no prime number is found, return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long gcd( long long int a,
long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
bool checkPrime( long long n)
{
long long ub = sqrt (n);
for ( long long i = 2; i <= ub; i++) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
long long smallestPrime(vector< int > arr,
int q)
{
long long lcm = arr[0];
for ( int i : arr) {
lcm = (lcm * i) / (gcd(i, lcm));
}
for ( auto i : arr) {
if (i < q)
return -1;
}
for ( long long i = lcm; i <= 1e9;
i += lcm) {
if (checkPrime(i + q)) {
return i + q;
}
}
return -1;
}
int main()
{
vector< int > arr = { 2, 5, 4 };
int q = 3;
cout << smallestPrime(arr, q);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static long gcd( long a, long b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static boolean checkPrime( long n)
{
long ub = ( long )Math.sqrt(n);
for ( int i = 2 ; i <= ub; i++) {
if (n % i == 0 ) {
return false ;
}
}
return true ;
}
public static long smallestPrime( int arr[], int q)
{
long lcm = ( long )arr[ 0 ];
for ( int i : arr) {
lcm =( long )(lcm * ( long )i) / ( long )(gcd(( long )i,( long )lcm));
}
for ( int i : arr) {
if (i < q)
return - 1 ;
}
for ( long i = lcm; i <= 1e9; i += lcm) {
if (checkPrime(i + q)) {
return i + q;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 5 , 4 };
int q = 3 ;
System.out.print(smallestPrime(arr, q));
}
}
|
Python3
import math
def gcd( a, b):
if b = = 0 :
return a;
return gcd(b, a % b);
def checkPrime(n):
ub = math.sqrt(n);
for i in range ( 2 ,ub):
if (n % i = = 0 ):
return 0 ;
return 1 ;
def smallestPrime(arr, q):
lcm = arr[ 0 ];
for i in range ( len (arr)):
lcm = (lcm * i) / (gcd(arr[i], lcm));
for i in range ( len (arr)):
if (arr[i] < q):
return - 1 ;
for i in range (lcm, 1e9 , lcm):
if (checkPrime(i + q)):
return i + q;
return - 1 ;
arr = [ 2 , 5 , 4 ];
q = 3 ;
print (smallestPrime(arr, q));
|
C#
using System;
class GFG {
static long gcd( long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static bool checkPrime( long n)
{
long ub = ( long )Math.Sqrt(n);
for ( int i = 2; i <= ub; i++) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
static long smallestPrime( int []arr, int q)
{
long lcm = ( long )arr[0];
for ( int i = 0; i < arr.Length; i++) {
lcm =( long )(lcm * ( long )arr[i]) / ( long )(gcd(( long )arr[i],( long )lcm));
}
for ( int i = 0; i < arr.Length; i++) {
if (arr[i] < q)
return -1;
}
for ( long i = lcm; i <= 1e9; i += lcm) {
if (checkPrime(i + q)) {
return i + q;
}
}
return -1;
}
public static void Main()
{
int []arr = { 2, 5, 4 };
int q = 3;
Console.Write(smallestPrime(arr, q));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function checkPrime(n)
{
ub = Math.Sqrt(n);
for ( var i = 2; i <= ub; i++) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
function smallestPrime(arr, q)
{
var lcm = arr[0];
for (const i of arr) {
lcm = (lcm * i) / (gcd(i, lcm));
}
for (const i of arr) {
if (i < q)
return -1;
}
for ( var i = lcm; i <= 1000000000; i += lcm) {
if (checkPrime(i + q)) {
return i + q;
}
}
return -1;
}
var arr = [ 2, 5, 4 ];
var q = 3;
document.write(smallestPrime(arr, q));
</script>
|
Time Complexity: O(N * logD + (M/lcm)*sqrt(M)) where D is max of array, M is upper possible limit of prime, lcm is LCM of all array elements
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...