Find an array element such that all elements are divisible by it | CPP Program to find the smallest number that divides all numbers in an array ; function to find smallest num ; Find the smallest element ; Check if all array elements are divisible by smallest . ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }