Sell All the Cars goes wrong

#include
using namespace std;

int main() {
// your code goes here
int T;
cin>>T;
while(T–)
{
int N;
cin>>N;
long int arr[N];
int counter = 0;
long int sum = 0;
for(int i=0; i<N; i++)
{
cin>>arr[i];
//counter initially zero
//counter increses by one for one sell
if(arr[i]>counter)
{
sum = (arr[i]-counter)%1000000007+sum)%1000000007;
counter++;
}
}
cout<<sum%1000000007<<endl;
}
return 0;
}
//Question link is CARSELL Problem - CodeChef

Actually your approach is not all correct.
Suppose test case ->1
N=5 A[i]'s => 1 2 3 4 5
According to your code → you will get answer=5 but this is not the correct answer as maximum profit can be 9 in this test case.
What you need to do is sort your array(in decreasing order) initially so that your if condition runs maximum time hence maximizing profit.
Just try to figure it out by yourself.
Here is my neat and simple code ==> CodeChef: Practical coding for everyone
This is your corrected code==>CodeChef: Practical coding for everyone
Hope it helps!!

Thank you so much!!!

1 Like