Title Requirements:
An array is formed by moving several bits to the left of a descending sequence, such as {4,3,2,1,6,5} is formed by {6,5,4,3,2,1,} left two bits, in which a number is found.
Topic Analysis:
Method 1 finds each one, iterates through the array, and the time Complexity O (N).
Method 22 points to find, time complexity O (LOGN). Specific analysis See:
Code implementation:
#include <iostream>using namespacestd;intHelper (intA[],intKintSintt);intFindkinarray (intA[],intKintn);intMainvoid){ intA[] = {4,3,2,1,6,5}; //int Pos = Findkinarray (a,3,6);//int Pos = Findkinarray (a,6,6); intPos = Findkinarray (A,7,6); if(pos==-1) cout<<"not found"<<Endl; Elsecout<<"found, in a["<< Pos <<"] in"<<Endl; return 0;}//if found, returns the position in the array, returns 1 without finding it .intFindkinarray (intA[],intKintN) { returnHelper (A,k,0, N-1);}intHelper (intA[],intKintSintt) { if(s>t)return-1; intmid; Mid= S+ (T-s)/2; if(a[mid]==k)returnmid; if(a[s]>A[mid]) { if(K<=a[s] && k>A[mid])returnHelper (a,k,s,mid-1); Else returnHelper (a,k,mid+1, T); } Else { if(K<a[mid] && k>=A[t])returnHelper (a,k,mid+1, T); Else returnHelper (a,k,s,mid-1); }}
Find a number in the descending array left shift "Microsoft interview 100 question 48th"