COUNTING VALLEYS HACKERRANK SOLUTION IN C LANGUAGE
Introduction:
In this tutorial, we will cover detailed information about Counting Valleys Hackerrank problem solution along with an example in the C language.
Counting Valleys Problem Description:
A passionate hiker maintains detailed logs of their hikes. In the most recent hike, each step was precisely measured to determine if it was uphill or downhill. Every hike begins and ends at sea level, and every elevation gain or loss is represented by one unit of change in altitude. The following terms are defined by us:
- A mountain comprises an arrangement of stairs that rise steadily above sea level, beginning at sea level and concluding at sea level.
- A valley is a series of steps below the sea level that finish with a step back up to sea level and then begin again below sea level.
- Determine and keep track of the number of valleys traversed during a hike, given the order of steps that are up and down.
Example:
The hiker initially descends into a deep valley. After that, they ascend atop a mountain that is many units high. The trip finally comes to a conclusion when the hiker reaches sea level.
Returns:
int: the total number of valleys crossed
Input Format:
The hike's step count, expressed as an integer in the first line.
The path is described by a single string of letters will be in the second line.
Constraints:
- 2 <= steps <= 106
- Path [i] € { U, D }
Sample input:
8
UDDDUDUU
Output:
The hike can be depicted as follows if we designate a step up as /, _ as sea level, and a step down as \.
_/\ _
\ /
\/\/
Implementation of Counting Valleys Hacker rank problem:
# include <assert.h>
# include <ctype.h>
# include <limits.h>
# include <math.h>
# include <stdbool.h>
# include <stddef.h>
# include <stdint.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main() {
int num;
printf (" Enter the number of steps in the hike : ");
scanf (" %i", &num);
char string [num];
printf (" Enter the single string path of steps which describes the path : ");
scanf (" %s", string);
int level = 0, res = 0, valley = 0;
for (int i = 0; i < num; i++) {
if (string [i] == 'U') {
level++;
if (level == 0 && valley) {
valley = 0;
res++;
}
}
else if (string [i] == 'D') {
if (level == 0)
valley = 1;
level--;
}
}
printf (" %i ", res);
return 0;
}
Output:
Explanation for the above program:
Header files: For input and output, memory allocation, mathematical calculations, and string manipulations, these serve as the standard C libraries that are included.
Main function: The main function is where the program executes first.
Variable num: The total number of steps during the hike is entered by the user and is stored in an integer variable called num.
Input and Variable Declaration: To hold the step sequence, it defines a character arrays string of size num. For the path string, user input is obtained.
Initializes the variables:
Initializations are made to three variables:
level: Shows the height at which you are currently.
res: Holds the outcome or the number of valleys.
valley: A flag to show if the hiker is in a valley right now.
For loop: It goes over every step from the input string one by one.
Checking steps either as ‘U’ or ‘D’: Lift the level if the path is a 'U' shape (i.e., uphill). Reset the valley flag and increase the result "res" when the level drops to 0, and then the hiker has been in a valley (valley is true).
Processing the downhill Lower the level if the step is a 'D' (downhill). Change the valley flag to 1, signifying that the hiker is in a valley whenever the level is 0.
Printing the result: The total number of valleys (res) visited during the hike is printed at the end.
Return Statement: The main function yields 0 to signify successful program termination when the program terminates.
Conclusion:
In this tutorial, we have seen the solution for the Counting Valleys program from Hacker rank. The solution for the program is discussed in C language.