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 elements is divisible by K.
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++
#include <iostream>
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 << ans;
}
int main()
{
int arr[] = { 3, 2, 6, 9, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
smallestNumber(arr, N);
return 0;
}
|
Java
class GFG
{
static void smallestNumber( int arr[], int len)
{
int maxi = 0 ;
for ( int i = 0 ; i < len; i++)
{
maxi = Math.max(maxi, arr[i]);
}
int ans = - 1 ;
for ( int i = 2 ; i < maxi + 2 ; i++)
{
boolean flag = true ;
for ( int j = 0 ; j < len; j++)
{
if (arr[j] % i == 0 )
{
flag = false ;
break ;
}
}
if (flag)
{
ans = i;
break ;
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 6 , 9 , 2 };
int N = arr.length;
smallestNumber(arr, N);
}
}
|
Python3
def smallestNumber(arr, lenn):
maxi = 0
for i in range (lenn):
maxi = max (maxi, arr[i])
ans = - 1
for i in range ( 2 , maxi + 2 ):
flag = True
for j in range (lenn):
if (arr[j] % i = = 0 ):
flag = False
break
if (flag):
ans = i
break
print (ans)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 6 , 9 , 2 ]
N = len (arr)
smallestNumber(arr, N)
|
C#
using System;
public class GFG
{
static void smallestNumber( int []arr, int len)
{
int maxi = 0;
for ( int i = 0; i < len; i++)
{
maxi = Math.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 ;
}
}
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
int []arr = { 3, 2, 6, 9, 2 };
int N = arr.Length;
smallestNumber(arr, N);
}
}
|
Javascript
<script>
function smallestNumber(arr, len)
{
let maxi = 0;
for (let i = 0; i < len; i++)
{
maxi = Math.max(maxi, arr[i]);
}
let ans = -1;
for (let i = 2; i < maxi + 2; i++)
{
let flag = true ;
for (let j = 0; j < len; j++)
{
if (arr[j] % i == 0)
{
flag = false ;
break ;
}
}
if (flag)
{
ans = i;
break ;
}
}
document.write(ans);
}
let arr = [ 3, 2, 6, 9, 2 ];
let N = arr.length;
smallestNumber(arr, N);
</script>
|
Time Complexity: O(N*max) where max is the maximum element in the given array
Auxiliary Space: O(1) as it is using constant space for variables