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 5.3, Problem 4E
Program Plan Intro
To show that each element
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Given an integer n and an array a of length n, your task is to apply the following mutation to a:
Array a mutates into a new array b of length n. For each i from 0 to n - 1, b[i] = a[i - 1] + a[i] + a[i + 1]. If some element in the sum a[i - 1] + a[i] + a[i + 1] does not exist, it should be set to 0. For example, b[0] should be equal to 0 + a[0] + a[1].
Algorthim of how to find the largest element missing in an unsorted array of n positive integers and the running time of the algorthim is in O(n).
Example of this is that the n=6 array C= [5,90,8,6,26,9] The largest element missing in the array is 7
Given an array of integers arr, sort the array by performing a series of pancake flips.
In one pancake flip we do the following steps:
Choose an integer k where 1 <= k <= arr.length.
Reverse the sub-array arr[0...k-1] (0-indexed).
For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3.
Print out the k-values corresponding to a sequence of pancake flips that sort arr.
Example 1:
Input: arr = [3,2,4,1]
Output: 4, 2, 4, 3
Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3.
Starting state: arr = [3, 2, 4, 1].
After 1st flip (k = 4): arr = [1, 4, 2, 3]
After 2nd flip (k = 2): arr = [4, 1, 2, 3]
After 3rd flip (k = 4): arr = [3, 2, 1, 4]
After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted.
Another potential solution is: Output = 3, 4, 2, 3, 1, 2, 1, 1 with a similar explanation. All potential solutions that solve the problem are…
Chapter 5 Solutions
Introduction to Algorithms
Knowledge Booster
Similar questions
- Let M(n) be the minimum number of comparisons needed to sort an array A with exactly n ele- ments. For example, M(1) = 0, M(2) = 1, and M(4) = 4. If n is an even number, clearly explain why M(n) = 2M(n/2) + n/2.arrow_forwardGiven an array, find the total number of inversions of it. If (i < j) and (A[i] > A[j]), then pair (i, j) is called an inversion of an array A. We need to count all such pairs in the array. For example, Input: A[] = [1, 9, 6, 4, 5] Output: The inversion count is 5 There are 5 inversions in the array: (9, 6), (9, 4), (9, 5), (6, 4), (6, 5)arrow_forwardDescribe an efecient in-place algorithm called Partition-Even-Odd(A) that partitions an array A in even and odd numbers. The algorithm must terminate with A containing all its even elements preceding all its odd elements. For example, for input A = [7, 17, 74, 21, 7, 9, 26, 10], the result might be A = [74, 10, 26, 17, 7, 21, 9, 7]. Partition-Even-Odd must be an in-place algorithm. What does this mean about your algorithm? (a) Write the pseudo-code for Partition-Even-Odd. (b) Characterize the complexity of Partition-Even-Odd. Brie y justify your answer. (c) Formalize the correctness of the partition problem as stated above, and prove that Partition-Even- Odd is correct using a loop-invariant.arrow_forward
- You are given an unsorted array A[1, ..., n].We know that the maximum (or minimum) element in the array can be found in (n-1) comparisons.Write an algorithm that finds both the maximum and the minimum element in the array in ~(3n/2) comparisons, i.e., (3n/2) + THETA(1) comparisons.arrow_forwardAlex had an array A of length N such that 1≤Ai≤N for all 1≤i≤N. He constructed another binary array B of length N in the following manner: Bi=1 if the frequency of element i in A is odd. Bi=0 if the frequency of element i in A is even. Such an array B is called the parity encoding array of A. For example, if A=[1,1,2,3], then B=[0,1,1,0]. Unfortunately, he completely forgot the array A and vaguely remembers the parity encoding array B. He is now wondering whether there exists any valid array A for which the parity encoding array is B. Can you help himf? Input Format The first line contains a single integer T — the number of test cases. Then the test cases follow. The first line of each test case contains an integer N — the size of the arrays A and B. The second line of each test case contains N space-separated integers B1,B2,…,BN denoting the parity encoding array B. Output Format For each test case, output YES if there exists any valid array A for which the parity encoding…arrow_forwardWrite a program that reads an array A of N elements containing only 0's and 1's. Your program should find the position of a 0 and replace it with a 1 to get the longest continuous sequence of 1's. Let this position of 0 be called P. Print P if such a 0 exists and print -1 if the original array A contains only 1's. Assume the array indexing starts from 0.arrow_forward
- Write a technique to locate the indices m and n in an array of integers such that sorting items m through n will sort the entire array. Find the smallest such sequence by minimising n - m. Example inputs: 1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, and 19; outputs: 3 and 9.arrow_forwardLet A and B be two arrays of length n, each containing a random permutation of the numbers from 1 to n. An inversion between the two permutations A and B is a pair of values (x, y) where the index of x is less than the index of y in array A, but the index of x is more than the index of y in array B. Design an algorithm which counts the total number of inversions between A and B that runs in O(n log n) time.arrow_forwardLet B be an array that is of size n >= 6 containing integers from 1 to n-5, inclusive, with exactly five repeated. Describe an O(n) algorithm for finding the five integers in B that are repeated.arrow_forward
- Given an integer array [A], efficiently find a descending sorted triplet such that: A[i]> A[i]> A[k] and 0 <=i<j <k <n, where n is the array size. For example, Input: A[] = {9, 1, 6, 7, 3, 4, 5) Output: Any one of the following triplets: (9, 7,5) (9, 7, 4) (9, 7, 3) (9, 6, 5) (9, 6, 4) (9, 6, 3) a) Design a brute-force algorithm to solve this problem complexity of your solution b) Design a more efficient algorithm to do the same task with less complexity and analyse the complexity of your solution. c) Develop a python code to implement your efficient algorithm. depend on the correctness of the code, indentation, comments, test-case] d) Prepare a brief report (250 words) comparing the two algorithmsarrow_forwardLet B be an array of n >= 6 numbers ranging from 1 to n-5, inclusive, with precisely five repetitions. Describe an O(n) method for determining the five repeated numbers in B.arrow_forwardLet A be an array of numbers. In the maximum sub-array problem, your goal is to determine the sub-array A[x . . . y] of consecutive terms for which the sum of the entries is as large as possible. For example, if A = [−2, −3, 4, −1, −2, 1, 5, −3], the maximum sub-array is [4, −1, 2, 1, 5], and the largest possible sum is S = 4 − 1 − 2 + 1 + 5 = 7. Suppose A = [1, 2, −4, 8, 16, −32, 64, 128, −256, 512, 1024, −2048]. Determine S, the largest possible sum of a sub-array of A.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