Smallest integer > 1 which divides every element of the given array
Given an array arr[], the task is to find the smallest possible integer (other than 1) which divides every element of the given array.
Examples:
Input: arr[] = { 2, 4, 8 }
Output: 2
2 is the smallest possible number which divides the whole array.
Input: arr[] = { 4, 7, 5 }
Output: -1
There’s no integer possible which divides the whole array other than 1.
Approach: We know that the GCD of the whole array will be the greatest integer that will divide every element of the array. If GCD = 1 then there’s no integer possible that divides the whole array. However, if GCD > 1 then there exists integer(s) which divides the array completely. For example,
If GCD = 36 then
36 divides the whole array.
18 divides the whole array.
12 divides the whole array.
9 divides the whole array.
…
1 divides the whole array.
Thus, we see that all factors of 36 also divide the array. The smallest prime factor of 36 i.e. 2 is the smallest possible integer which divides the whole array. Hence, we need to find the smallest prime factor of the GCD as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestDivisor( int x)
{
if (x % 2 == 0)
return 2;
for ( int i = 3; i * i <= x; i += 2) {
if (x % i == 0)
return i;
}
return x;
}
int smallestInteger( int * arr, int n)
{
int gcd = 0;
for ( int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
return smallestDivisor(gcd);
}
int main()
{
int arr[] = { 2, 4, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << smallestInteger(arr, n);
return 0;
}
|
Java
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
static int smallestDivisor( int x)
{
if (x % 2 == 0 )
return 2 ;
for ( int i = 3 ; i * i <= x; i += 2 )
{
if (x % i == 0 )
return i;
}
return x;
}
static int smallestInteger( int []arr, int n)
{
int gcd = 0 ;
for ( int i = 0 ; i < n; i++)
gcd = __gcd(gcd, arr[i]);
return smallestDivisor(gcd);
}
public static void main(String[] args)
{
int []arr = { 2 , 4 , 8 };
int n = arr.length;
System.out.println(smallestInteger(arr, n));
}
}
|
Python3
from math import sqrt, gcd
def smallestDivisor(x) :
if (x % 2 = = 0 ) :
return 2 ;
for i in range ( 3 , int (sqrt(x)) + 1 , 2 ) :
if (x % i = = 0 ) :
return i;
return x
def smallestInteger(arr, n) :
__gcd = 0 ;
for i in range (n) :
__gcd = gcd(__gcd, arr[i]);
return smallestDivisor(__gcd);
if __name__ = = "__main__" :
arr = [ 2 , 4 , 8 ];
n = len (arr);
print (smallestInteger(arr, n));
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
static int smallestDivisor( int x)
{
if (x % 2 == 0)
return 2;
for ( int i = 3; i * i <= x; i += 2)
{
if (x % i == 0)
return i;
}
return x;
}
static int smallestInteger( int []arr, int n)
{
int gcd = 0;
for ( int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
return smallestDivisor(gcd);
}
static void Main()
{
int []arr = { 2, 4, 8 };
int n = arr.Length;
Console.WriteLine(smallestInteger(arr, n));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return gcd( $b , $a % $b );
}
function smallestDivisor( $x )
{
if ( $x % 2 == 0)
return 2;
for ( $i = 3; $i < sqrt( $x ) + 1; $i += 2)
{
if ( $x % $i == 0)
return $i ;
}
return $x ;
}
function smallestInteger( $arr , $n )
{
$__gcd = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$__gcd = gcd( $__gcd , $arr [ $i ]);
}
return smallestDivisor( $__gcd );
}
$arr = array (2, 4, 8);
$n = count ( $arr );
echo smallestInteger( $arr , $n );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
function smallestDivisor(x)
{
if (x % 2 == 0)
return 2;
for (let i = 3; i * i <= x; i += 2)
{
if (x % i == 0)
return i;
}
return x;
}
function smallestInteger(arr, n)
{
let gcd = 0;
for (let i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
return smallestDivisor(gcd);
}
let arr = [ 2, 4, 8 ];
let n = arr.length;
document.write(smallestInteger(arr, n));
</script>
|
Time Complexity: O(n*(log(min(a, b))))
Auxiliary Space: O(1)
For multiple queries, we can precompute the smallest prime factors for numbers to a maximum value using a sieve.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
int spf[MAX];
void sieve()
{
memset (spf, 0, sizeof (spf));
spf[0] = 1;
spf[1] = -1;
for ( int i = 2; i * i < MAX; i++) {
if (spf[i] == 0) {
for ( int j = i * 2; j < MAX; j += i) {
if (spf[j] == 0) {
spf[j] = i;
}
}
}
}
for ( int i = 2; i < MAX; i++) {
if (!spf[i])
spf[i] = i;
}
}
int smallestInteger( int * arr, int n)
{
int gcd = 0;
for ( int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
return spf[gcd];
}
int main()
{
sieve();
int arr[] = { 2, 4, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << smallestInteger(arr, n);
return 0;
}
|
Java
class GFG
{
static int MAX = 100005 ;
static int spf[] = new int [MAX];
static void sieve()
{
spf[ 0 ] = 1 ;
spf[ 1 ] = - 1 ;
for ( int i = 2 ; i * i < MAX; i++)
{
if (spf[i] == 0 )
{
for ( int j = i * 2 ; j < MAX; j += i)
{
if (spf[j] == 0 )
{
spf[j] = i;
}
}
}
}
for ( int i = 2 ; i < MAX; i++)
{
if (spf[i] != 1 )
spf[i] = i;
}
}
static int smallestInteger( int [] arr, int n)
{
int gcd = 0 ;
for ( int i = 0 ; i < n; i++)
gcd = __gcd(gcd, arr[i]);
return spf[gcd];
}
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
public static void main(String[] args)
{
sieve();
int arr[] = { 2 , 4 , 8 };
int n = arr.length;
System.out.println(smallestInteger(arr, n));
}
}
|
Python3
MAX = 10005 ;
spf = [ 0 ] * MAX ;
def sieve():
spf[ 0 ] = 1 ;
spf[ 1 ] = - 1 ;
i = 2 ;
while (i * i < MAX ):
if (spf[i] = = 0 ):
for j in range (i * 2 , MAX , i):
if (spf[j] = = 0 ):
spf[j] = i;
i + = 1 ;
for i in range ( 2 , MAX ):
if (spf[i] = = 0 ):
spf[i] = i;
def __gcd(a, b):
if (b = = 0 ):
return a;
return __gcd(b, a % b);
def smallestInteger(arr, n):
gcd = 0 ;
for i in range (n):
gcd = __gcd(gcd, arr[i]);
return spf[gcd];
sieve();
arr = [ 2 , 4 , 8 ];
n = len (arr);
print (smallestInteger(arr, n));
|
C#
using System;
class GFG
{
static int MAX = 100005;
static int []spf = new int [MAX];
static void sieve()
{
spf[0] = 1;
spf[1] = -1;
for ( int i = 2; i * i < MAX; i++)
{
if (spf[i] == 0)
{
for ( int j = i * 2; j < MAX; j += i)
{
if (spf[j] == 0)
{
spf[j] = i;
}
}
}
}
for ( int i = 2; i < MAX; i++)
{
if (spf[i] != 1)
spf[i] = i;
}
}
static int smallestInteger( int [] arr, int n)
{
int gcd = 0;
for ( int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
return spf[gcd];
}
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
public static void Main(String[] args)
{
sieve();
int []arr = { 2, 4, 8 };
int n = arr.Length;
Console.WriteLine(smallestInteger(arr, n));
}
}
|
PHP
<?php
$MAX = 10005;
$spf = array_fill (0, $MAX , 0);
function sieve()
{
global $spf , $MAX ;
$spf [0] = 1;
$spf [1] = -1;
for ( $i = 2; $i * $i < $MAX ; $i ++)
{
if ( $spf [ $i ] == 0)
{
for ( $j = $i * 2; $j < $MAX ; $j += $i )
{
if ( $spf [ $j ] == 0)
{
$spf [ $j ] = $i ;
}
}
}
}
for ( $i = 2; $i < $MAX ; $i ++)
{
if (! $spf [ $i ])
$spf [ $i ] = $i ;
}
}
function __gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return __gcd( $b , $a % $b );
}
function smallestInteger( $arr , $n )
{
global $spf , $MAX ;
$gcd = 0;
for ( $i = 0; $i < $n ; $i ++)
$gcd = __gcd( $gcd , $arr [ $i ]);
return $spf [ $gcd ];
}
sieve();
$arr = array ( 2, 4, 8 );
$n = count ( $arr );
echo smallestInteger( $arr , $n );
?>
|
Javascript
<script>
let MAX = 100005;
let spf = new Array(MAX);
function sieve()
{
spf[0] = 1;
spf[1] = -1;
for (let i = 2; i * i < MAX; i++)
{
if (spf[i] == 0)
{
for (let j = i * 2; j < MAX; j += i)
{
if (spf[j] == 0)
{
spf[j] = i;
}
}
}
}
for (let i = 2; i < MAX; i++)
{
if (spf[i] != 1)
spf[i] = i;
}
}
function smallestInteger(arr, n)
{
let gcd = 0;
for (let i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
return spf[gcd];
}
function __gcd(a, b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
sieve();
let arr = [ 2, 4, 8 ];
let n = arr.length;
document.write(smallestInteger(arr, n));
</script>
|
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...