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
function validPosition ( arr , N , K ) { var count = 0 , sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( var i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) count ++ ; } return count ; } var arr = [ 2 , 1 , 6 , 7 ] , K = 4 ; var N = arr . length ; document . write ( validPosition ( arr , N , K ) ) ;