Smallest positive integer that does not divide any elements of the given array | C ++ program for the above approach ; Function to find the smallest number which doesn 't divides any integer in the given array arr[] ; Traverse the array arr [ ] ; Maximum array element ; Initialize variable ; Traverse from 2 to max ; Stores if any such integer is found or not ; If any array element is divisible by j ; Smallest integer ; Print the answer ; Driver Code ; Function Call
#include <iostream> NEW_LINE 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 ; }