Mon
1
Q:

counting valleys hackerrank solution in c++

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

int main() {
    int n,updown = 0,res = 0;
    char c;
    cin >> n;
    
    for(int i = 0; i++ < n;){
        cin >> c;
        if(c == 'U')updown++;
        else updown--;
        if(updown == 0 && c == 'U')res++;
    }
    cout << res << endl;
    return 0;
}
0

New to Communities?

Join the community