What is the problem in my logic?

#include <algorithm>
#include <iostream>

int main() {
    int N = 0, largest = 0, largest_index = 0, left_bound_index = 0,
        first_non_zero_index = 0, flag = 0, right_bound_index = 0,
        water_harvested = 0;
    std::cin >> N;
    int *arr = new int[N];
    for (int i = 0; i < N; ++i) std::cin >> arr[i];
    for (int i = 0; i < N && flag == 0; ++i) {
        if (arr[i] != 0) {
            first_non_zero_index = i;
            flag = 1;
            break;
        }
    }
    left_bound_index = first_non_zero_index;
    for (int i = first_non_zero_index; i < N; ++i) {
        largest = 0;
        for (int j = left_bound_index + 1; j < N; ++j) {
            if (arr[j] > largest) {
                largest = arr[j];
                largest_index = j;
            }
        }
        right_bound_index = largest_index;
        for (int j = left_bound_index + 1; j <= right_bound_index - 1; ++j) {
            if (arr[left_bound_index + 1] >= arr[left_bound_index])
                continue;
            else
                water_harvested +=
                    std::min(arr[left_bound_index], arr[right_bound_index]) -
                    arr[j];
        }
        left_bound_index = right_bound_index;
    }
    std::cout << water_harvested;
}

Hey @moon check for test case
12
0 1 0 2 1 0 1 3 2 1 2 1
Expected output is: 6
Yours is giving is: 2
To make things easier make 2 arrays one which will store leftmost maximum block and another which will store rightmost maximum block
Then find the minimum of both and subtract it with height. You will get the desired output.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.