Help me in solving WEIRDSUBARR problem

My issue

why this solution is failing can you please find the mistake.<3

My code

//Radhe Radhe
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
const ll M = 998244353;
ll add(ll x, ll y)  { return (x%M + y%M)%M; }
ll sub(ll x, ll y)  { return (x%M - y%M + M)%M;}
ll mul(ll x, ll y)  { return (x%M * y%M)%M; }
void print(vector<ll>v1){
for(auto x:v1){cout<<x<<" ";}
cout<<endl;}
ll binexp(ll a,ll b){
ll res = 1;
while(b){
if(b&1)res = ((res%M)*(a%M)%M);
a = ((a%M)*(a%M)%M);
 b>>=1LL;
}
return res%M;
}
void Radhe(){
 ll t;
 cin>>t;
 while(t--){
     ll n;
     cin>>n;
     vector<int>v1(n);
     for(int i=0;i<n;i++){
        cin>>v1[i];
     }
     vector<ll>ans;
     for(int i=0;i<n;i++){
        int cnt = 1;
        while(i<n-1 && v1[i]>v1[i+1]){
            cnt++;
            i++;
        }
        while(i<n-1 && v1[i]<v1[i+1]){
            cnt++;
            i++;
        }
        ans.push_back(cnt);
     }
     ll res = 0;
     res+=ans.size()-1;
     for(auto x:ans){
        // cout<<x<<" ";
        res += x*(x+1)/2;
     }
    //  cout<<endl;
     
     cout<<res<<endl;
 }
}
int main(){
Radhe();
return 0;
}

Problem Link: Weird Subarrays Practice Coding Problem - CodeChef

1 Like

@lone_wulf
here plzz refer my c++ code

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    long long int n;
	    cin>>n;
	    int a[n];
	    long long int ans=0,cnt=1;
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	    }
	    for(int i=1;i<n;i++)
	    {
	        if(a[i]<a[i-1])
	        {
	            cnt++;
	        }
	        else
	        {
	            while(i<n and a[i]>a[i-1])
	            {
	                i++;
	                cnt++;
	            }
	            i--;
	            ans+=(cnt*(cnt-1))/2;
	            cnt=1;
	        }
	    }
	    ans+=(cnt*(cnt-1))/2;
	   // cnt=1;
	   // for(int i=1;i<n;i++)
	   // {
	   //     if(a[i]<a[i-1])
	   //     {
	   //         cnt++;
	   //     }
	   //     else
	   //     {
	   //         ans+=(cnt*(cnt-1))/2;
	   //         cnt=1;
	   //     }
	   // }
	   // ans+=(cnt*(cnt-1))/2;
	    ans+=n;
	    cout<<ans<<endl;
	}
	return 0;
}