CARVANS where i am going wrong PLEASE CHECK

I AM JUST CHECKING FOR WHICH TWO CONSECUTIVE PAIRS THE FIRST CAR SPEED IS GREATER THAN THAT OF SECOND ONE AND INCREMENTS THE COUNT. FIRST CAR WILL ALWAYS MOVE AT ITS MAX SPEED SO INITIAL VALUE OF COUNT IS SET TO ONE , BUT WHERE I AM GOING WRONG?

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

    int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
    	int t;
    	cin>>t;
    	while(t--){
    	    int n,count=1;
    	    cin>>n;
    	    long int arr[n];
    	    for(int i=0;i<n;i++)
    	    cin>>arr[i];
    	    
    	    for(int i=1;i<n;i++){
    	        if(arr[i-1]>arr[i])
    	        count++;
    	    }
    	    cout<<count<<endl;
    	}
    	return 0;
    }

consider a test case 4 8 5 3
as the first car speed is at 4 the second car has to reduce its speed at 4 so the third car cannot be at its maximum speed at 5 as the car in front of him is at 4 but by your code you will count the third car at its maximum speed.

hope it will clear your doubt.

5 Likes

have a look at this brother

yeah i got it… i had to check all the front cars for any one to have speed lower.
Thanks.

welcome! happy coding

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

	int t;
	cin>>t;
	while(t--){
	    int n,count=1;
	    cin>>n;
	    long int arr[n];
	    for(int i=0;i<n;i++)
	    cin>>arr[i];
        
        maximum = arr[0]  // maximum speed at 
                                                  present//
        count = 1   // first one will always be in its 
                                 maximum speed //
        for(int i=1;i<n;i++){
	        if(maximum  >=  arr[i]);
                    maximum = arr[i]
                    count ++
	             
	    }
	    cout<<count<<endl;
	}
	return 0;
}

// this should be the correct answer… hope you understand
// please don’t just copy and paste it there might be some error but the approach is correct.

you have closed the if with a semicolon just after that

1 Like

#yeah! there should be curly brackets…
#i told you there may be some errors but the approach is correct.

oh sorry my bad!

1 Like

i did a similar code,but i get wrong answer …can u help me figure this out …

if(n[i]>1){
for(j=1;j<n[i];j++)
{
if(speed[i][j-1]<speed[i][j])
{
speed[i][j]=speed[i][j-1];
}
else
{
count++;
}
}}
cout <<count<<endl;

you don’t have to check consecutive cars speed, but the minimum in a series(i.e. from starting to the cars position)…

if you don’t understand check below algo…

minimum = arr[0]

count = 1
for i -> 1 to n-1:
    if l[i] <= minimum: 
        minimum = l[i]
        count = count + 1 

hope this is helpfull

Thanks a lot for the hint! :slight_smile: