#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int
policeThief(
char
arr[],
int
n,
int
k)
{
int
pol = -1, thi = -1, res = 0;
for
(
int
i = 0; i < n; i++) {
if
(arr[i] ==
'P'
) {
pol = i;
break
;
}
}
for
(
int
i = 0; i < n; i++) {
if
(arr[i] ==
'T'
) {
thi = i;
break
;
}
}
if
(thi == -1 || pol == -1)
return
0;
while
(pol < n && thi < n) {
if
(
abs
(pol - thi) <= k) {
pol = pol + 1;
while
(pol < n && arr[pol] !=
'P'
)
pol = pol + 1;
thi = thi + 1;
while
(thi < n && arr[thi] !=
'T'
)
thi = thi + 1;
res++;
}
else
if
(thi < pol) {
thi = thi + 1;
while
(thi < n && arr[thi] !=
'T'
)
thi = thi + 1;
}
else
{
pol = pol + 1;
while
(pol < n && arr[pol] !=
'P'
)
pol = pol + 1;
}
}
return
res;
}
int
main()
{
int
k, n;
char
arr1[] = {
'P'
,
'T'
,
'T'
,
'P'
,
'T'
};
k = 2;
n =
sizeof
(arr1) /
sizeof
(arr1[0]);
printf
(
"Maximum thieves caught: %d\n"
,
policeThief(arr1, n, k));
char
arr2[] = {
'T'
,
'T'
,
'P'
,
'P'
,
'T'
,
'P'
};
k = 2;
n =
sizeof
(arr2) /
sizeof
(arr2[0]);
printf
(
"Maximum thieves caught: %d\n"
,
policeThief(arr2, n, k));
char
arr3[] = {
'P'
,
'T'
,
'P'
,
'T'
,
'T'
,
'P'
};
k = 3;
n =
sizeof
(arr3) /
sizeof
(arr3[0]);
printf
(
"Maximum thieves caught: %d\n"
,
policeThief(arr3, n, k));
return
0;
}