Number of positions such that adding K to the element is greater than sum of all other elements | Function that will find out the valid position ; find sum of all the elements ; adding K to the element and check whether it is greater than sum of all other elements ; Driver code
< ? php function validPosition ( $ arr , $ N , $ K ) { $ count = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum += $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( ( $ arr [ $ i ] + $ K ) > ( $ sum - $ arr [ $ i ] ) ) $ count ++ ; } return $ count ; } $ arr = array ( 2 , 1 , 6 , 7 ) ; $ K = 4 ; $ N = count ( $ arr ) ; echo validPosition ( $ arr , $ N , $ K ) ; ? >