-
Notifications
You must be signed in to change notification settings - Fork 368
/
Rain_Water.c
54 lines (54 loc) · 1.45 KB
/
Rain_Water.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*Name : Atul Kumar
Github username : atul1510
Repositary name : Algorithms
Problem Description
Program to find the maximum trap water by using stack.
Examples:
1] INPUT: {1, 1, 5, 2, 1, 8 , 1, 0, 2, 1, 2, 6};
OUTPUT 31 */
// Implementation in C of the above approach
#include <stdio.h>
// Creating MAX function
#define max(x, y) (((x) > (y)) ? (x) : (y))
// Creating MIN function
#define min(x, y) (((x) < (y)) ? (x) : (y))
// Creating a Function that returns the maximum water
int RainWater(int arr[], int x)
{
// left[i] contains height of wall
// left of ith wall
int left[x];
// Right [i] contains height of wall
// right of ith wall
int right[x];
// Initialize the result
int water = 0;
// Filled the left array
left[0] = arr[0];
for (int i = 1; i < x; i++)
{
left[i] = max(left[i - 1], arr[i]);
}
// Filled the right array
right[x - 1] = arr[x - 1];
for (int i = x - 2; i >= 0; i--) {
right[i] = max(right[i + 1], arr[i]);
}
// Calculate the accumulated water element by element consider the amount of water on i'th bar, the amount of water accumulated on this particular
// bar will be equal to min(left[i], right[i]) - arr[i]
for (int i = 1; i < x - 1; i++) {
int var = min(left[i - 1], right[i + 1]);
if (var > arr[i]) {
water += var - arr[i];
}
}
return water;
}
// Main Function
int main()
{
int arr[] = {1, 1, 5, 2, 1, 8 , 1, 0, 2, 1, 2, 6};
int m = sizeof(arr) / sizeof(arr[0]);
printf("%d", RainWater(arr, m));
return 0;
}