DSA/algorithms/Java/arrays/trapping-rain-water.java

92 lines
2.1 KiB
Java
Raw Normal View History

// Given an array arr[] of N non-negative integers representing the height of blocks.
// If width of each block is 1, compute how much water can be trapped between the blocks during the rainy season.
//Time Complexity: O(N)
//Auxiliary Space: O(1)
import java.io.*;
import java.util.*;
import java.lang.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim()); //Inputting the testcases
while(t-->0){
//size of array
int n = Integer.parseInt(br.readLine().trim());
int arr[] = new int[n];
String inputLine[] = br.readLine().trim().split(" ");
//adding elements to the array
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(inputLine[i]);
}
Solution obj = new Solution();
//calling trappingWater() function
System.out.println(obj.trappingWater(arr, n));
}
}
}
class Solution{
// arr: input array
// n: size of array
// Function to find the trapped water between the blocks.
static int trappingWater(int arr[], int n) {
// initialize output
int result = 0;
// maximum element on left and right
int left_max = 0, right_max = 0;
// indices to traverse the array
int lo = 0, hi = n - 1;
while (lo <= hi) {
if (arr[lo] < arr[hi]) {
if (arr[lo] > left_max)
// update max in left
left_max = arr[lo];
else
// water on curr element = max - curr
result += left_max - arr[lo];
lo++;
}
else {
if (arr[hi] > right_max)
// update right maximum
right_max = arr[hi];
else
result += right_max - arr[hi];
hi--;
}
}
return result;
}
}
/* Test Case:
Input:
N = 6
arr[] = {3,0,0,2,0,4}
Output:
10
Explanation:
____|
| |
| | |
| | |
300204
Total Water Trapped: 3+3+1+3 = 10
*/