Skip to content
Related Articles

Related Articles

Improve Article

Smallest positive integer that does not divide any elements of the given array

  • Last Updated : 08 Jun, 2021

Given an array arr[] consisting of N positive integers, the task is to determine the smallest positive integer K such that none of the array element is divisible by K. If no such integer is

Examples:

Input: arr[] = {3, 2, 6, 9, 2}
Output: 4
Explanation: None of the array elements is divisible by 4(the smallest positive).

Input: arr[] = {3, 5, 1, 19, 11}
Output: 2

Approach: Follow the steps below to solve the problem:



  • Find the maximum element of the given array, say maxE.
  • Iterate over the range [1, maxE + 1] using the variable i and check if there is an integer in the given array which is divisible by i or not. If found to be true, then check for the next integer in this range.
  • Otherwise, print the current number i.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to find the smallest number
// which doesn't divides any integer in
// the given array arr[]
void smallestNumber(int arr[], int len)
{
    int maxi = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < len; i++) {
 
        // Maximum array element
        maxi = std::max(maxi, arr[i]);
    }
 
    // Initialize variable
    int ans = -1;
 
    // Traverse from 2 to max
    for (int i = 2; i < maxi + 2; i++) {
 
        // Stores if any such
        // integer is found or not
        bool flag = true;
 
        for (int j = 0; j < len; j++) {
 
            // If any array element
            // is divisible by j
            if (arr[j] % i == 0) {
 
                flag = false;
                break;
            }
        }
 
        if (flag) {
 
            // Smallest integer
            ans = i;
            break;
        }
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 6, 9, 2 };
    int N = sizeof(arr)
/ sizeof(arr[0]);
 
    // Function Call
    smallestNumber(arr, N);
 
    return 0;
}

Java




// Java program for the above approach
class GFG
{
 
// Function to find the smallest number
// which doesn't divides any integer in
// the given array arr[]
static void smallestNumber(int arr[], int len)
{
    int maxi = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < len; i++)
    {
 
        // Maximum array element
        maxi = Math.max(maxi, arr[i]);
    }
 
    // Initialize variable
    int ans = -1;
 
    // Traverse from 2 to max
    for (int i = 2; i < maxi + 2; i++)
    {
 
        // Stores if any such
        // integer is found or not
        boolean flag = true;
        for (int j = 0; j < len; j++)
        {
 
            // If any array element
            // is divisible by j
            if (arr[j] % i == 0)
            {
                flag = false;
                break;
            }
        }
 
        if (flag)
        {
 
            // Smallest integer
            ans = i;
            break;
        }
    }
 
    // Print the answer
    System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 2, 6, 9, 2 };
    int N = arr.length;
 
    // Function Call
    smallestNumber(arr, N);
}
}
 
// This code is contributed by shikhasingrajput

Python3




# Python3 program for the above approach
 
# Function to find the smallest number
# which doesn't divides any integer in
# the given array arr[]
def smallestNumber(arr, lenn):
    maxi = 0
 
    # Traverse the array arr[]
    for i in range(lenn):
 
        # Maximum array element
        maxi = max(maxi, arr[i])
 
    # Initialize variable
    ans = -1
 
    # Traverse from 2 to max
    for i in range(2, maxi + 2):
 
        # Stores if any such
        # integer is found or not
        flag = True
        for j in range(lenn):
 
            # If any array element
            # is divisible by j
            if (arr[j] % i == 0):
                flag = False
                break
        if (flag):
 
            # Smallest integer
            ans = i
            break
 
    # Prthe answer
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    arr = [3, 2, 6, 9, 2]
    N = len(arr)
 
    #Function Call
    smallestNumber(arr, N)
 
# This code is contributed by mohit kumar 29.

C#




// C# program for the above approach
using System;
public class GFG
{
 
  // Function to find the smallest number
  // which doesn't divides any integer in
  // the given array arr[]
  static void smallestNumber(int []arr, int len)
  {
    int maxi = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < len; i++)
    {
 
      // Maximum array element
      maxi = Math.Max(maxi, arr[i]);
    }
 
    // Initialize variable
    int ans = -1;
 
    // Traverse from 2 to max
    for (int i = 2; i < maxi + 2; i++)
    {
 
      // Stores if any such
      // integer is found or not
      bool flag = true;
      for (int j = 0; j < len; j++)
      {
 
        // If any array element
        // is divisible by j
        if (arr[j] % i == 0)
        {
          flag = false;
          break;
        }
      }
      if (flag)
      {
 
        // Smallest integer
        ans = i;
        break;
      }
    }
 
    // Print the answer
    Console.WriteLine(ans);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int []arr = { 3, 2, 6, 9, 2 };
    int N = arr.Length;
 
    // Function Call
    smallestNumber(arr, N);
  }
}
 
// This code is contributed by AnkThon

Javascript




<script>
// javascript program of the above approach
 
// Function to find the smallest number
// which doesn't divides any leteger in
// the given array arr[]
function smallestNumber(arr, len)
{
    let maxi = 0;
 
    // Traverse the array arr[]
    for (let i = 0; i < len; i++)
    {
 
        // Maximum array element
        maxi = Math.max(maxi, arr[i]);
    }
 
    // Initialize variable
    let ans = -1;
 
    // Traverse from 2 to max
    for (let i = 2; i < maxi + 2; i++)
    {
 
        // Stores if any such
        // leteger is found or not
        let flag = true;
        for (let j = 0; j < len; j++)
        {
 
            // If any array element
            // is divisible by j
            if (arr[j] % i == 0)
            {
                flag = false;
                break;
            }
        }
 
        if (flag)
        {
 
            // Smallest leteger
            ans = i;
            break;
        }
    }
 
    // Prlet the answer
    document.write(ans);
}
 
    // Driver Code
     
    let arr = [ 3, 2, 6, 9, 2 ];
    let N = arr.length;
 
    // Function Call
    smallestNumber(arr, N);
 
</script>
Output: 
4

 

Time Complexity: O(N*max) where max is the maximum element in the given array
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :