Recent Post
Counting Valleys.c-HackerRank
- Get link
- X
- Other Apps
·
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 Gary's sequence
of up and down steps during his last hike,
find and print the number of valleys he
walked through.
Solution-
#include <assert.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 n,i,count=0,up=0;
char s[10000000];
scanf("%d\n",&n);
for(i=0;i<10000001;i++)
{
scanf("%c",&s[i]);
}
for(i=0;i<10000001;i++)
{
if(s[i]=='U')
up++;
else
up--;
if(up==0&&s[i]=='U')
count++;
}
printf("%d",count);
}
Link to the problem:-
https://www.hackerrank.com/challenges/counting-valleys/problem
- Get link
- X
- Other Apps
Comments
Thanks...so helping for me
ReplyDeletewelcomeš
DeleteThis comment has been removed by the author.
ReplyDeleteNice
ReplyDeleteNd very helpful
thankuš
Delete