## Hard level of brute force tactics.

So, here’s one of the hardest forms of brute force techniques that you may need to solve the challenge. But what’s interesting here is that you can take advantage of the previous challenges; experiences and knowledge. Let’s try in C!

It’s been a while coming back to the main coding course in my blog – *Be Excel in C Language*. Anyways, this is C’s recreating source code of what I’ve done in *Road to Google – Part 3 Trapping Rainwater Ⅰ: Brute Force*.

Without further adieu, let’s get into it!

## Question:

*Given an array of integers representing an elevation map where the width of each bar is 1, return how much rainwater can be trapped.*

## The given array:

[0,1,0,2,1,0,3,1,0,1,2]

## Explanation:

Please refer to the aforementioned post: *Road to Google – Part 3 Trapping Rainwater Ⅰ: Brute Force*.

## Coding solution:

This is the brute force solution to it.

## Executed result:

## Actual code:

```
#include <stdio.h>
#include <math.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
int waterCalc(int *nums, int len) {
int currentWater = 0;
int waterLevel = 0;
int totalAmount = 0;
for (int i = 0; i < len - 1; i++) {
int max_l = 0;
int max_r = 0;
int l = i;
int r = i;
int currentHeight = nums[i];
while(r < len) {
if (nums[r] > max_r) {
max_r = nums[r];
}
r++;
}
while(l >= 0) {
if (nums[l] > max_l) {
max_l = nums[l];
}
l--;
}
waterLevel = MIN(max_r, max_l) - currentHeight;
if (waterLevel > 0) {
totalAmount += waterLevel;
}
}
return totalAmount;
}
void main() {
int nums[11] = { 0,1,0,2,1,0,3,1,0,1,2 };
int len = sizeof(nums) / sizeof(nums[0]);
printf("%d\n",waterCalc(nums, len));
}
```

Afterthoughts:

Since I’ve been learning a lot in other languages, including Pythion and Kotlin, I tend to pay enough attention to this legacy language: C.