Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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

Question: You are given an infinite array A[·]in which the first n cells contain integers in sorted order and the rest of the cells are filled with . You are not given the value of n. Describe an algorithm that takes an integer x as input and finds a position in the array containing x, if such a position exists, in O(log n) time. (If you are disturbed by the fact that the array A has infinite length, assume instead that it is of length n, but that you don’t know this length, and that the implementation of the array data type in your programming language returns the error message whenever elements A[i]withi>n are accessed.)

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) .

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

Consider the task of searching a sorted array A[1,,n] for a given element x: a task we usually perform by binary search in time O(logn) . Show that any algorithm that accesses the array only via comparisons (that is, by asking questions of the form “is A[i]z 0?”), must take Ω(logn) steps.

An array A[1...n] is said to have a majority element if more than half of its entries are the same. Given an array, the task is to design an efficient algorithm to tell whether the array has a majority element, and, if so, to find that element. The elements of the array are not necessarily from some ordered domain like the integers, and so there can be no comparisons of the form “ is A[i]>A[j] ?”. (Think of the array elements as GIF files, say.) However you can answer questions of the form: “is ..?” in constant time.

(a) Show how to solve this problem in O(nlogn) time. (Hint: Split the array A into two arrays A1and A2of half the size. Does knowing the majority elements of A1and A2help you figure out the majority element of A? If so, you can use a divide-and-conquer approach.)

(b) Can you give a linear-time algorithm? (Hint: Here’s another divide-and-conquer approach:• Pair up the elements of A arbitrarily, to get n/2 pairs• Look at each pair: if the two elements are different, discard both of them; if they are the same, keep just one of them . Show that after this procedure there are at most n/2 elements left, and that they have a majority element if A does.)

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