Why the answer is 2?

In the second problem, where bhaiya took balls on number line. Why the final answer is 2?

Because as we are finding absolute difference, which means the order of occurrance of word 1 and word 2 does not matter than the answer should have been 1. Because there is one red ball at index 2 and one blue ball at index 3.

hello @LPLC0111

it will 2 only, the word kartik is present at index 0, the word teaches is present at index 2 so the differenc is 2-0 =2

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

I was talking about second problem which bhaiya took where he used balls.

is it in the same video ? if yes then pls mention the time stamp.

if in other then pls raise doubt from that particular video

Example discussed by bhaiya in this video only at 5:45

Still stuck on this. please reply??

Any update on this???

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

what is this ?? i am asking for update on this and you people are closing the doubt

Yes you are right, it should be 2 , since the algo is for absolute difference we will do this

index1 = -1,index2 = -1;
minDistance = Integer.MAX_VALUE
tempDistance = 0

for (string x in array of strings) 
    if strings[x].equals(targetString) 
        index1 = x;
    
    if strings[x].equals(targetString2)
        index2 = x;
    
    if index1 != -1 and index2 != -1  // both words have to be found
        tempDistance = absolute(index2 - index1)
        if tempDistance < minDistance
            then minDistance = tempDistance
       

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.