Find A and B from list of divisors | Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code
< ? php function printNumbers ( $ arr , $ n ) { sort ( $ arr ) ; $ A = $ arr [ $ n - 1 ] ; $ B = -1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ A % $ arr [ $ i ] != 0 ) { $ B = $ arr [ $ i ] ; break ; } if ( $ i - 1 >= 0 && $ arr [ $ i ] == $ arr [ $ i - 1 ] ) { $ B = $ arr [ $ i ] ; break ; } } echo ( " A ▁ = ▁ " . $ A . " , B = " } $ arr = array ( 1 , 2 , 4 , 8 , 16 , 1 , 2 , 4 ) ; $ n = sizeof ( $ arr ) ; printNumbers ( $ arr , $ n ) ;