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
![Counting Valleys in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 image 65](https://technorj.com/wp-content/uploads/2021/12/image-65.png)
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(); } }
tadalafil 20mg tadalafil 20mg for sale buy erectile dysfunction meds
buy cefadroxil 500mg buy proscar pills purchase finasteride online
flagyl where to buy buy flagyl tablets purchase cephalexin online
buy bimatoprost tablets order methocarbamol 500mg generic trazodone brand
buy catapres 0.1 mg sale spiriva 9 mcg without prescription order spiriva 9mcg generic
order azipro 250mg without prescription purchase azithromycin without prescription order generic gabapentin 800mg
order furosemide for sale order monodox online cheap albuterol inhaler
levitra 20mg cost buy tizanidine 2mg online buy plaquenil pills for sale
buy generic ramipril amaryl generic buy etoricoxib online
order levitra without prescription order levitra 20mg for sale how to buy hydroxychloroquine
asacol uk oral azelastine cost irbesartan 300mg
benicar usa buy divalproex no prescription purchase divalproex online
clobetasol uk buy cheap amiodarone buy cordarone tablets
buy generic clobetasol buy generic buspar 10mg buy cordarone pills
buy diamox 250mg sale purchase isosorbide generic buy imuran 25mg without prescription
order carvedilol sale cenforce 50mg cost buy aralen 250mg pills
buy digoxin generic molnupiravir 200 mg drug order molnupiravir 200 mg for sale
how to get naproxen without a prescription buy generic lansoprazole prevacid order
proventil uk protonix 40mg for sale order phenazopyridine 200 mg for sale