Find the two repeating elements in a given array | printRepeating function ; S is for sum of elements in arr [ ] ; P is for product of elements in arr [ ] ; x and y are two repeating elements ; D is for difference of x and y , i . e . , x - y ; Calculate Sum and Product of all elements in arr [ ] ; S is x + y now ; P is x * y now ; D is x - y now ; factorial of n ; driver code
< ? php function printRepeating ( $ arr , $ size ) { $ S = 0 ; $ P = 1 ; $ x ; $ y ; $ D ; $ n = $ size - 2 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ S = $ S + $ arr [ $ i ] ; $ P = $ P * $ arr [ $ i ] ; } $ S = $ S - $ n * ( $ n + 1 ) / 2 ; $ P = $ P / fact ( $ n ) ; $ D = sqrt ( $ S * $ S - 4 * $ P ) ; $ x = ( $ D + $ S ) / 2 ; $ y = ( $ S - $ D ) / 2 ; echo " The ▁ two ▁ Repeating ▁ elements ▁ are ▁ " . $ x . " & " } function fact ( $ n ) { return ( $ n == 0 ) ? 1 : $ n * fact ( $ n - 1 ) ; } $ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 1 ) ; $ arr_size = count ( $ arr ) ; printRepeating ( $ arr , $ arr_size ) ; ? >