Suppose you are given an array A holding n distinct integers (negative values are allowed) in sorted order; in other words, A[i] < A[i + 1] for each i ∈ [0, n − 2]. We say the ith element is self referential if A[i] = i. Design an O(log n) time algorithm to determine if there is a self referencial element in the array. Your solution must include
a) Statement of your algorithm in plain English. (Pseudo-code is optional.)
b) Short proof of correctness.
c) Time complexity analysis: Derive recurrence and solve by unrolling
Please answer with pseudo code.
a. Algorithm is very simple. Since entire array is sorted in ascending order, we will perform binary search to find an index i such that A[i]=i if it exist. Below is the algorithm.
FIND_INDEX(A, n)
1. Begin = 1, End = n, Mid = (Begin + End)/2
2. While(Begin < End )
3...........If( A[Mid] ==Mid || A[Mid+1] == Mid)
4..................return ( A[Mid] ==Mid ? Mid : Mid+1 ) //Return the index among Mid and Mid+1 whichever is equal with index
5...........else if( A[Mid] > Mid) //Since every index i greater than Mid will have A[ i ] > i, hence search in lower half indices
6...................End = Mid
7............else
8...................Begin = Mid //otherwise search in upper half indices from Mid to End
9. Return FALSE //In case when we are unable to find such index
b. Proof of correctness:- Since entire elements are integers and they are distinct elements and the array is sorted. Hence at any index i , if A[ i ] > i , then for all index j >= i , A[ j ] > j will hold because there will be increment in value of element in array corresponding to every increment in index. Similarly at index i, if A[ i ] < i , then for every index j <= i , A[ j ] < j. Hence our binary search at every iteration decides to search for index i with A[ i ] = i based on whether A[Mid] > Mid or A[Mid] < Mid or A[Mid] = Mid. Hence our algorithm is correct.
c. Since our algorithm, reduces the search space by half in every iteration, hence the time complexity recurrence relation will be
T(n) = T(n/2) + O(1)
Which will be solved as
T(n) = T(n/2) + O(1) = T(n/22) + 2*O(1) = T(n/23) + 3*O(1) = ....= T(n/2k) + k*O(1)
If k = log2 n
Then,
T(n) = T(n/2k) + k*O(1) = T(1) + (log2 n)*O(1) = O(log2 n)
Please comment for any clarification.
Suppose you are given an array A holding n distinct integers (negative values are allowed) in...
Suppose that we are given a sorted array of distinct integers A[1, ......, n] and we want to decide whether there is an index i for which A[i] = i. Describe an efficient divide-and-conquer algorithm that solves this problem and explain the time complexity. 1. Describe the steps of your algorithm in plain English. 2. Write a recurrence equation for the runtime complexity. 3. Solve the equation by the master theorem.
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.
We know that binary search on a sorted array of size n takes O(log n) time. Design a similar divide-and-conquer algorithm for searching in a sorted singly linked list of size n. Describe the steps of your algorithm in plain English. Write a recurrence equation for the runtime complexity. Solve the equation by the master theorem.
1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.
(20 points) You are given an array A of distinct integers of size n. The sequence A[1], A[2], ..., A[n] is unimodal if for some index k between 1 and n the values increase up to position k and then decrease the reminder of the way until position n. (example 1, 4, 5, 7, 9, 10, 13, 14, 8, 6, 4, 3, 2 where the values increase until 14 and then decrease until 1). (a) Propose a recursive algorithm to...
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)
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:
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)
Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to...