Open In App

Smallest prime giving remainder K when divided by any Array element

Last Updated : 12 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the
// gcd of two integers
long long gcd(long long int a,
              long long int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
// Function to check if an integer
// is prime or not
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;
}
 
// Function to find the smallest prime
// that gives the same remainder when
// divided by any of the element in arr.
long long smallestPrime(vector<int> arr,
                        int q)
{
 
    // Finding the lcm of the array
    long long lcm = arr[0];
    for (int i : arr) {
        lcm = (lcm * i) / (gcd(i, lcm));
    }
 
    // Edge case, if the value of any
    // in the array is less than remainder
    for (auto i : arr) {
        if (i < q)
            return -1;
    }
    // Check whether (lcm + remainder) is
    // prime or not, for all multiples of lcm
    for (long long i = lcm; i <= 1e9;
         i += lcm) {
        if (checkPrime(i + q)) {
            return i + q;
        }
    }
 
    // If any prime satisfying the
    // condition is not found
    // simply return -1;
    return -1;
}
 
// Driver code
int main()
{
 
    vector<int> arr = { 2, 5, 4 };
    int q = 3;
    cout << smallestPrime(arr, q);
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG {
 
  // Function to find the
  // gcd of two integers
  public static long gcd(long a, long b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Function to check if an integer
  // is prime or not
  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;
  }
 
  // Function to find the smallest prime
  // that gives the same remainder when
  // divided by any of the element in arr.
  public static long smallestPrime(int arr[], int q)
  {
 
    // Finding the lcm of the array
    long lcm = (long)arr[0];
    for (int i : arr) {
      lcm =(long)(lcm * (long)i) / (long)(gcd((long)i,(long)lcm));
    }
 
    // Edge case, if the value of any
    // in the array is less than remainder
    for (int i : arr) {
      if (i < q)
        return -1;
    }
    // Check whether (lcm + remainder) is
    // prime or not, for all multiples of lcm
    for (long i = lcm; i <= 1e9; i += lcm) {
      if (checkPrime(i + q)) {
        return i + q;
      }
    }
 
    // If any prime satisfying the
    // condition is not found
    // simply return -1;
    return -1;
  }
 
  public static void main(String[] args)
  {
    int arr[] = { 2, 5, 4 };
    int q = 3;
    System.out.print(smallestPrime(arr, q));
  }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python code for the above approach
import math
 
# Function to find the
# gcd of two integers
def gcd( a, b):
 
    if b == 0:
        return a;
    return gcd(b, a % b);
 
# Function to check if an integer
# is prime or not
def checkPrime(n):
 
    ub = math.sqrt(n);
    for i in range(2,ub):
        if (n % i == 0):
            return 0;
    return 1;
 
# Function to find the smallest prime
# that gives the same remainder when
# divided by any of the element in arr.
def smallestPrime(arr, q):
 
    # Finding the lcm of the array
    lcm = arr[0];
    for i in range(len(arr)):
        lcm = (lcm * i) / (gcd(arr[i], lcm));
     
    # Edge case, if the value of any
    # in the array is less than remainder
    for i in range(len(arr)):
        if (arr[i] < q):
            return -1;
     
    # Check whether (lcm + remainder) is
    # prime or not, for all multiples of lcm
    for i in range(lcm, 1e9, lcm):
        if (checkPrime(i + q)):
            return i + q;
         
    # If any prime satisfying the
    # condition is not found
    # simply return -1;
    return -1;
 
# Driver code
arr = [2, 5, 4];
q = 3;
print(smallestPrime(arr, q));
   
# This code is contributed by Potta Lokesh


C#




// C# code for the above approach
using System;
 
class GFG {
 
  // Function to find the
  // gcd of two integers
  static long gcd(long a, long b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Function to check if an integer
  // is prime or not
  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;
  }
 
  // Function to find the smallest prime
  // that gives the same remainder when
  // divided by any of the element in arr.
  static long smallestPrime(int []arr, int q)
  {
 
    // Finding the lcm of the array
    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));
    }
 
    // Edge case, if the value of any
    // in the array is less than remainder
    for (int i = 0; i < arr.Length; i++) {
      if (arr[i] < q)
        return -1;
    }
    // Check whether (lcm + remainder) is
    // prime or not, for all multiples of lcm
    for (long i = lcm; i <= 1e9; i += lcm) {
      if (checkPrime(i + q)) {
        return i + q;
      }
    }
 
    // If any prime satisfying the
    // condition is not found
    // simply return -1;
    return -1;
  }
 
  public static void Main()
  {
    int []arr = { 2, 5, 4 };
    int q = 3;
    Console.Write(smallestPrime(arr, q));
  }
}
 
// This code is contributed by Samim Hossain Mondal


Javascript




<script>
// JavaScript code for the above approach
 
// Function to find the
// gcd of two integers
function gcd(a, b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
// Function to check if an integer
// is prime or not
function checkPrime(n)
{
    ub = Math.Sqrt(n);
    for (var i = 2; i <= ub; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
 
// Function to find the smallest prime
// that gives the same remainder when
// divided by any of the element in arr.
function smallestPrime(arr,  q)
{
 
    // Finding the lcm of the array
    var lcm = arr[0];
    for (const i of arr) {
        lcm = (lcm * i) / (gcd(i, lcm));
    }
 
    // Edge case, if the value of any
    // in the array is less than remainder
    for (const i of arr) {
        if (i < q)
            return -1;
    }
    // Check whether (lcm + remainder) is
    // prime or not, for all multiples of lcm
    for (var i = lcm; i <= 1000000000; i += lcm) {
        if (checkPrime(i + q)) {
            return i + q;
        }
    }
 
    // If any prime satisfying the
    // condition is not found
    // simply return -1;
    return -1;
}
 
// Driver code
var arr = [ 2, 5, 4 ];
var q = 3;
document.write(smallestPrime(arr, q));
 
// This code is contributed by phasing17
</script>


Output

-1

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

Similar Reads