Smallest integer > 1 which divides every element of the given array
Last Updated :
22 Jun, 2022
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++
// 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 x
# 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
?>
JavaScript
<script>
// Javascript implementation of the approach
function __gcd(a, b)
{
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 (let 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
function smallestInteger(arr, n)
{
// To store the GCD of all the array elements
let gcd = 0;
for (let i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return smallestDivisor(gcd);
}
let arr = [ 2, 4, 8 ];
let n = arr.length;
document.write(smallestInteger(arr, n));
// This code is contributed by divyeshrabadiya07.
</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++
// 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# implementation 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
?>
JavaScript
<script>
// Javascript implementation of above approach
let MAX = 100005;
// To store the smallest prime factor
let spf = new Array(MAX);
// Function to store spf of integers
function sieve()
{
spf[0] = 1;
// When gcd is 1 then the answer is -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 to return smallest possible integer
// which divides the whole array
function smallestInteger(arr, n)
{
// To store the GCD of all the array elements
let gcd = 0;
for (let i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
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>
Similar Reads
How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma
2 min read
Minimum possible sum of array elements after performing the given operation Given an array arr[] of positive integers and an integer x, the task is to minimize the sum of elements of the array after performing the given operation at most once. In a single operation, any element from the array can be divided by x (if it is divisible by x) and at the same time, any other elem
8 min read
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 elements is divisible by K. Examples: Input: arr[] = {3, 2, 6, 9, 2}Output: 4Explanation: None of the array elements is divisible by 4(the smallest positive).
6 min read
Smallest number dividing minimum number of elements in the Array Given an array arr[] of N integers, the task is to find the smallest number that divides the minimum number of elements from the array.Examples: Input: arr[] = {2, 12, 6} Output: 5 Here, 1 divides 3 elements 2 divides 3 elements 3 divides 2 elements 4 divides 1 element 5 divides no element 6 divides
6 min read
Smallest number dividing minimum number of elements in the array | Set 2 Given an array arr[] of N integers, the task is to find the smallest number that divides the minimum number of elements from the array. Examples: Input: arr[] = {2, 12, 6} Output: 5 Here, 1 divides 3 elements 2 divides 3 elements 3 divides 2 elements 4 divides 1 element 5 divides no element 6 divide
6 min read