Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
expand_more
expand_more
format_list_bulleted
Question
Chapter 33.4, Problem 1E
Program Plan Intro
To identify the flaw in Professor W’s scheme which states that at most 6 points can reside in
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Given an 8-element array: A = {x1, X2, X3, X4, X5, X6, 7, x8}, we would like to find
its 3rd smallest element. It is known that x₁ and 8 are the two extreme elements:
max and min, but we do not know which one is the max and which one is the min.
It is also known that x4 and x5 are the two medians: left-median and right-median.
What is the minimum number of comparisons you need to find the 3rd smallest
element of the original array?
Ans:
You're given an array of N integers, and a
number K.
The maximum K-product of the array is the
maximum product of any K length subsequence of
the array. For example, the maximum 2-product of
the array [-5, 3, 4, -6] is 30 because the product of
the subsequence [-5, -6] is 30 and it is impossible to
achieve larger subsequence product.
What is the maximum K-product you can achieve?
Using JAVA programming output the max K-
product.
Input:
1
42
5 -3 -4 6
Output:
30
There are n cities connected by some number of flights. You are given an array of flights F[1, . . . , n], where F[i] = (Fromi, Toi, pi) indicates that there is a flight from city Fromi to city Toi with cost pi. You are also given three integers src, dst, and k, return the cheapest price from city src to city dst with at most k stops. If there is no such route, return -1.
Precisely define the subproblem.Provide the recurrence equation.Describe the algorithm in pseudocode to compute the optimal value.Describe the algorithm in pseudocode to print out an optimal solution.
Chapter 33 Solutions
Introduction to Algorithms
Ch. 33.1 - Prob. 1ECh. 33.1 - Prob. 2ECh. 33.1 - Prob. 3ECh. 33.1 - Prob. 4ECh. 33.1 - Prob. 5ECh. 33.1 - Prob. 6ECh. 33.1 - Prob. 7ECh. 33.1 - Prob. 8ECh. 33.2 - Prob. 1ECh. 33.2 - Prob. 2E
Ch. 33.2 - Prob. 3ECh. 33.2 - Prob. 4ECh. 33.2 - Prob. 5ECh. 33.2 - Prob. 6ECh. 33.2 - Prob. 7ECh. 33.2 - Prob. 8ECh. 33.2 - Prob. 9ECh. 33.3 - Prob. 1ECh. 33.3 - Prob. 2ECh. 33.3 - Prob. 3ECh. 33.3 - Prob. 4ECh. 33.3 - Prob. 5ECh. 33.3 - Prob. 6ECh. 33.4 - Prob. 1ECh. 33.4 - Prob. 2ECh. 33.4 - Prob. 3ECh. 33.4 - Prob. 4ECh. 33.4 - Prob. 5ECh. 33.4 - Prob. 6ECh. 33 - Prob. 1PCh. 33 - Prob. 2PCh. 33 - Prob. 3PCh. 33 - Prob. 4PCh. 33 - Prob. 5P
Knowledge Booster
Similar questions
- A wrestling tournament has 256 players. Each match includes 2 players. The winner each match will play another winner in the next round. The tournament is single elimination, so no one will wrestle after they lose. The 2 players that are undefeated play in the final game, and the winner of this match wins the entire tournament. How would you determine the winner? Here is one algorithm to answer this question. Compute 256/2 = 128 to get the number of pairs (matches) in the first round, which results in 128 winners to go on to the second round. Compute 128/2 = 64, which results in 64 matches in the second round and 64 winners, to go on to the third round. For the third round compute 64/2 = 32, so the third round has 64 matches, and so on. The total number of matches is 128 + 64 + 32+ .... Finish this process to find the total number of matches.arrow_forwardYou are given an array J[1 : n] which includes the scores of n players in a game. You are additionally given an array P[1 : m] with the score of m new players. Design and analyze an algorithm that given both arrays J and P, can find the rank of each player P inside the array J, i.e., for each P[i], determines what would be the rank of P[i] in the array consisting of all elements of J plus P[i]. Your algorithm should run in O((n + m) ·log n) time.arrow_forwardGiven two arrays a and b, where both arrays consisting of n positive integers. It is allowed to swap any element from array a with any element from b. Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k moves (swaps). Input • The first line of the test case contains two integers n and k (1sns30;0sksn) - where n is the number of elements in a and b, while k is the maximum number of moves you can do. • The second line of the test case contains n integers a1,a2,... an • The third line of the test case contains n integers b1,b2,..,bn Output Print the answer – the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k swaps. Examples: Ex 1: Ex 2: Ex 3: Input 55 53 40 55665 12345 2243 12543 10 9 10 10 9 2423 Output: 27 39 11 • In the first test case of the example, you don't need to swap anything. • In the second test case of the example, you can swap a1=1 with b1=10, a3=3 with b3=10…arrow_forward
- Given two arrays a and b, where both arrays consisting of n positive integers. It is allowed to swap any element from array a with any element from b. Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k moves (swaps). Input • The first line of the test case contains two integers n and k (1Sns30;0sksn) - where n is the number of elements in a and b, while k is the maximum number of moves you can do. • The second line of the test case contains n integers a1,a2,... an • The third line of the test case contains n integers b1,b2,..,bn Output Print the answer – the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k swaps. Examples: Ex 1: Ex 2: Ex 3: Input 55 53 40 55665 1234 5 2243 12543 10 9 10 10 9 2423 Output: 27 39 11 • In the first test case of the example, you don't need to swap anything. • In the second test case of the example, you can swap a1=1 with bi=10, a3=3 with b3=10…arrow_forwardArray x is [a, b, c, . . . , y] has 25 integers. To find median of medians we form five sub-arrays: [d, c, b, e, a], [i, f, h, j, g], [1, m, k, n, o], [p, s, q, r, t], [w, x, v, u, y]. We find median from these sub-arrays and generate new sorted array [k, h, q, b, v]. Median of new sorted array is q, so median of medians is q. List all elements guaranteed to be greater or equal to q.arrow_forwardYou have been given an array A of size N and an integer K. This array consists of N integers ranging from 1 to 10^7. Each element in this array is said to have a Special Weight. The special weight of an element a[i] is a[i]%K. You now need to sort this array in Non-Increasing order of the weight of each element, i.e the element with the highest weight should appear first, then the element with the second highest weight and so on. In case two elements have the same weight, the one with the lower value should appear in the output first. Input Format: The first line consists of two space separated integers N and K. The next line consists of N space separated integers denoting the elements of array A. Output Format: Print N space separated integers denoting the elements of the array in the order in which they are required. Constraints: 1≤N≤10^5 1≤A[i]≤10^7 1≤K≤10^7 Note: You need to print the value of each element and not their weight. SAMPLE INPUT 5 21 2 3 4 5 SAMPLE OUTPUT 1 3 5 2 4…arrow_forward
- in java ecplise . Implement a program that randomly generates 10 integers from -100 to 100,stores them in a 1D array, and finds their maximum value. Calculate theexecution time of finding a maximum. Repeat the test for 10,000 and10,000,000 numbers. Provide your results in the form of a table below andprovide a small summary. Implement a Java program that will populate a 5x5 matrix with randomlygenerated integers from 0 to 100.(1) print your matrix in a table form.(2) modify your code to multiply all even numbers by 10 and print the matrix.Your output of questions (1) and (2) should match the format of the followingsample outputs:arrow_forwardIn n=4, what set(s) of cube(s) is(are) equivalent to the set {0000, 0010, 01x1, 0110, 1000, 1010}? (equivalent= they cover the same set of minterms)arrow_forwardImagine that we want to keep track of friendships between n people. We can do this with an array of size nXn. Each row of the array represents the friends of an individual, with the columns indicating who has that individual as a friend. For example, if person j is a friend of person i, then we place a mark in column j of row i in the array. Likewise, we should also place a mark in column i of row j if we assume that friendship works both ways. What will be the space complexity of this problemarrow_forward
- You are given an integer array X[1, . . . , n]. You should move each element of X[1, . . . , n] into one of the two arrays A and B such that A and B are non-empty and average(A) = average(B). Return true if it is possible to achieve that and false otherwise. Note that for an array A, average(A) is the sum of all the elements of A over the length of A. Precisely define the subproblem.Provide the recurrence equation.Describe the algorithm in pseudocode to compute the optimal value.Describe the algorithm in pseudocode to print out an optimal solution.arrow_forwardCorrect answer will be upvoted else downvoted. Computer science. You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median. A median in an array of length n is an element which occupies position number ⌊n+12⌋ after we sort the elements in non-decreasing order. For example: median([1,2,3,4])=2, median([3,2,1])=2, median([2,1,2,1])=1. Subarray a[l..r] is a contiguous part of the array a, i. e. the array al,al+1,…,ar for some 1≤l≤r≤n, its length is r−l+1. Input The first line contains two integers n and k (1≤k≤n≤2⋅105). The second line contains n integers a1,a2,…,an (1≤ai≤n). Output Output one integer m — the maximum median you can get.arrow_forwardcan you solve this please ? Given an array of unsorted integers, you are requested to investigate whether there exist a pair of numbers in this array that has a sum equal to a given key or not. For example, if the arr = [8, 7, 2, 5, 3] and the key = 12, the answer will be "Yes" as the array has a pair (7 and 5) where their sum = 12 = key, otherwise "No" is returned. To solve this problem, the following idea can be used: i. Consider every pair in the given array and check if their sum = key. ii. Sort the array first, keeping two pointers to on the minimum and maximum of the array and then move from both directions toward the center of the array while checking whether the sum = key or not. Answer the following questions: a) Transform each of the above ideas (i) & (ii) into algorithm (pseudocode) and compute their complexities b) Critic these two ideas: [Which is better? Why?] c) Design a better solution with less complexity. [note: your idea should be…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- 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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education