Failing Perf-Test-5

I have spent some time trying to figure out how to pass this testcase but I am not able to.

I have referred to the following posts :


I was failing one test case after checking, and I fixed it, so it passes locally.
But the same test case still fails during assessment.
I’m not sure if I am applying two pointer approach incorrectly but I highly doubt it.
Would appreciate it if someone could take a look at it.

Hey @S4G4R,

Let’s take an input and dry run your code. Assume all the given registration numbers are within distance of K. So let’s only consider numbers

Brokencars : [10 ,7]
Goodcars : [7, 12]

brokenptr is pointing to 10
goodptr is pointing to 7

Now your brokenCar pair holds <registrationNumber, 10>
and your goodCar pair holds <registrationNumber, 7>

We assumed all numbers are in K distance so we enter the first if block(Line Number 40 in your code)

now numberOfPeopleCanFit will be 7 as you are taking min(brokenCar.second, goodCar.second)

Now you update your both pairs and they will be in the following state
brokenCar = <registrationNumber, 3> ==>(10 - 7 made brokencar capacity as 3)
goodCar = <registrationNumber, 0> ==>(10 - 10 made goodcar capacity as 0)

Now your if condition (Line number 48) is true (i.e goodCar capacity is 0) so we do goodptr++

At this point you reach back to start of the while loop for next iteration(Observe your brokenptr is not updated anywhere so it still is at 0th index and goodptr is at 1st index)

here’s the problem. You are creating a new pair with brokenptr and goodptr which are at index 0 and 1 respectively

brokenCar = <registrationNumber, 7> (Notice that we are fetching value from arraylist and we did not update our array anywhere in previous iteration)
goodCar = <regisrationNumber, 12>

So, make sure you handle this and rest of your code looks OK to me.

Kudos to you for writing good code with neat variable names, Made my life easy to look for the bug.

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.