# Maximize number of circular buildings that can be covered by L length wire

• Difficulty Level : Medium
• Last Updated : 30 Jun, 2021

Given an array arr[] of size N, representing the diameter of N circular buildings and a straight wire of length L. The task is to find the maximum number of continuous buildings the wire can cover if it is round it once around the building.

Note: Distance between each building is 1 unit length, so it takes 1 unit length extra to reach one build to the next building.

Examples:

Input: arr[] = {4, 1, 6}, L = 24
Output: 2
Explanation: 1 round of building will require  pi * d length of wire, where pi is 3.14159 and d is the diameter of circle.
For 1st building â†’ 12.566 length of wire required, remaining wireâ†’ 24-12.566=11.434 -1( to reach next building)=10.434
Similarly, for second building 3.141 length of wire required, remaining wireâ†’ 10.434-3.141= 7.292 -1= 6.292
For third building 18.849, which is > remaining wire i.e, 18.849>6.292
Therefore, Maximum of 2 building can be covered.

Input: arr[] = {2, 5, 3, 4}, L = 36
Output: 3

Approach: The idea is to use the greedy approach. Follow the steps below to solve the problem:

• Initialize variables curr_sum, start, curr_count, max_count to calculate the current sum of elements, current count, starting index of the current subarray, and maximum count of covered buildings.
• Traverse the array for i in range [0, N – 1],
• Update current sum of wire length required, curr_sum += arr[i] * 3.14
• If i is greater than 0, Increment curr_sum by 1.
• If curr_sum â‰¤ L. Increment curr_count by 1
• Otherwise, exclude arr[start] from the current group
• Update curr_sum = curr_sum – ((double)arr[start] * 3.14)
• Decrement curr_sum by 1
• Increment start pointer by 1
• Decrement curr_count by 1
• Update max_count i.e, max_count = max(curr_count, max_count).
• After completing the above steps, print the value of max_count as the result.

Below is the implementation of the above approach.

## C++14

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;` `const` `double` `Pi = 3.141592;`   `// Function to find the maximum` `// number of buildings covered` `int` `MaxBuildingsCovered(``int` `arr[], ``int` `N, ``int` `L)` `{` `    ``// Store the current sum` `    ``double` `curr_sum = 0;`   `    ``int` `start = 0, curr_count = 0, max_count = 0;`   `    ``// Traverse the array` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// Add the length of wire required for` `        ``// current building to cur_sum` `        ``curr_sum = curr_sum + ((``double``)arr[i] * Pi);`   `        ``// Add extra unit distance 1` `        ``if` `(i != 0)` `            ``curr_sum += 1;`   `        ``// If curr_sum <= length of wire` `        ``// increment count by 1` `        ``if` `(curr_sum <= L) {` `            ``curr_count++;` `        ``}`   `        ``// If curr_sum > length of wire` `        ``// increment start by 1 and` `        ``// decrement count by 1 and` `        ``// update the new curr_sum` `        ``else` `if` `(curr_sum > L) {` `            ``curr_sum = curr_sum - ((``double``)arr[start] * Pi);` `            ``curr_sum -= 1;` `            ``start++;` `            ``curr_count--;` `        ``}`   `        ``// Update the max_count` `        ``max_count = max(curr_count, max_count);` `    ``}`   `    ``// Return the max_count` `    ``return` `max_count;` `}`   `// Driver Code` `int` `main()` `{` `    ``// Given Input` `    ``int` `arr[] = { 4, 1, 6, 2 };` `    ``int` `L = 24;`   `    ``// Size of the array` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``// Function Call` `    ``cout << MaxBuildingsCovered(arr, N, L);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.io.*;`   `class` `GFG{`   `static` `final` `double` `Pi = ``3.141592``;`   `// Function to find the maximum` `// number of buildings covered` `static` `int` `MaxBuildingsCovered(``int` `arr[], ``int` `N,` `                               ``int` `L)` `{` `    `  `    ``// Store the current sum` `    ``double` `curr_sum = ``0``;`   `    ``int` `start = ``0``, curr_count = ``0``, max_count = ``0``;`   `    ``// Traverse the array` `    ``for``(``int` `i = ``0``; i < N; i++) ` `    ``{` `        `  `        ``// Add the length of wire required for` `        ``// current building to cur_sum` `        ``curr_sum = curr_sum + ((``double``)arr[i] * Pi);`   `        ``// Add extra unit distance 1` `        ``if` `(i != ``0``)` `            ``curr_sum += ``1``;`   `        ``// If curr_sum <= length of wire` `        ``// increment count by 1` `        ``if` `(curr_sum <= L)` `        ``{` `            ``curr_count++;` `        ``}`   `        ``// If curr_sum > length of wire` `        ``// increment start by 1 and` `        ``// decrement count by 1 and` `        ``// update the new curr_sum` `        ``else` `if` `(curr_sum > L)` `        ``{` `            ``curr_sum = curr_sum - ((``double``)arr[start] * Pi);` `            ``curr_sum -= ``1``;` `            ``start++;` `            ``curr_count--;` `        ``}`   `        ``// Update the max_count` `        ``max_count = Math.max(curr_count, max_count);` `    ``}`   `    ``// Return the max_count` `    ``return` `max_count;` `}`   `// Driver Code` `public` `static` `void` `main (String[] args)` `{` `    `  `    ``// Given Input` `    ``int` `arr[] = { ``4``, ``1``, ``6``, ``2` `};` `    ``int` `L = ``24``;`   `    ``// Size of the array` `    ``int` `N = arr.length;`   `    ``// Function Call` `    ``System.out.println(MaxBuildingsCovered(arr, N, L));` `}` `}`   `// This code is contributed by Dharanendra L V.`

## Python3

 `# Python3 program for the above approach` `Pi ``=` `3.141592`   `# Function to find the maximum` `# number of buildings covered` `def` `MaxBuildingsCovered(arr, N,  L):` `    ``# Store the current sum` `    ``curr_sum ``=` `0`   `    ``start ``=` `0` `    ``curr_count ``=` `0` `    ``max_count ``=` `0`   `    ``# Traverse the array` `    ``for` `i ``in` `range``(N):` `        ``# Add the length of wire required for` `        ``# current building to cur_sum` `        ``curr_sum ``=` `curr_sum ``+` `(arr[i] ``*` `Pi)`   `        ``# Add extra unit distance 1` `        ``if` `(i !``=` `0``):` `            ``curr_sum ``+``=` `1`   `        ``# If curr_sum <= length of wire` `        ``# increment count by 1` `        ``if` `(curr_sum <``=` `L):` `            ``curr_count ``+``=` `1`   `        ``# If curr_sum > length of wire` `        ``# increment start by 1 and` `        ``# decrement count by 1 and` `        ``# update the new curr_sum` `        ``elif``(curr_sum > L):` `            ``curr_sum ``=` `curr_sum ``-` `(arr[start] ``*` `Pi)` `            ``curr_sum ``-``=` `1` `            ``start ``+``=` `1` `            ``curr_count ``-``=` `1`   `        ``# Update the max_count` `        ``max_count ``=` `max``(curr_count, max_count)`   `    ``# Return the max_count` `    ``return` `max_count`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``# Given Input` `    ``arr ``=` `[``4``, ``1``, ``6``, ``2``]` `    ``L ``=` `24`   `    ``# Size of the array` `    ``N ``=` `len``(arr)`   `    ``# Function Call` `    ``print``(MaxBuildingsCovered(arr, N, L))` `    `  `    ``# This code is contributed by SURENDRA_GANGWAR.`

## C#

 `// C# program for the above approach` `using` `System;`   `class` `GFG{`   `static` `double` `Pi = 3.141592;`   `// Function to find the maximum` `// number of buildings covered` `static` `int` `MaxBuildingsCovered(``int``[] arr, ``int` `N,` `                               ``int` `L)` `{` `    `  `    ``// Store the current sum` `    ``double` `curr_sum = 0;`   `    ``int` `start = 0, curr_count = 0, max_count = 0;`   `    ``// Traverse the array` `    ``for``(``int` `i = 0; i < N; i++) ` `    ``{` `        `  `        ``// Add the length of wire required for` `        ``// current building to cur_sum` `        ``curr_sum = curr_sum + ((``double``)arr[i] * Pi);`   `        ``// Add extra unit distance 1` `        ``if` `(i != 0)` `            ``curr_sum += 1;`   `        ``// If curr_sum <= length of wire` `        ``// increment count by 1` `        ``if` `(curr_sum <= L)` `        ``{` `            ``curr_count++;` `        ``}`   `        ``// If curr_sum > length of wire` `        ``// increment start by 1 and` `        ``// decrement count by 1 and` `        ``// update the new curr_sum` `        ``else` `if` `(curr_sum > L)` `        ``{` `            ``curr_sum = curr_sum - ((``double``)arr[start] * Pi);` `            ``curr_sum -= 1;` `            ``start++;` `            ``curr_count--;` `        ``}`   `        ``// Update the max_count` `        ``max_count = Math.Max(curr_count, max_count);` `    ``}`   `    ``// Return the max_count` `    ``return` `max_count;` `}`   `// Driver code` `static` `void` `Main()` `{` `    `  `    ``// Given Input` `    ``int``[] arr = { 4, 1, 6, 2 };` `    ``int` `L = 24;`   `    ``// Size of the array` `    ``int` `N = arr.Length;`   `    ``// Function Call` `    ``Console.Write(MaxBuildingsCovered(arr, N, L));` `}` `}`   `// This code is contributed by code_hunt`

## Javascript

 ``

Output

`2`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles