FastPrepUsable Water with Contaminated Pillars

Usable Water with Contaminated Pillars

Infosys logoInfosys● MediumNEW GRADOA
Learn

Problem statement

You are given bar heights and a same-length binary contamination array. Rainwater is trapped by the bars under the usual one-dimensional rules.

Water directly above index i is unusable when contaminated[i] == 1; do not include those units in the result. Return the total usable trapped water above clean positions.

Function

usableTrappedWater(height: int[], contaminated: int[]) → long

Examples

Example 1

height = [0,1,0,2,1,0,1,3,2,1,2,1]contaminated = [0,0,0,0,0,1,0,0,0,0,0,0]return = 4

The standard total is 6, but the two units above contaminated index 5 are excluded.

Example 2

height = [4,2,0,3,2,5]contaminated = [0,0,1,0,0,0]return = 5

Four of the standard nine units lie above the contaminated index and are excluded.

Constraints

  • 1 <= height.length == contaminated.length <= 200000
  • 0 <= height[i] <= 10^9
  • Every contamination value is either 0 or 1.

Source note: Interview prompt excerpt preserved as source evidence.

More Infosys problems

See Infosys hiring insights
public long usableTrappedWater(int[] height, int[] contaminated) {
    // Write your code here.
}
height[0,1,0,2,1,0,1,3,2,1,2,1]
contaminated[0,0,0,0,0,1,0,0,0,0,0,0]
expected4
Checking account…