Skip to content
Related Articles

Related Articles

Smallest integer > 1 which divides every element of the given array
  • Last Updated : 04 Apr, 2019
GeeksforGeeks - Summer Carnival Banner

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 which 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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the smallest divisor 
int smallestDivisor(int x) 
    // if divisible by 2 
    if (x % 2 == 0) 
        return 2; 
    
    // iterate from 3 to sqrt(n) 
    for (int i = 3; i * i <= x; i += 2) { 
        if (x % i == 0) 
            return i; 
    
    
    return x; 
  
// Function to return smallest possible integer
// which divides the whole array
int smallestInteger(int* arr, int n)
{
    // To store the GCD of all the array elements
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(gcd, arr[i]);
  
    // Return the smallest prime factor
    // of the gcd calculated
    return smallestDivisor(gcd);
}
  
// Driver code
int main()
{
    int arr[] = { 2, 4, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << smallestInteger(arr, n);
  
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
  
static int __gcd(int a, int b) 
    if (b == 0
        return a; 
    return __gcd(b, a % b); 
      
  
// Function to find the smallest divisor 
static int smallestDivisor(int x) 
    // if divisible by 2 
    if (x % 2 == 0
        return 2
  
    // iterate from 3 to sqrt(n) 
    for (int i = 3; i * i <= x; i += 2
    
        if (x % i == 0
            return i; 
    
  
    return x; 
  
// Function to return smallest possible integer
// which divides the whole array
static int smallestInteger(int []arr, int n)
{
    // To store the GCD of all the array elements
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(gcd, arr[i]);
  
    // Return the smallest prime factor
    // of the gcd calculated
    return smallestDivisor(gcd);
}
  
// Driver code
public static void main(String[] args)
{
    int []arr = { 2, 4, 8 };
    int n = arr.length;
    System.out.println(smallestInteger(arr, n));
}
}
  
// This code is contributed by Code_Mech.

Python3




# Python3 implementation of the approach 
from math import sqrt, gcd
  
# Function to find the smallest divisor 
def smallestDivisor(x) :
      
    # if divisible by 2 
    if (x % 2 == 0) :
        return 2
      
    # iterate from 3 to sqrt(n) 
    for i in range(3, int(sqrt(x)) + 1, 2) :
        if (x % i == 0) :
            return i; 
      
    return
  
# Function to return smallest possible 
# integer which divides the whole array 
def smallestInteger(arr, n) :
      
    # To store the GCD of all the
    # array elements 
    __gcd = 0
    for i in range(n) :
        __gcd = gcd(__gcd, arr[i]); 
  
    # Return the smallest prime factor 
    # of the gcd calculated 
    return smallestDivisor(__gcd); 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 2, 4, 8 ];
    n = len(arr);
      
    print(smallestInteger(arr, n)); 
  
# This code is contributed by Ryuga

C#




// C# implementation of the approach
using System;
  
class GFG
{
  
static int __gcd(int a, int b) 
    if (b == 0) 
        return a; 
    return __gcd(b, a % b); 
      
  
// Function to find the smallest divisor 
static int smallestDivisor(int x) 
    // if divisible by 2 
    if (x % 2 == 0) 
        return 2; 
  
    // iterate from 3 to sqrt(n) 
    for (int i = 3; i * i <= x; i += 2) 
    
        if (x % i == 0) 
            return i; 
    
  
    return x; 
  
// Function to return smallest possible integer
// which divides the whole array
static int smallestInteger(int []arr, int n)
{
    // To store the GCD of all the array elements
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(gcd, arr[i]);
  
    // Return the smallest prime factor
    // of the gcd calculated
    return smallestDivisor(gcd);
}
  
// Driver code
static void Main()
{
    int []arr = { 2, 4, 8 };
    int n = arr.Length;
    Console.WriteLine(smallestInteger(arr, n));
}
}
  
// This code is contributed by mits

PHP




<?php
// PHP implementation of the approach
function gcd($a, $b)
{
    // Everything divides 0
    if($b == 0)
        return $a;
  
    return gcd($b , $a % $b);
}
  
// Function to find the smallest divisor
function smallestDivisor($x)
{
    // if divisible by 2
    if ($x % 2 == 0)
        return 2;
  
    // iterate from 3 to sqrt(n)
    for ($i = 3; $i < sqrt($x) + 1; $i += 2)
    {
        if ($x % $i == 0)
            return $i;
    }
    return $x;
}
  
// Function to return smallest possible
// integer which divides the whole array
function smallestInteger($arr, $n)
{
      
    // To store the GCD of all the
    // array elements
    $__gcd = 0;
    for ($i = 0; $i < $n; $i++) 
    {
        $__gcd = gcd($__gcd, $arr[$i]);
    }
      
    // Return the smallest prime factor
    // of the gcd calculated
    return smallestDivisor($__gcd);
}
      
// Driver code
$arr = array(2, 4, 8);
$n = count($arr);
  
echo smallestInteger($arr, $n);
  
// This code is contributed by Srathore
?>
Output:
2

For multiple queries, we can precompute the smallest prime factors for numbers till a maximum value using sieve.

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
const int MAX = 100005;
  
// To store the smallest prime factor
int spf[MAX];
  
// Function to store spf of integers
void sieve()
{
    memset(spf, 0, sizeof(spf));
    spf[0] = 1;
  
    // When gcd is 1 then the answer is -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;
    }
}
  
// Function to return smallest possible integer
// which divides the whole array
int smallestInteger(int* arr, int n)
{
  
    // To store the GCD of all the array elements
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(gcd, arr[i]);
  
    // Return the smallest prime factor
    // of the gcd calculated
    return spf[gcd];
}
  
// Driver code
int main()
{
    sieve();
    int arr[] = { 2, 4, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << smallestInteger(arr, n);
  
    return 0;
}

Java




// Java implementation of the approach
class GFG 
{
  
static int MAX = 100005
  
// To store the smallest prime factor 
static int spf[] = new int[MAX]; 
  
// Function to store spf of integers 
static void sieve() 
    spf[0] = 1
  
    // When gcd is 1 then the answer is -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; 
    
  
// Function to return smallest possible integer 
// which divides the whole array 
static int smallestInteger(int[] arr, int n) 
  
    // To store the GCD of all the array elements 
    int gcd = 0
    for (int i = 0; i < n; i++) 
        gcd = __gcd(gcd, arr[i]); 
  
    // Return the smallest prime factor 
    // of the gcd calculated 
    return spf[gcd]; 
}
  
static int __gcd(int a, int b) 
    if (b == 0
        return a; 
    return __gcd(b, a % b); 
      
}
  
// Driver code 
public static void main(String[] args) 
{
    sieve(); 
    int arr[] = { 2, 4, 8 }; 
    int n = arr.length; 
    System.out.println(smallestInteger(arr, n)); 
}
}
  
/* This code contributed by PrinciRaj1992 */

Python3




# Python3 implementation of the approach
MAX = 10005;
  
# To store the smallest prime factor
spf = [0] * MAX;
  
# Function to store spf of integers
def sieve():
  
    spf[0] = 1;
  
    # When gcd is 1 then the answer is -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;
  
# find gcd of two no
def __gcd(a, b): 
    if (b == 0): 
        return a; 
    return __gcd(b, a % b); 
  
# Function to return smallest possible integer
# which divides the whole array
def smallestInteger(arr, n):
      
    # To store the GCD of all the array elements
    gcd = 0;
    for i in range(n):
        gcd = __gcd(gcd, arr[i]);
  
    # Return the smallest prime factor
    # of the gcd calculated
    return spf[gcd];
  
# Driver code
sieve();
arr = [ 2, 4, 8 ];
n = len(arr);
print(smallestInteger(arr, n));
  
# This code is contributed by mits

C#




// C# implemenatation of above approach 
using System;
      
class GFG 
{
  
static int MAX = 100005; 
  
// To store the smallest prime factor 
static int []spf = new int[MAX]; 
  
// Function to store spf of integers 
static void sieve() 
    spf[0] = 1; 
  
    // When gcd is 1 then the answer is -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; 
    
  
// Function to return smallest possible integer 
// which divides the whole array 
static int smallestInteger(int[] arr, int n) 
  
    // To store the GCD of all the array elements 
    int gcd = 0; 
    for (int i = 0; i < n; i++) 
        gcd = __gcd(gcd, arr[i]); 
  
    // Return the smallest prime factor 
    // of the gcd calculated 
    return spf[gcd]; 
}
  
static int __gcd(int a, int b) 
    if (b == 0) 
        return a; 
    return __gcd(b, a % b); 
      
}
  
// Driver code 
public static void Main(String[] args) 
{
    sieve(); 
    int []arr = { 2, 4, 8 }; 
    int n = arr.Length; 
    Console.WriteLine(smallestInteger(arr, n)); 
}
}
  
// This code has been contributed by 29AjayKumar

PHP




<?php
// PHP implementation of the approach
  
$MAX = 10005;
  
// To store the smallest prime factor
$spf = array_fill(0, $MAX, 0);
  
// Function to store spf of integers
function sieve()
{
    global $spf, $MAX;
    $spf[0] = 1;
  
    // When gcd is 1 then the answer is -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;
    }
}
  
// find gcd of two no
function __gcd($a, $b
    if ($b == 0) 
        return $a
    return __gcd($b, $a % $b); 
      
  
// Function to return smallest possible integer
// which divides the whole array
function smallestInteger($arr, $n)
{
    global $spf, $MAX;
      
    // To store the GCD of all the array elements
    $gcd = 0;
    for ($i = 0; $i < $n; $i++)
        $gcd = __gcd($gcd, $arr[$i]);
  
    // Return the smallest prime factor
    // of the gcd calculated
    return $spf[$gcd];
}
  
// Driver code
sieve();
$arr = array( 2, 4, 8 );
$n = count($arr);
echo smallestInteger($arr, $n);
  
// This code is contributed by mits
?>
Output:
2

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :