Bob and Walls

Bob the builder has constructed some walls of various lengths in a line. Now, he wants to count the total number of walls constructed and report that number to his boss. But the report that Bob made was wrong because he counted the total number of walls by standing on the leftmost side of the first wall. So he was only able to see some walls and not all because others were hidden behind. Can you predict the number of walls counted by Bob?

Example:

Input:  n = 5, a = [1,3,2,5,4]
Output: 3

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int bobAndWalls(int nint a[])
{

    int cnt = 1, max1 = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] > max1)
        {
            cnt++;
            max1 = a[i];
        }
    }
    return cnt;
}
int main()
{
    int n = 5;

    int a[n] = {13254};

    cout << bobAndWalls(n, a) << "\n";

    return 0;
}


No comments:

Post a Comment