Find an array element such that all elements are divisible by it | Java program to find an array element that divides all numbers in the array using naive approach ; function to find smallest num ; traverse for all elements ; stores the minimum if it divides all ; driver code
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 ) ) ; } }