Recent Post

Counting Valleys.c-HackerRank

Problem-Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly n steps. For every step he took, he noted if it was an uphill, U, or a downhill, D step. Gary's hikes start and end at sea level and each step up or down represents a 1 unit change in altitude. We define the following terms:

·        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.

·        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

Comments

Post a Comment

Popular posts from this blog

Caesar Cipher.c-HackerRank

Bon AppƩtit.c-HackerRank

Electronics Shop.c-HackerRank