careercup-recursive and dynamic programming 9.3

Source: Internet
Author: User

9.3 in array a[0...n-1], there is a so-called magic Index that satisfies the condition a[i]=i. Given an array of ordered integers, the element values are not the same, and a method is written to find a magic index in array A, if one exists.

Advanced:

If the array element has duplicate values, what to do with it.?

Solution One, select Brute force method, we can directly iterate over the entire array to find the elements of the symbol condition.

 int  magicslow (int  Arr[],int   N) { if  (N==0  )  return -1  ;     int   I;  for  (I=0 ; I<n;i++ if  (Arr[i]==i)  Span style= "color: #0000ff;"    >return   I;  return -1  ;}

Solution Two: Since the given array is orderly, we should make full use of this condition.

You can see that this problem is very similar to the classic binary search problem. Using the matching method fully, we can find the appropriate algorithm, how should we use the binary search method?

In binary lookup, to find the element K, we will first compare it with the element x in the middle of the array to determine if K is on the left or right side of X.

Based on this, is it possible to determine the location of the Magic Index by examining the intermediate elements?

By comparing A[mid]<mid, you can determine that the Magic Index must be on the right side of mid, so left=mid+1;

If A[mid]>mid, can get the Magic index must be on the left of mid, so right=mid-1;

The algorithm is as follows:

intMagicquick (intArr[],intN) {    if(n==0)        return-1; intmid; intleft=0; intright=n-1;  while(left<=Right ) {Mid= (left+right)/2; if(mid==Arr[mid])returnmid; Else if(mid<Arr[mid]) right=mid-1; Else Left=mid+1; }    return-1;}

Advanced:

If there are duplicate elements, the previous algorithm is invalidated. Take the following array as an example:

-10-5 2 2 2 3 4 7 9 12 13

0 1 2 3 4 5 6 7 8 9 10 11

When we see A[MID]<MID, we cannot determine which side of the array The Magic Index is on. It may be on the right side of the array, as before. Alternatively, it may be on the left side (in this case, indeed on the left).

Is it possible to be anywhere on the left? No way. by A[5]=3, A[4] cannot be a magic index. A[4] must be equal to 4, its index can be a magic index, but the array is ordered, so a[4] must be less than a[5].

In fact, when we see a[5]=3, we need to recursively search the right half of the way, as we did before. However, as in the left half of the search, we can skip elements that value recursively search a[0] to a[3]. A[3] is the first element that could become a magic index.

All in all: we get a search pattern that compares whether Midindex and Midvalue are the same. Then, if the two are different, the left and right halves are recursively searched as follows.

Left half: The elements of the search index from start to min (Midindex-1,midvalue).

Right half: The search index elements from Max (Midindex+1,minvalue) to end.

You can look at the intersection of the left and right sides on the basis of the previous, for example A[mid]<mid, you need to search for some elements on the left side of mid, that is, the elements from start to min (Midindex-1,midvalue) and all the elements on the far side.

A[mid]>mid, you need to search all the elements on the left side of the mid and some elements on the right, that is, the interval from Max (midindex+1,minvalue) to end, and then the intersection of the two, the above range is obtained.

The algorithm is as follows:

intMagichelper (intArr[],intNintLintR) {    if(l>r| | l<0|| r>=N)return-1; intMid= (L+R)/2; if(mid==Arr[mid])returnmid; intRightindex=min (mid-1, Arr[mid]); intleft=Magichelper (Arr,n,l,rightindex); if(left>=0)        returnLeft ; intLeftindex=max (mid+1, Arr[mid]); intright=Magichelper (ARR,N,LEFTINDEX,R); returnRight ;}//there are repeating elementsintMagicfast (intArr[],intN) {    if(n==0)        return-1; returnMagichelper (Arr,n,0, N-1);}

Full code:

#include <iostream>using namespacestd;intMagicslow (intArr[],intN) {    if(n==0)        return-1; inti;  for(i=0; i<n;i++)    {        if(arr[i]==i)returni; }    return-1;}intMagicquick (intArr[],intN) {    if(n==0)        return-1; intmid; intleft=0; intright=n-1;  while(left<=Right ) {Mid= (left+right)/2; if(mid==Arr[mid])returnmid; Else if(mid<Arr[mid]) right=mid-1; Else Left=mid+1; }    return-1;}intMagichelper (intArr[],intNintLintR) {    if(l>r| | l<0|| r>=N)return-1; intMid= (L+R)/2; if(mid==Arr[mid])returnmid; intRightindex=min (mid-1, Arr[mid]); intleft=Magichelper (Arr,n,l,rightindex); if(left>=0)        returnLeft ; intLeftindex=max (mid+1, Arr[mid]); intright=Magichelper (ARR,N,LEFTINDEX,R); returnRight ;}//there are repeating elementsintMagicfast (intArr[],intN) {    if(n==0)        return-1; returnMagichelper (Arr,n,0, N-1);}intMain () {intarr[Ten]={-1,0,1,2,3,4,5,6,7,9}; cout<<magicslow (arr,Ten) <<Endl; cout<<magicquick (arr,Ten) <<Endl; cout<<magicfast (arr,Ten) <<Endl;}

careercup-recursive and dynamic programming 9.3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.