CARVANS - Editorial

thats a beautiful solution

i think that in the place of : speed = max(speed,maxSpeed)
it should be : speed = min(speed,maxSpeed)

Hi,
Can anyone please suggest me. What I am doing wrong in my solution

That isn’t the proper link to the solution, please check it and send the correct one :slight_smile:

1 Like

#include
using namespace std;
void fastscan(int &number)
{
//variable to indicate sign of input number
bool negative = false;
register int c;

number = 0; 

// extract current character from buffer 
c = getchar(); 
if (c=='-') 
{ 
    // number is negative 
    negative = true; 

    // extract the next character from the buffer 
    c = getchar(); 
} 

// Keep on extracting characters if they are integers 
// i.e ASCII Value lies from '0'(48) to '9' (57) 
for (; (c>47 && c<58); c=getchar()) 
    number = number *10 + c - 48; 

// if scanned input has a negative sign, negate the 
// value of the input number 
if (negative) 
    number *= -1; 

}

int main() {
int t ;
fastscan(t);
for (int i =0;i<t; i++)
{
int n ;
fastscan(n);
int a[n],z= 0;
// cout <<"Vlaue of n = "<<n<<endl;
for (int j=0 ;j<n;j++)
{
fastscan(a[j]);
}
if (n==1)
cout<<n<<endl;
else{
for (int j = 0;j<n–;j++){
// cout<<"value of j = "<<j <<endl;
// cout <<"Vlaue of a[j] = "<<a[j]<<endl;
//cout <<"Vlaue of a[j+1] = "<<a[j+1]<<endl;

       if (a[j]>=a[j+1]){
           
           z++;
         // cout<<z<<endl;
       }
       else {
       a[j+1]=a[j];
    }
    }
   cout <<z+1<<endl;

}
}
}
//What is wrong in this code can anyone explain please

My Solution without using extra space

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

void solve() {
	int n, ans=0;
	cin>>n;

	vector<int> max_speed(n);

	for(int i=0;i<n;i++) {
		cin>>max_speed[i];
	}
	ans++;
	if(n==1) {
		cout<<ans<<endl;
		return;
	} else {
		for(int i=1;i<n;i++) {
			if(max_speed[i] <= max_speed[i-1]) {
				ans++;
			} else {
				max_speed[i] = max_speed[i] - (abs(max_speed[i] - max_speed[i-1]));
			} 
		}
	}
	cout<<ans<<endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin>>T;

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

	return 0;
}
`#include <bits/stdc++.h>

using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n,count=0,max=0,s=0;
cin>>n;
cin>>s;
max=s;
count++;
for(int i=0;i<n-1;i++){
cin>>s;
if(s<max){
count++;
max=s;
}
}
cout<<count<<endl;
}
return 0;
}
`

1 Like

Hello Every One,
I have written a code for this question & tested it on sample input. It works as expected. But when I submitted the solution I am getting WA for all test cases. I have checked excepted solutions and editorials but I am not getting where I am going wrong.
Please help me understand my mistake.
My solution is Here

Thanks in Advance.

1 Like

Why this solution was accepted

n = int(input())
count = 0
for i in range(n):
c = int(input())
s = list(map(int, input().split()))
ans = s[0]
for j in range(c):
    if s[j] <= ans:
        ans = s[j]
        count += 1
print(count)
count = 0

and not this one as both seems to be the same math after all

n = int(input())
count = 0

for i in range(n):
    c = int(input())
    s = list(map(int, input().split()))
    for j in range(1, c):
        if s[j] >= s[j-1]:
            count += 1
    print(c-count)
    count = 0
#include <bits/stdc++.h>

using namespace std;



int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long n;
    scanf("%lld",&n);
    while(n--){
            int minspeed=INT_MAX;
            int q;
            scanf("%d",&q);
            int ans=0;
            while(q--){
                    int speed;
                    scanf("%d",&speed);
                    if(speed<=minspeed){
                        minspeed=min(speed,minspeed);
                        ans++;
                    }
    }
    cout<<ans<<endl;
}
}

I did it without the use of arrays

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	// your code goes here
	
	int t; cin>>t;
	while(t--)
	{
	    int n; cin>>n;
	    int32_t mn = 10001, x = 10001, ans=0;
	    for(int i=0;i<n;++i)
	    {
	        cin>>x;
	        mn = min(x,mn);
	        if(mn>=x)
	        {
	            ans++;
	        }
	    }
	   // 45123
	   // 44111
	    cout<<ans<<'\n';
	}
	return 0;
}

I need to understand at which test case would this fail?

Can anyone explain why it is wrong solution, output is correct you can check

#include
using namespace std;

int main(){
int T,N,arr[N],max=1;
cin>>T;
while(T–){
cin>>N;
for (int i = 0; i < N; i++)
{
cin>>arr[i];
}
for (int i = 1; i < N; i++)
{
if (arr[i]<=arr[i-1])
{
max++;
}
}
cout<<max<<endl;
max=1;
}
return 0;
}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Edit:

Neither mind - I can figure it out. Pay attention to compiler warnings!

[simon@simon-laptop][13:37:52]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling ansh2002-CARVANS.cpp
Executing command:
  g++ -std=c++17 ansh2002-CARVANS.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
ansh2002-CARVANS.cpp: In function ‘int main()’:
ansh2002-CARVANS.cpp:5:18: warning: ‘N’ is used uninitialized in this function [-Wuninitialized]
    5 |     int T,N,arr[N],max=1;
      |                  ^
Successful

https://www.codechef.com/viewsolution/55952316

Thanks for your reply, I dont actually understand it but i found that i was missing an else condition in code which I corrected and it got submitted

For Python users:
All the best !!
n = int(input())
for i in range(n):
cars = int(input())
speed = [int(j) for j in input().split()]
min, c = speed[0], 1
for i in range(1, cars):
if speed[i] <= min:
c += 1
min = speed[i]
print(c)