CROSBLK - Editorial

take the test case :
5,5,3,2,1
correct answer is 4
but your answer will be -1 which is wrong bcz it may be possible u get a[0] element duplicates before index 0 (where ur curr !=0). Try to take this hint nd correct ur answer, if not check(curr_in ==a[0] && curr!=0 then ans++) for -1 (check if(curr_in!=a[0]) then -1).

3 Likes

Thank you so much, I almost took 1 hour to debug this still did’nt understand my mistake and now when you told me it seems a very silly mistake

Thank you so much

2 Likes

Can anyone tell me why my code is giving WA on TC 2 ?? I have tried all possible cases but it looks fine to me.

// Author : shreyas
// McMurdo Station, Antartica
// Pengiun Intelligence Agency
// Date :

#include <bits/stdc++.h>
using namespace std;

#define fast                    ios_base::sync_with_stdio(false);  cin.tie(NULL);
#define time                    cerr<<"Time taken : " <<(float)clock()/CLOCKS_PER_SEC <<" secs"<<"\n"  ;
#define F                       first
#define S                       second
#define pb                      push_back
typedef long long int           ll  ;


void solve() {

    ll n ;
    cin >> n ;
    vector<ll>arr(n) ;

    for(ll i = 0 ; i<n; i++){
        cin >> arr[i]  ;
    }

    reverse(arr.begin(), arr.end())  ;
    ll maxi = *max_element(arr.begin() , arr.end())  ;

    ll cnt= 0  ;

    for(ll i = 0 ; i <n-1 ; i++){
        if(arr[i] <= arr[i+1]){
            // trace(i,i+1)  ;
            continue;
        }
        else{
            cnt++ ;
        }
    }

    if(arr[n-1] == maxi){
        cnt++ ;
    }
    else{
        cout << -1  <<"\n"  ;
        return ;
    }

    cout << cnt+1 <<"\n";
}


int32_t main() {

    fast ; time;
    
    int t = 1;
    cin >> t;

    while (t--) {
        solve()  ;
    }


    return 0  ;
}



The ans for this test case should be 3 na OR 4 ?
As it moves from 8 → 6 —> 5 → 4 => 3 moves.

#include<bits/stdc++.h>
#define ll long long
#define mp make_pair 
#define ff first
#define ss second
#define pyes cout<<"Yes"<<endl
#define pno cout<<"No"<<endl
#define pcyes cout<<"YES"<<endl
#define pcno cout<<"NO"<<endl
#define debug(x) cout<<"# "<<x<<endl
#define all(x) (x).begin(),(x).end()
#define f(i,s,e) for(int(i)= int(s); i<int(e);i++)
#define vi vector<ll>
#define MOD 1000000007
#define srt(v) sort(v.begin(), v.end())
#define pb push_back
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
char uc[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char lc[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

//Fast exponention
// ll pw(ll a, ll b){
//   ll r;
//   if(b==0) return 1;
//   r = pw(a,b/2);
//   r = (r*r)%M;
//   if(b%2) r = (r*a)%M;
//   return r;
// }
 
using namespace std;

ll int mod(ll x){if(x<=0){return -x;}else{return x;}}
ll int sq(ll x){return x*x;}
 
ll powermod(ll x, ll y, ll p){ll res = 1;x = x % p; if (x == 0){return 0;}
    while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}
    return res;}

//---------Sieve of Eratosthenes-----------------------//    
 
void SieveOfEratosthenes( vector<ll> &mem)
{ ll n = 100000 +1;
    
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
 
    for (ll p = 2; p * p <= n; p++)
    {
        
        if (prime[p] == true)
        {
            for (ll i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
 
     f(i,2,n)
     if(prime[i]){mem.pb(i);}
    
} 
 
ll fact(ll n)
{
    if (n == 0)
        return 1;
    return n * fact(n - 1);
}

//Sort a vector of pairs by the second entry!
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}

 //----------------------Solve----------------------------//

void solve(){
int n;cin>>n;
int a[n];
int s[n];
int max_ele = 0 ;
int f = 1;
f(i,0,n) {cin>>a[i];s[i]=a[i]; max_ele = max(max_ele, a[i]);}
//cout<<max_ele<<endl;
if(a[0]!= max_ele) f=0;
else
{
    f(i,1,n)
    {
        if(a[i]==max_ele && a[i]!=a[i-1]) {f=0;break;}
    }
}

int j = 1;
int t =1;
int cnt = 0;
if(f){
while(t!=n-1){
    int cur_max = 0;
f(i,j,n)
{
    if(a[i]>=cur_max)
    {
        cur_max = a[i];
        t = i;
    }
    
}
cnt++;
j = t+1;
//cout<<t<<endl;
}
}
if(f) cout<<cnt<<endl;
else cout<<-1<<endl;
}
 //----------------------Main----------------------------//

int main()
    {
       
      FIO;
     
         int t; cin>>t; while(t--)
            solve();    
     
        return 0;
    }
// in ceil always take float
// always look for edge cases like n == 1 

Please help why does this fail?

try this test case : 4 4 4 4 3
correct ans should be 2, ur code gives 1

nope…
test case: 8,8,8,6,5,5,4,4
for this movement will be from 8(initial index0) → 8(index 2) ->6->5(index 5)->4(last index 7)
answer become 4;
Please try to read and examine the problem once again.

1 Like

Start the loop from i=1 instead of 0, leaving the 0th elemnt.
change ans = s.size()-1 to ans = s.size(), as we have not included 0th element in stack.

Pls help me @rajsrivastava that why i’m getting WA. I tried all T.C

// Author : shreyas
// McMurdo Station, Antartica
// Pengiun Intelligence Agency
// Date :

#include <bits/stdc++.h>
using namespace std;

#define fast                    ios_base::sync_with_stdio(false);  cin.tie(NULL);
#define time                    cerr<<"Time taken : " <<(float)clock()/CLOCKS_PER_SEC <<" secs"<<"\n"  ;
#define F                       first
#define S                       second
#define pb                      push_back
typedef long long int           ll  ;


void solve() {

    ll n ;
    cin >> n ;
    vector<ll>arr(n) ;

    for(ll i = 0 ; i<n; i++){
        cin >> arr[i]  ;
    }

    reverse(arr.begin(), arr.end())  ;
    ll maxi = *max_element(arr.begin() , arr.end())  ;

    ll cnt= 0  ;

    for(ll i = 0 ; i <n-1 ; i++){
        if(arr[i] <= arr[i+1]){
            // trace(i,i+1)  ;
            continue;
        }
        else{
            cnt++ ;
        }
    }

    if(arr[n-1] == maxi){
        cnt++ ;
    }
    else{
        cout << -1  <<"\n"  ;
        return ;
    }

    cout << cnt+1 <<"\n";
}


int32_t main() {

    fast ; time;
    
    int t = 1;
    cin >> t;

    while (t--) {
        solve()  ;
    }


    return 0  ;
}






// Author : shreyas
// McMurdo Station, Antartica
// Pengiun Intelligence Agency
// Date :

#include <bits/stdc++.h>
using namespace std;

#define fast                    ios_base::sync_with_stdio(false);  cin.tie(NULL);
#define time                    cerr<<"Time taken : " <<(float)clock()/CLOCKS_PER_SEC <<" secs"<<"\n"  ;
#define F                       first
#define S                       second
#define pb                      push_back
typedef long long int           ll  ;


void solve() {

    ll n ;
    cin >> n ;
    vector<ll>arr(n) ;

    for(ll i = 0 ; i<n; i++){
        cin >> arr[i]  ;
    }
    reverse(arr.begin(), arr.end())  ;
    ll maxi = *max_element(arr.begin() , arr.end())  ;

    ll cnt= 0  ;
    int maximum = INT_MIN;
    for(ll i = 0 ; i <n-1 ; i++){
        if(arr[i] <= maximum){
            // trace(i,i+1)  ;
            continue;
        }
        else{
            cnt++ ;
            maximum = arr[i];
        }
    }

    if(arr[n-1]!=maxi){
    	cout<<"-1\n";
    	return;
    }

    cout << cnt <<"\n";
}




int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("inputf.txt","r",stdin);
		freopen("outputf.txt","w",stdout);
	#endif
	int tt;
	cin>>tt;
	while(tt--)	{
		solve();
	}
	return 0;
}

Ohkayy got it. Thanks

what is my mistake in this
https://www.codechef.com/viewsolution/50416136

@anurag41682 your code fails the test cases where Ai=0
for eg
1
3
5 3 0
Here your code gives SIGSEGV .

Thank you so much bro

i passed that testcase it still fails
https://www.codechef.com/viewsolution/50434891

No bro 2 is the correct answer.

1 Like

O yeah 2 is correct sorry , my bad .
And about the error , i am not able to debug it . You may ask some experienced person .

oh thanks for your effort

1 Like

hey buddy can you find which test case is failing in my program thanks
https://www.codechef.com/viewsolution/50434891

exact same approach even WA kindly see …
#include

using namespace std;
void answer(int a[],int n)
{
int count=0;
int maxi=a[n-1];

int i=n-1;
while(i>=0)
{

     if(maxi<a[i])
     {
         
         maxi=a[i];
         count++;
     }
     i--;
      
}
if(maxi==a[0])
cout<<count<<endl;
else 
cout<<-1<<endl;

}

int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{

    cin>>a[i];
    }
    answer(a,n);
}

}