Given a sorted array of distinct integersA[1,...,n] , you want to find out whether there is an indexi for which A[i]=i. Give a divide-and-conquer algorithm that runs in time O(logn).

Short Answer

Expert verified

By using binary search, only certain that there is no such value to the left of the middle element if array[mid]<=startindex, where mid is index of middle element, and start index is the start of the array. A divide-and-conquer algorithm is constructed with run time O(logn).

Step by step solution

01

Binary search algorithm and Divide-and-conquer algorithm.

Binary Search is used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity toO(logn) .Binary Search also known as half-interval search, logarithmic search, or binary chop.Binary Search Algorithm can be implemented in the following two ways,

1.Iterative Method

2. Recursive Method

02

Sorting algorithm to be use in given situation

Use modification of binary search here :

Basically we have to find index such that a[i]=iinothera[i]-i=0.

Now as the array is sorted , so all the indices j which is less than i will satisfy the property

:a[j]-j<=0

as the array is sorted so a[j] will be at least one less than a[i] and left index will also be at least one less than i hence the conclusion.

Similarly to the right of the last index element .

a[j]-j>=0.

So the key idea is :

mid=left+right/2where left and right are the boundary indices and then check whether mid is the last index ,index we are looking for using the above mentioned property.

For this we compare only its immediate left and right neighbours.

Given that the array is “SORTED”. We can more use of this property to improve the fine complexity to O(logn).

-1

2

5

7

11

20

30

Ex: A

Let’s say we to search “20” in our Array. First we go for middle element i.e.,7

Search if 7>20?? False

If 7>20teen we will only search on the right hand side of 7.

Then, we have to search only in

112030

Here, we are dividing the large problem into a sub-problem with less sited array.

Mid=20

Apply same procedure.

Here, we found the mid value as required 20. This approach is called “Binary Search”

03

Algorithm

Algorithm BinarySearch(arr[],min,max,key)

{

If (max<min)then

Else return false.

Mid=(max+min)/2

If arr[mid]>keythen

Return BinarySearch(arr,min,mid1,key)

Else if arr[mid]<keythen

ReturnBinarySearch(arr,mid+1,max,key)

Else

Return

End if

End if

}

array[mid] <= start index

p[1.n]is array of words frequencies of sizen.

for(s=1ton)for(i=0ton-s)j=i+sif(i=j)

T(i,j)=p[i]for(k=itoj+1)T(i,j)=T(i,j)+p[k]returnT(0,n-1)

This algorithm is constructed or take run time of O(logn).

Here, the analysis of algorithm is given below, binary search, only certain that there is no such value to the left of the middle element ifrole="math" localid="1659776120568" array[mid]<=startindex, where mid is index of middle element, and start index is the start of the array.And divide-and-conqueralgorithm is constructed with run timeO(logn)

Every fine we are dividing the array into half. i.e.,

Ifn=100, next call will be,

Calln=50,25,12,6,3,1base case.

We are performing logn operations.

Hence, time complexity: O(logn)

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

A kway merge operation. Suppose you have ksorted arrays, each with nelements, and you want to combine them into a single sorted array ofkn elements.

(a)Here’s one strategy: Using the merge procedure from Section 2.3, merge the first two arrays, then merge in the third, then merge in the fourth, and so on. What is the time complexity of this algorithm, in terms of kand n?

(b) Give a more efficient solution to this problem, using divide-and-conquer.

Question: Solve the following recurrence relations and give a bound for each of them.

(a)T(n)=2T(n/3)+1(b)T(n)=5T(n/4)+n(c)T(n)=7T(n/7)+n(d)T(n)=9T(n/3)+n2(e)T(n)=8T(n/2)+n3(f)T(n)=49T(n/25)+n(3/2)logn(g)T(n)=T(n-1)+2(h)T(n)=T(n-1)+nc,whereisaconstant(i)T(n)=T(n-1)+cn,whereissomeconstant(j)T(n)=2T(n-1)+1(k)T(n)=T(n)+1

A binary tree is full if all of its vertices have either zero or two children. Let Bndenote the number of full binary trees with n vertices. (a)By drawing out all full binary trees with 3, 5, or 7 vertices, determine the exact values of B3, B5, and B7. Why have we left out even numbers of vertices, like B4?

(b) For general n, derive a recurrence relation for Bn.

(c) Show by induction that Bnis Ω(2n).

You are given two sorted lists of size mandn. Give an O(logm+logn)time algorithm for computing the k th smallest element in the union of the two lists.

You are given an array of nelements, and you notice that some of the elements are duplicates; that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O(nlogn) .

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free