
Concept explainers
Implement the Greedy
C++.
Using C++, implement the given problem below. (Greedy Algorithm)
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:
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.
Copy the code. Show the algorithm use. Don't copy code from other sites.

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 5 images

- A number is called monotone if it consists of repeated decimal digits. For example, 3333 and 7777 are monotone numbers. (i) (ii) Write a divide and conquer function (in pseudocode) with time complexity (n) named multiply_monotone that takes as input a two mono- tone numbers. We assume n is maximum length (number of digits) of the two monotone numbers, and in turn, the length of each of them (for simplicity) may be taken to be a power of 2. You may assume a number is represented as a string of digits. code is O(n). Using Master Theorem, argue that the time complexity of yourarrow_forwardImplement the following function, without using any data structure. /* Given two vectors of chars, check if the two vectors are permutations of each other, i.e., they contains same values, in same or different order.e.g., V1=[‘a’,’b’,’a’] and V2=[‘b’,’a’,’a’] stores same multi-set of data points: i.e., both contains two ‘a’, and one ‘b’. e.g., V3=[‘a’,’c’,’t’,’a’] and V4=[‘a’,’c’,’t’] are not same multi-set. V3 contains two ‘a’s, while V4 has only one ‘a’. Note: when considering multiset, the number of occurrences matters. @param list1, list2: two vectors of chars @pre: list1, list2 have been initialized @post: return true if list1 and list2 stores same values (in same or different order); return false, if not. */ bool SameMultiSet (vector<char> list1, vector<char> list2)arrow_forwardComputer Science Exercise: depth [★★] Write a function depth : 'a tree -> int that returns the number of nodes in any longest path from the root to a leaf. For example, the depth of an empty tree (simply Leaf) is 0, and the depth of tree t above is 3. Hint: there is a library function max : 'a -> 'a -> 'a that returns the maximum of any two values of the same type. please use Ocaml for the codingarrow_forward
- Solve this algorithm problem using Pythknarrow_forwardUsing C++, code the following problem: In this problem, we are given a knapsack of fixed capacity C. We are also given a list of N objects, each having a weight W(I) and profit P(I). We can put any subset of the objects into the knapsack, as long as the total weight of our selection does not exceed C. The goal is to maximize the total profit, which is the sum of the profits of each object we put into the knapsack. Example: Input: C = 3 W: 1 2 3 P: 100 5 50 Selected Items: 1 2 Explanation: Possible selections that can be put in the knapsack are: 1 with profit 100 2 with profit 5 3 with profit 50 1 and 2 with profit 105 Note that in this problem, there is no need that the some of weights be exactly C. Hence, in this example, 1 is a valid solution but since the profit is lower than the maximum, is not the final answer. Input Format First line is C. Second line is the number of objects, N. The next N lines, each line contains two numbers: the first number is the weight of an…arrow_forward1. Below, enter code to complete implementation of a recursive function sum allintegers(), which takes an input n and adds all integers preceding it, up to n: add all integers(n):arrow_forward
- All of the information is below(JAVA), I need this in an hour if possible, thank you!!arrow_forwardC++ only Example Input14 31 1 1 24 14 23 2Example Output432arrow_forwardWrite a fraction calculator program that adds, subtracts, multiplies, and di-vides fractions. Your program should check for the division by 0, have and use the following functions:(a) subtract - finds the reduced difference of a pair of given fractions, by makingthe second fraction negative then using the add function.(b) multiply - finds the reduced product of a pair of given fractions.(c) divide - finds the reduced quotient of a pair of given fractions by inverting the second fraction then using the multiply functionarrow_forward
- Write a fraction calculator program that adds, subtracts, multiplies, and di-vides fractions. Your program should check for the division by 0, have and use the following functions (a) reduce - reduces a given fraction.(b) flip - reduces a given fraction and flips the sign if the denominator is negative.(c) add - finds the reduced sum of a pair of given fractions.arrow_forwardWrite a function lis_rec(arr) that outputs the length of the longest increasing sequence and the actual longest increasing sequence. This function should use divide and conquer strategy to the solve the problem, by using an auxiliary function that is recursive. For example, one possibility is to define a recursive function, lis_rec(arr, i, prev), that takes the array arr, an index i, and the previous element index prev of LIS (which is part of the array arr before index i), and returns the length of the LIS that can be obtained by considering the subarray arr[i:]. Write a dynamic programming version ofthe function, lis_dp(arr), that outputs the length of the longest increasing sequence and the actual longest increasing sequence by using a table to store the results of subproblems in a bottom-up manner. Test the performance of the two functions on arrays of length n = 100, 500, 1000, 5000, 10000. Compare the running times and memory usage of the two functions.arrow_forward10 - JAVA CODE ONLYarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





