a)
Algorithm(A , start, end, n)
mid = start + (end - start)/2
if ((mid == 0 || A[mid-1] <= A[mid]) && (mid == n-1 || A[mid+1] <= A[mid]))
return mid
else if (mid > 0 && A[mid-1] > A[mid])
return Algorithm(A, start, (mid -1), n)
else
return Algorithm(A, (mid + 1), end, n)
b) T(n) = T(n/2) +1
We can solve using Masters theorem
=> a = 1, b = 2 and f(n) = 1
=> nlog 21 = n0 = 1
f(n) = g(n) => nlogb a log n
=> O(log n)
(20 points) You are given an array A of distinct integers of size n. The sequence...
Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your...
1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.
Let S be a sequence of n distinct integers stored in an array as array elements S[1], S[2], · · · , S[n]. Use the technique of dynamic programming to find the length of a longest ascending subsequence of entries in S. For example, if the entries of S are 11, 17, 5, 8, 6, 4, 7, 12, 3, then one longest ascending subsequence is 5, 6, 7, 12. Specifically: (a) define a proper function and find the recurrence for...
(13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conquer algorithm to find some i and j, where j > 1, such that A[j]-Ali] is maximized. For example, given A 6, 1,3,8,4,5, 12,6], the maximum value of AL] - Ali] for j > i is 12-1 11 where j -7 and i 2. Give the underlying recurrence relation for your algorithm and analyze its running time. You should carefully state all details of your algorithm:...
6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative. Give an algorithm that can find an index i such that 1 <i<n and T[i] = i, provided such an index exists. Your algorithm should take a time in O(lg n) in the worst case. Answers must be proven (or at least well justified)
An array A[1,2,... ,n is unimodal if its consists of an increasing sequence followed by sequence a decreasing sequence. More precisely, there exists an index k є {1,2,… ,n} such that there exists an indes . AlE]< Ali1 for all 1 i< k, and Ai]Ali 1 for all k< i< n A1,2,..,n] in O(logn) time the loop invariant (s) that your algorithm maintains and show why they lead to the correctness Give an algorithm to compute the maximum element of...
A sequence of n distinct values A[O..n – 1] is said to be downup if there is an index p with 0 < p < n such that the values of A decrease up to Aſp) and then increase for the remainder of the sequence. The index p of value Aſp) is the valley of the sequence. For example sequence 50, 10, 5, 2, 1, 20, 30 is downup with valley 4, since A[5] = 60 and the sequence decreases...
We are given an array A holding n integers, for some large n. The array is sorted, and the values in A range from -2147483648 to 2147483647, evenly distributed. Give Θ expressions for the following tasks: A. Running the insertion sort algorithm on the array A: B. Running the selection sort algorithm on the array A: C. Performing binary search for integer k which is not in A: D. Performing interpolation search for integer k not in A:
Plz show the ans step by step -Q8. [8 marks] You are given n distinct integers in an array arr. For finding the k-th smallest element in this array, there is a random algorithm shown as below. Based on this algorithm, answer the following questions. Algorithm kthSmallest (arr, left, right, k) 1: if left == right 2: return arrleft 3: pivot = random(left, right) 4: pivotnewpos = partition(arr, left, right, pivot) 5: while pivotnewpos left+ right-left+1), left + (right-left+1)] 6:...
Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)