CSES Problem Apartments

Problem
My Solution

3 testcases are showing Wrong Answers. I am wrong by just difference of 1.
Please help me find where I am wrong.

Hi,

When you start the program, if a[0] is more than both min and max, you are still adding 1 to the answer. This should not be happening

You can change the last condition, https://cses.fi/paste/68f0acfb5df3e858176be6/

Thanks for replying. I found the problem with my code.
Your reasoning is incorrect, In my code when a[j] reaches a[n] it goes out of range (j is less than n, still it can become n).
Here is updated solution.
Solution

1 Like

The reasoning was correct only. You should try it manually with the test cases that were failing if you have trouble understanding.

Of course, in addition to my reasoning, j must also go till n-1 and not n in the inner loop as you rectified in your later code. Even I was thinking of that before but since your code was giving WA and not runtime error, I did not point it out.

If you are to not change the last condition and only change the range of j to n-1, you would still get WA.

1 Like

thanks,
Take this test case
10 10 0
10 16 34 37 46 49 56 62 69 86
7 9 43 47 50 62 71 71 83 95
and update the conditional as below, you will understand.
And yes i have to include the a[j]>=min for correct answer but that has nothing to do with a[0].

    if(a[j]<=max){
        		cout << a[j] <<" "<< b[i] << "\n";
        	 c++;j++;
        	}

It will print the applicant’s price followed by appartment price.

1 Like

Thank you.
I understand correctly now.
Sorry, I spoke garbage before because I saw AC and became overconfident