Maximize number of circular buildings that can be covered by L length wire | C ++ 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 ; Driver Code ; Given Input ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const double Pi = 3.141592 ; int MaxBuildingsCovered ( int arr [ ] , int N , int L ) { double curr_sum = 0 ; int start = 0 , curr_count = 0 , max_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { curr_sum = curr_sum + ( ( double ) arr [ i ] * Pi ) ; if ( i != 0 ) curr_sum += 1 ; if ( curr_sum <= L ) { curr_count ++ ; } else if ( curr_sum > L ) { curr_sum = curr_sum - ( ( double ) arr [ start ] * Pi ) ; curr_sum -= 1 ; start ++ ; curr_count -- ; } max_count = max ( curr_count , max_count ) ; } return max_count ; } int main ( ) { int arr [ ] = { 4 , 1 , 6 , 2 } ; int L = 24 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MaxBuildingsCovered ( arr , N , L ) ; return 0 ; }