From 44d8fbfbafbc3d95358b439a432228713d0e5032 Mon Sep 17 00:00:00 2001 From: Sahil Tripathi <55251741+sahil2128@users.noreply.github.com> Date: Wed, 21 Jul 2021 14:20:17 +0530 Subject: [PATCH 1/2] Create Police_catch_thieves.cpp --- Police catch thieves/Police_catch_thieves.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Police catch thieves/Police_catch_thieves.cpp diff --git a/Police catch thieves/Police_catch_thieves.cpp b/Police catch thieves/Police_catch_thieves.cpp new file mode 100644 index 0000000..fd6cd1c --- /dev/null +++ b/Police catch thieves/Police_catch_thieves.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +//returning maximum number of thieves that can be caught +int policeThief(char arr[], int n, int k){ + int caught = 0; + vector thieves; + vector policemen; + for (int i = 0; i < n; i++) { // here we are stroing indices in the vectors + if (arr[i] == 'P') + policemen.push_back(i); + else if (arr[i] == 'T') + thieves.push_back(i); + } + int thief = 0, police = 0; // tracking the lower indices + while (thief < thieves.size() && police < policemen.size()) { + if (abs(thieves[thief] - policemen[police]) <= k) { // thieves that can be caught + caught++; + thief++; + police++; + } + else if (thieves[thief] < policemen[police]) + thief++; + else + police++; + } + return caught; +} +int main(){ + int k, n; + char arr2[] = {'P', 'T', 'T', 'P', 'P', 'T', 'T', 'T', 'T', 'P' }; + k = 2; + n = sizeof(arr2) / sizeof(arr2[0]); + cout << "Maximum number of thieves that can be caught by police is :"< Date: Wed, 21 Jul 2021 14:24:37 +0530 Subject: [PATCH 2/2] Create Readme.md --- Police catch thieves/Readme.md | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Police catch thieves/Readme.md diff --git a/Police catch thieves/Readme.md b/Police catch thieves/Readme.md new file mode 100644 index 0000000..3eea548 --- /dev/null +++ b/Police catch thieves/Readme.md @@ -0,0 +1,36 @@ +# POLICEMEN CATCH THIEVES + +Given an array of size n that has the following specifications: + +1-Each element in the array contains either a policeman or a thief. + +2-Each policeman can catch only one thief. + +3-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: +## Running Tests + +### Input + +```bash + arr[] = {'P', 'T', 'T', 'P', 'T'}, + k = 1. +``` +### Output + +```bash +2 +``` + +### Input +```bash +arr[] = {'T', 'T', 'P', 'P', 'T', 'P'}, + k = 2 +``` + +### Output +```bash +3 +```