Find an array element such that all elements are divisible by it
Given an array of numbers, find the number among them such that all numbers are divisible by it. If not possible print -1.
Examples:
Input : arr = {25, 20, 5, 10, 100}
Output : 5
Explanation : 5 is an array element
which divides all numbers.
Input : arr = {9, 3, 6, 2, 15}
Output : -1
Explanation : No numbers are divisible
by any array element.
Method 1:(naive): A normal approach will be to take every element and check for division with all other elements. If all the numbers are divisible then return the number.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findSmallest( int a[], int n)
{
for ( int i = 0; i < n; i++) {
int j;
for (j = 0; j < n; j++)
if (a[j] % a[i])
break ;
if (j == n)
return a[i];
}
return -1;
}
int main()
{
int a[] = { 25, 20, 5, 10, 100 };
int n = sizeof (a) / sizeof ( int );
cout << findSmallest(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findSmallest( int a[], int n)
{
for ( int i = 0 ; i < n; i++)
{
int j;
for (j = 0 ; j < n; j++)
if (a[j] % a[i]>= 1 )
break ;
if (j == n)
return a[i];
}
return - 1 ;
}
public static void main(String args[])
{
int a[] = { 25 , 20 , 5 , 10 , 100 };
int n = a.length;
System.out.println(findSmallest(a, n));
}
}
|
Python3
def findSmallest(a, n) :
for i in range ( 0 , n ) :
for j in range ( 0 , n) :
if ((a[j] % a[i]) > = 1 ) :
break
if (j = = n - 1 ) :
return a[i]
return - 1
a = [ 25 , 20 , 5 , 10 , 100 ]
n = len (a)
print (findSmallest(a, n))
|
C#
using System;
class GFG {
static int findSmallest( int []a, int n)
{
for ( int i = 0; i < n; i++)
{
int j;
for (j = 0; j < n; j++)
if (a[j] % a[i] >= 1)
break ;
if (j == n)
return a[i];
}
return -1;
}
public static void Main()
{
int []a = { 25, 20, 5, 10, 100 };
int n = a.Length;
Console.WriteLine(findSmallest(a, n));
}
}
|
PHP
<?php
function findSmallest( $a , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$j ;
for ( $j = 0; $j < $n ; $j ++)
if ( $a [ $j ] % $a [ $i ])
break ;
if ( $j == $n )
return $a [ $i ];
}
return -1;
}
$a = array ( 25, 20, 5, 10, 100 );
$n = sizeof( $a );
echo findSmallest( $a , $n );
?>
|
Javascript
<script>
function findSmallest(a, n)
{
for (let i = 0; i < n; i++)
{
let j;
for (j = 0; j < n; j++)
if (a[j] % a[i]>=1)
break ;
if (j == n)
return a[i];
}
return -1;
}
let a = [ 25, 20, 5, 10, 100 ];
let n = a.length;
document.write(findSmallest(a, n));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Method 2 : (Efficient): An efficient approach is to find smallest of all numbers, and check if it divides all the other numbers, if yes then the smallest number will be the required number.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findSmallest( int a[], int n)
{
int smallest = *min_element(a, a+n);
for ( int i = 1; i < n; i++)
if (a[i] % smallest)
return -1;
return smallest;
}
int main()
{
int a[] = { 25, 20, 5, 10, 100 };
int n = sizeof (a) / sizeof ( int );
cout << findSmallest(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int min_element( int a[])
{
int min = Integer.MAX_VALUE, i;
for (i = 0 ; i < a.length; i++)
{
if (a[i] < min)
min = a[i];
}
return min;
}
static int findSmallest( int a[], int n)
{
int smallest = min_element(a);
for ( int i = 1 ; i < n; i++)
if (a[i] % smallest >= 1 )
return - 1 ;
return smallest;
}
public static void main(String args[])
{
int a[] = { 25 , 20 , 5 , 10 , 100 };
int n = a.length;
System.out.println(findSmallest(a, n));
}
}
|
Python3
def min_element(a) :
m = 10000000
for i in range ( 0 , len (a)) :
if (a[i] < m) :
m = a[i]
return m
def findSmallest(a, n) :
smallest = min_element(a)
for i in range ( 1 , n) :
if (a[i] % smallest > = 1 ) :
return - 1
return smallest
a = [ 25 , 20 , 5 , 10 , 100 ]
n = len (a)
print (findSmallest(a, n))
|
C#
using System;
class GFG {
static int min_element( int []a)
{
int min = int .MaxValue;
int i;
for (i = 0; i < a.Length; i++)
{
if (a[i] < min)
min = a[i];
}
return min;
}
static int findSmallest( int []a, int n)
{
int smallest = min_element(a);
for ( int i = 1; i < n; i++)
if (a[i] % smallest >= 1)
return -1;
return smallest;
}
public static void Main()
{
int []a = {25, 20, 5, 10, 100};
int n = a.Length;
Console.WriteLine(findSmallest(a, n));
}
}
|
PHP
<?php
function findSmallest( $a , $n )
{
$smallest = min( $a );
for ( $i = 1; $i < $n ; $i ++)
if ( $a [ $i ] % $smallest )
return -1;
return $smallest ;
}
$a = array (25, 20, 5, 10, 100);
$n = count ( $a );
echo findSmallest( $a , $n );
?>
|
Javascript
<script>
function min_element(a)
{
let min = Number.MAX_VALUE;
let i;
for (i = 0; i < a.length; i++)
{
if (a[i] < min)
min = a[i];
}
return min;
}
function findSmallest(a, n)
{
let smallest = min_element(a);
for (let i = 1; i < n; i++)
if (a[i] % smallest >= 1)
return -1;
return smallest;
}
let a = [25, 20, 5, 10, 100];
let n = a.length;
document.write(findSmallest(a, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...