public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number of steps”);
int s = sc.nextInt();
System.out.println(“Enter path in D or U:”);
String path = sc.next();
sc.close();
int level=0;
int count=0;
int temp=0;
for (int i = 0; i < path.length(); i++) {
if (path.charAt(i)=='D') {
temp=level;
level–;
}else {
temp=level;
level++;
}
//velly count will increase if level earlier value 'temp' is negative and current
//'level' is zero. this indicates that we had came across a velly.
if (level==0 && temp<0) {
count++;
}
}
System.out.println("No of velly "+count);
}
#include
using namespace std;
int main(){
int n;
string str;
cin>>str;
int d=0, count=0;
for( int i=0; i0){
if(d-1 ==0){
count++;
d–;
}else{
d–;
}
}else{
d–;
}
}
}
cout<<count<<endl;
return 0;
}
#include
#include
using namespace std;
int main()
{
int n;
cin >> n;
char s[n];
for (int i = 0; i > s[i];
}
stack stk;
int valleys = 0;
for (int i = 0; i < n; i++)
{
if (s[i] == 'D')
{
stk.push(s[i]);
}
else
{
stk.pop();
if (stk.empty())
{
valleys++;
}
}
}
cout << valleys << endl;
return 0;
}
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number of steps”);
int s = sc.nextInt();
System.out.println(“Enter path in D or U:”);
String path = sc.next();
sc.close();
int level=0;
int count=0;
int temp=0;
for (int i = 0; i < path.length(); i++) {
if (path.charAt(i)=='D') {
temp=level;
level–;
}else {
temp=level;
level++;
}
//velly count will increase if level earlier value 'temp' is negative and current
//'level' is zero. this indicates that we had came across a velly.
if (level==0 && temp<0) {
count++;
}
}
System.out.println("No of velly "+count);
}
}
#include
#include
using namespace std;
int main()
{
printf(“Hello World”);
int a=0,v,m,n,i; //altitude
string s;
char d,u;
cin>>d;
cin>>u;
cout<>n;
cout<>s;
//set variables lagenge bhai
int x=0,y=0;
for(i=0;i<n;i++)
{
if(s[i]==u)
{
a++;
}
if(s[i]==d)
{
a–;
}
if(a0&&y==0)
{
m++;
y=1; //matlab mountain chadraha hai
}
if(a==0)
{
x=0;
y=0;
}
}
cout<<"number of valleys"<<v;
cout<<"number of mountains"<<m;
return 0;
}
public static int countingValleys(int steps, String path) {
// Write your code here
int valleyCounter =0;
int altitude =0;
for(int i=0;i<steps;i++){
char ch = path.charAt(i);
if(ch =='U'){
altitude++;
if(altitude == 0)
valleyCounter++;
}else{
altitude–;
}
}
return valleyCounter;
}
}