Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions counting valley.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<stdio.h>
#include<string.h>

int main()
{
int n, i, x=0;

scanf("%d", &n);

char a[n];
int b[n];
Comment on lines +10 to +11
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable length arrays are bad imo


scanf("%s", a);


for(i=0;i<n;i++)
{
if(a[i]=='U'){
x++;
b[i]=x;
}
else
{
x--;
b[i]=x;
}
}
Comment on lines +18 to +27
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do the same thing in both branches?


x=0;

for(i=0;i<n;i++)
Comment on lines +29 to +31
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: spacing

{
if(b[i]<0 && b[i+1]==0) //checking number is negative, and when reach to next position it becomes zero, then valley created
x++;
}

printf("%d", x);

return 0;
}