Check if the given array contains all the divisors of some integer | C # implementation of the approach ; returns th maximum element of the array ; Function that returns true if arr [ ] contains all the divisors of some integer ; Maximum element from the array ; Vector to store divisors of the maximum element i . e . X ; Store all the divisors of X ; If the lengths of a [ ] and b are different return false ; Sort a [ ] and b ; If divisors are not equal return false ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int max_element ( int [ ] a ) { int m = a [ 0 ] ; for ( int i = 0 ; i < a . Length ; i ++ ) m = Math . Max ( a [ i ] , m ) ; return m ; } static bool checkDivisors ( int [ ] a , int n ) { int X = max_element ( a ) ; List < int > b = new List < int > ( ) ; for ( int i = 1 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { b . Add ( i ) ; if ( X / i != i ) b . Add ( X / i ) ; } } if ( b . Count != n ) return false ; Array . Sort ( a ) ; b . Sort ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] != a [ i ] ) return false ; } return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 1 , 2 , 12 , 48 , 6 , 4 , 24 , 16 , 3 } ; int N = arr . Length ; if ( checkDivisors ( arr , N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }