Given an array of size n that has the following specifications:

- Each element in the array contains either a policeman or a thief.
- Each policeman can catch only one thief.
- A policeman cannot catch a thief who is more than K units away from the policeman.

We need to find the maximum number of thieves that can be caught.

Examples:

Input : arr[] = {'P', 'T', 'T', 'P', 'T'}, k = 1. Output : 2. Here maximum 2 thieves can be caught, first policeman catches first thief and second police- man can catch either second or third thief. Input : arr[] = {'T', 'T', 'P', 'P', 'T', 'P'}, k = 2. Output : 3. Input : arr[] = {'P', 'T', 'P', 'T', 'T', 'P'}, k = 3. Output : 3.

A **brute force** approach would be to check all feasible sets of combinations of police and thief and return the maximum size set among them. Its time complexity is exponential and it can be optimized if we observe an important property.

An **efficient** solution is to use a greedy algorithm. But which greedy property

to use can be tricky. We can try using: “For each policeman from the left catch the nearest possible thief.” This works for example three given above but fails for example two as it outputs 2 which is incorrect.

We may also try: “For each policeman from the left catch the farthest possible thief”. This works for example two given above but fails for example three as it outputs 2 which is incorrect. A symmetric argument can be applied to show that traversing for these from the right side of the array also fails. We can observe that thinking irrespective of the

policeman and focusing on just the allotment works:

1. Get the lowest index of policeman p and thief t. Make an allotment

if |p-t| <= k and increment to the next p and t found.

2. Otherwise increment min(p, t) to the next p or t found.

3. Repeat above two steps until next p and t are found.

4. Return the number of allotments made.

Below is the implementation of the above algorithm. It uses vectors to

store the indices of police and thief in the array and processes them.

## C++

`// C++ program to find maximum number of thieves ` `// caught ` `#include <iostream> ` `#include <vector> ` `#include <cmath> ` ` ` `using` `namespace` `std; ` ` ` `// Returns maximum number of thieves that can ` `// be caught. ` `int` `policeThief(` `char` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `res = 0; ` ` ` `vector<` `int` `> thi; ` ` ` `vector<` `int` `> pol; ` ` ` ` ` `// store indices in the vector ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] == ` `'P'` `) ` ` ` `pol.push_back(i); ` ` ` `else` `if` `(arr[i] == ` `'T'` `) ` ` ` `thi.push_back(i); ` ` ` `} ` ` ` ` ` `// track lowest current indices of ` ` ` `// thief: thi[l], police: pol[r] ` ` ` `int` `l = 0, r = 0; ` ` ` `while` `(l < thi.size() && r < pol.size()) { ` ` ` ` ` `// can be caught ` ` ` `if` `(` `abs` `(thi[l] - pol[r]) <= k) { ` ` ` `res++; ` ` ` `l++; ` ` ` `r++; ` ` ` `} ` ` ` ` ` `// increment the minimum index ` ` ` `else` `if` `(thi[l] < pol[r]) ` ` ` `l++; ` ` ` `else` ` ` `r++; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `k, n; ` ` ` ` ` `char` `arr1[] = { ` `'P'` `, ` `'T'` `, ` `'T'` `, ` `'P'` `, ` `'T'` `}; ` ` ` `k = 2; ` ` ` `n = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `cout << ` `"Maximum thieves caught: "` ` ` `<< policeThief(arr1, n, k) << endl; ` ` ` ` ` `char` `arr2[] = { ` `'T'` `, ` `'T'` `, ` `'P'` `, ` `'P'` `, ` `'T'` `, ` `'P'` `}; ` ` ` `k = 2; ` ` ` `n = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` `cout << ` `"Maximum thieves caught: "` ` ` `<< policeThief(arr2, n, k) << endl; ` ` ` ` ` `char` `arr3[] = { ` `'P'` `, ` `'T'` `, ` `'P'` `, ` `'T'` `, ` `'T'` `, ` `'P'` `}; ` ` ` `k = 3; ` ` ` `n = ` `sizeof` `(arr3) / ` `sizeof` `(arr3[0]); ` ` ` `cout << ` `"Maximum thieves caught: "` ` ` `<< policeThief(arr3, n, k) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum number of ` `// thieves caught ` `import` `java.util.*; ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns maximum number of thieves ` ` ` `// that can be caught. ` ` ` `static` `int` `policeThief(` `char` `arr[], ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `int` `res = ` `0` `; ` ` ` `ArrayList<Integer> thi = ` `new` `ArrayList<Integer>(); ` ` ` `ArrayList<Integer> pol = ` `new` `ArrayList<Integer>(); ` ` ` ` ` `// store indices in the ArrayList ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(arr[i] == ` `'P'` `) ` ` ` `pol.add(i); ` ` ` `else` `if` `(arr[i] == ` `'T'` `) ` ` ` `thi.add(i); ` ` ` `} ` ` ` ` ` `// track lowest current indices of ` ` ` `// thief: thi[l], police: pol[r] ` ` ` `int` `l = ` `0` `, r = ` `0` `; ` ` ` `while` `(l < thi.size() && r < pol.size()) { ` ` ` ` ` `// can be caught ` ` ` `if` `(Math.abs(thi.get(l) - pol.get(r)) <= k) { ` ` ` `res++; ` ` ` `l++; ` ` ` `r++; ` ` ` `} ` ` ` ` ` `// increment the minimum index ` ` ` `else` `if` `(thi.get(l) < pol.get(r)) ` ` ` `l++; ` ` ` `else` ` ` `r++; ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `k, n; ` ` ` `char` `arr1[] =` `new` `char` `[] { ` `'P'` `, ` `'T'` `, ` `'T'` `, ` ` ` `'P'` `, ` `'T'` `}; ` ` ` `k = ` `2` `; ` ` ` `n = arr1.length; ` ` ` `System.out.println(` `"Maximum thieves caught: "` ` ` `+policeThief(arr1, n, k)); ` ` ` ` ` `char` `arr2[] =` `new` `char` `[] { ` `'T'` `, ` `'T'` `, ` `'P'` `, ` `'P'` `, ` ` ` `'T'` `, ` `'P'` `}; ` ` ` `k = ` `2` `; ` ` ` `n = arr2.length; ` ` ` `System.out.println(` `"Maximum thieves caught: "` ` ` `+policeThief(arr2, n, k)); ` ` ` ` ` `char` `arr3[] = ` `new` `char` `[]{ ` `'P'` `, ` `'T'` `, ` `'P'` `, ` `'T'` `, ` ` ` `'T'` `, ` `'P'` `}; ` ` ` `k = ` `3` `; ` ` ` `n = arr3.length; ` ` ` `System.out.println(` `"Maximum thieves caught: "` ` ` `+policeThief(arr3, n, k)); ` ` ` `} ` `} ` ` ` `/* This code is contributed by Danish kaleem */` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find maximum ` `# number of thieves caught ` ` ` `# Returns maximum number of thieves ` `# that can be caught. ` `def` `policeThief(arr, n, k): ` ` ` `i ` `=` `0` ` ` `l ` `=` `0` ` ` `r ` `=` `0` ` ` `res ` `=` `0` ` ` `thi ` `=` `[] ` ` ` `pol ` `=` `[] ` ` ` ` ` `# store indices in list ` ` ` `while` `i < n: ` ` ` `if` `arr[i] ` `=` `=` `'P'` `: ` ` ` `pol.append(i) ` ` ` `elif` `arr[i] ` `=` `=` `'T'` `: ` ` ` `thi.append(i) ` ` ` `i ` `+` `=` `1` ` ` ` ` `# track lowest current indices of ` ` ` `# thief: thi[l], police: pol[r] ` ` ` `while` `l < ` `len` `(thi) ` `and` `r < ` `len` `(pol): ` ` ` ` ` `# can be caught ` ` ` `if` `(` `abs` `( thi[l] ` `-` `pol[r] ) <` `=` `k): ` ` ` `res ` `+` `=` `1` ` ` `l ` `+` `=` `1` ` ` `r ` `+` `=` `1` ` ` ` ` `# increment the minimum index ` ` ` `elif` `thi[l] < pol[r]: ` ` ` `l ` `+` `=` `1` ` ` `else` `: ` ` ` `r ` `+` `=` `1` ` ` ` ` `return` `res ` ` ` `# Driver program ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `arr1 ` `=` `[` `'P'` `, ` `'T'` `, ` `'T'` `, ` `'P'` `, ` `'T'` `] ` ` ` `k ` `=` `2` ` ` `n ` `=` `len` `(arr1) ` ` ` `print` `((` `"Maximum thieves caught: {}"` `. ` ` ` `format` `(policeThief(arr1, n, k)))) ` ` ` ` ` `arr2 ` `=` `[` `'T'` `, ` `'T'` `, ` `'P'` `, ` `'P'` `, ` `'T'` `, ` `'P'` `] ` ` ` `k ` `=` `2` ` ` `n ` `=` `len` `(arr2) ` ` ` `print` `((` `"Maximum thieves caught: {}"` `. ` ` ` `format` `(policeThief(arr2, n, k)))) ` ` ` ` ` `arr3 ` `=` `[` `'P'` `, ` `'T'` `, ` `'P'` `, ` `'T'` `, ` `'T'` `, ` `'P'` `] ` ` ` `k ` `=` `3` ` ` `n ` `=` `len` `(arr3) ` ` ` `print` `((` `"Maximum thieves caught: {}"` `. ` ` ` `format` `(policeThief(arr3, n, k)))) ` ` ` `# This code is contributed by `jahid_nadim` ` |

*chevron_right*

*filter_none*

Output:

Maximum thieves caught: 2 Maximum thieves caught: 3 Maximum thieves caught: 3

The time complexity of the above approach in **O(N)** where N is the size of the array.

This article is contributed by **Satish Srinivas**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Modify given array to make sum of odd and even indexed elements same
- Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array
- Count maximum non-overlapping subarrays with given sum
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Minimize subarray increments/decrements required to reduce all array elements to 0
- Substring of length K having maximum frequency in the given string
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Rearrange an array to make similar indexed elements different from that of another array
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Swap the elements between any two given quadrants of a Matrix
- National Instruments Interview Experience (On-Campus)
- Maximize the sum of Kth column of a Matrix
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Maximized partitions of a string such that each character of the string appears in one substring