Skip to content

Police and Thieves ​

Police and Thieves — GeeksforGeeks

Given an array of P (police) and T (thief) where each policeman can catch only one thief within k units of distance, find the maximum number of thieves that can be caught.

Approach ​

Keep a pointer for police, keep a pointer for thieves. Find the closest police index. Find the closest thief index. Check if the difference in absolute <= k -> caught, move both pointers. If not and police is less -> find next police else find next theif

Remarks ​

I did it by copying it to two arrays and then comparing.