Saturday, 16 Oct 2021

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

Smallest positive integer that does not divide any elements of the given array
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 not divisible by K. If no such integer is
Examples:

Input: arr[] = {3, 2, 6, 9, 2}Output: 4Explanation: None of the array elements is divisible by 4( 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 variable i and check if there is any 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++

#include
using namespace std;

void smallestNumber(int arr[], int len)
{
int maxi = 0;

for (int i = 0; i < len; i++) {                     maxi = std::max(maxi, arr[i]);     }             int ans = -1;             for (int i = 2; i < maxi + 2; i++) {                              bool flag = true;            for (int j = 0; j < len; j++) {                                          if (arr[j] % i == 0) {                    flag = false;                 break;             }         }            if (flag) {                             ans = i;             break;         }     }             cout