Hello **Programmers/Coders,** Today we are going to share ** solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java**. At Each Problem with Successful submission with

**all Test Cases Passed,**you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for **Counting Valleys** **in Java-HackerRank Problem**. We are providing the **correct and tested solutions** of coding problems present on **HackerRank**. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

**Introduction To Algorithm**

The word **Algorithm** means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.

**Advantages of Algorithms:**

- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

** Link for the Problem** – Counting Valleys – Hacker Rank Solution

Counting Valleys – Hacker Rank Solution

**Problem:**

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 UDDDUDUU

**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 – Hacker Rank Solution

import java.util.Scanner; /** * @author Techno-RJ * */ public class CountingValleys { static int countingValleys(int n, String s) { int valleyCounter = 0, altitude = 0; for (int i = 0; i < n; i++) { char ch = s.charAt(i); if (ch == 'U') { altitude++; if (altitude == 0) { valleyCounter++; } } else { altitude--; } } return valleyCounter; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String str = sc.next(); System.out.println(countingValleys(n, str)); sc.close(); } }