Maximize number of circular buildings that can be covered by L length wire | JavaScript program for the above approach ; Function to find the maximum number of buildings covered ; Store the current sum ; Traverse the array ; Add the length of wire required for current building to cur_sum ; Add extra unit distance 1 ; If curr_sum <= length of wire increment count by 1 ; If curr_sum > length of wire increment start by 1 and decrement count by 1 and update the new curr_sum ; Update the max_count ; Return the max_count ; Given Input ; Size of the array ; Function Call
var Pi = 3.141592 ; function MaxBuildingsCovered ( arr , N , L ) { var curr_sum = 0 ; var start = 0 , curr_count = 0 , max_count = 0 ; for ( var i = 0 ; i < N ; i ++ ) { curr_sum = curr_sum + parseFloat ( arr [ i ] ) * Pi ; if ( i != 0 ) curr_sum += 1 ; if ( curr_sum <= L ) { curr_count ++ ; } else if ( curr_sum > L ) { curr_sum = curr_sum - parseFloat ( arr [ start ] ) * Pi ; curr_sum -= 1 ; start ++ ; curr_count -- ; } max_count = Math . max ( curr_count , max_count ) ; } return max_count ; } var arr = [ 4 , 1 , 6 , 2 ] ; var L = 24 ; var N = arr . length ; document . write ( MaxBuildingsCovered ( arr , N , L ) ) ;