Water supply

 Your country can be represented by 

N cities connected using N1 roads. There exists an edge between city (i, i+1) for all i from 1 to n1.

You have to set up a connection for water supply. You set this in one city and water gets transported from it to other cities using road transport. Certain cities are blocked which means that water cannot pass through this city. Determine the maximum number of cities to which water can be

Example:

Input: n=4, arr[]={0,1,1,0}
Output: 2

Approach

Java


public class Watersupply {
    public static void main(String[] args) {
        int n = 4;
        int arr[] = { 0110 };
        int maxChain = 0;
        int currChain = 0;
        boolean someUnblocked = false;
        for (int i = 0; i < n; i++) {
            int temp = arr[i];
            if (temp == 0) {
                someUnblocked = true;
            }
            if (currChain == 0 || temp == 0) {
                currChain++;
            } else {
                maxChain = Math.max(maxChain, currChain + 1);
                currChain = 1;
            }
        }

        if (!someUnblocked) {
            System.out.println(1);
        } else {
            System.out.println(maxChain);
        }
    }

}


No comments:

Post a Comment