Find largest d in array such that a + b + c = d | function to find largest d ; sort the array in ascending order ; iterating from backwards to find the required largest d ; since all four a , b , c , d should be distinct ; if the current combination of j , k , l in the set is equal to S [ i ] return this value as this would be the largest d since we are iterating in descending order ; Driver Code
< ? php function findLargestd ( $ S , $ n ) { $ found = false ; sort ( $ S ) ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i == $ j ) continue ; for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) { if ( $ i == $ k ) continue ; for ( $ l = $ k + 1 ; $ l < $ n ; $ l ++ ) { if ( $ i == $ l ) continue ; if ( $ S [ $ i ] == $ S [ $ j ] + $ S [ $ k ] + $ S [ $ l ] ) { $ found = true ; return $ S [ $ i ] ; } } } } } if ( $ found == false ) return PHP_INT_MIN ; } $ S = array ( 2 , 3 , 5 , 7 , 12 ) ; $ n = count ( $ S ) ; $ ans = findLargestd ( $ S , $ n ) ; if ( $ ans == PHP_INT_MIN ) echo " No ▁ Solution " ; else echo " Largest ▁ d ▁ such ▁ that ▁ a ▁ + ▁ b ▁ + ▁ " , " c ▁ = ▁ d ▁ is ▁ " , $ ans ; ? >