Counting Valleys HackerRank Solution in C, C++, Java, Python

An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly steps, for every step it was noted if it was an uphill, , or a downhill, step. Hikes always start and end at sea level, and each step up or down represents a unit change in altitude. We define the following terms:

A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.
Given the sequence of up and down steps during a hike, find and print the number of valleys walked through.

Example

The hiker first enters a valley units deep. Then they climb out and up onto a mountain units high. Finally, the hiker returns to sea level and ends the hike.

Function Description

Complete the countingValleys function in the editor below.

countingValleys has the following parameter(s):

int steps: the number of steps on the hike
string path: a string describing the path
Returns

int: the number of valleys traversed

Input Format

The first line contains an integer , the number of steps in the hike.
The second line contains a single string , of characters that describe the path.

Constraints

Sample Input

8

Sample Output

1

Explanation

If we represent _ as sea level, a step up as /, and a step down as \, the hike can be drawn as:

_/\ _
\ /
\/\/

The hiker enters and leaves one valley.

 

Counting Valleys HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n;
    scanf("%i", &n);
    char str[n];
    scanf("%s", str);
    int level = 0, result = 0, valley = 0;
    for (int i = 0; i < n; i++) {
        if(str[i] == 'U') {
            level++;
            if(level == 0 && valley) {
                valley = 0;
                result++;
            }
        }
        else if(str[i] == 'D') {
            if(level == 0)
                valley = 1;
            level--;
        }
    }
    printf("%i", result);
    return 0;
}

Counting Valleys HackerRank Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int l;
    string str; cin>>l>>str;
    int height = 0;
    int count = 0;
    for(int i=0;i<l;i++){
        if (str[i]=='U') height++;
        else {
            if (height==0) count++;
            height--;
        }
    }
    if (height<0) count--;
    cout<<count<<endl;
    
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        String str = in.next();
        new Solver().solve(str);
    }
}

class Solver {
    public void solve (String str) {
        int currLevel = 0;
        int valley = 0;
        boolean isFound = false;
        for (int i=0; i<str.length(); i++) {
            if (str.charAt(i) == 'U') {
                currLevel++;
            } else {
                currLevel--;
            }

            if (currLevel < 0){
                isFound = true;
            }

            if (currLevel >= 0  && isFound) {
                isFound = false;
                valley++;
            }

        }
        System.out.println(valley);
    }
}

Counting Valleys HackerRank Solution in Python

n = raw_input()

l = raw_input().strip().lower()

valleys = 0
level = 0

for i in xrange(len(l)):
    if level == 0 and l[i] == 'd':
        valleys += 1
        
    if l[i] == 'u':
        level += 1
    else:
        level -= 1
print valleys

Attempt Counting Valleys HackerRank Challenge

Link – https://www.hackerrank.com/challenges/counting-valleys

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/electronics-shop-hackerrank-solution/

Leave a Comment