An efficient array of interview topics

Source: Internet
Author: User
Tags arrays bool int size mixed
1. How to use recursion to implement an array summation. int getsum (int *a,int n) {return n==0?0:getsum (a,n-1) +a[n-1];//is mainly written with conditional expressions to be relatively concise} 2. How to print a two-dimensional array with a for loop. for (int i=0;i<m*n;i++) printf ("%d", array[i/n][i%n]); 3. The average number of nodes to move in the sequential table to insert and delete a node. Insertion: Average N/2 Delete: average (n-1)/2 4. How to use recursive algorithm to determine whether an array is incremented. BOOL Isincrease (int a[],int n) {if (n==1) return true; Return (A[n-1]>=a[n-2]) && isincrease (a,n-1); }
5. How to use recursive and non-recursive methods to achieve two-point search.
int binarysearch (int array[],int start,int end,int finddata) {    if (start>end)       &NBSP ;  return-1;     int mid= (start+end)/2;     if (array[mid]==finddata)     {        return mid,    }   &nbs P else if (array[mid]>finddata)     {         Return binarysearch (Array,start, Mid-1,finddata);         else         return BinarySearch (Array,mid+1,end,finddata); }//Iteration int binarysearch (int array[],int len,int finddata) {    if (Array==null | | len<0)     &NBSP ;   return-1;      int start=0;end=len-1;     while (start<=end)     {        int mid= (start+end)/2;//mid in the loop inside   &NB Sp     if ()             ...         ELSE if           &NBSp end=mid-1;//main change end,start subscript         else             start=mid+1;    }}
6. How to find the number of occurrences of a given number in a sorted array
For example [1,2,2,2,2,3] 2 appears 3 times.
Two-time binary lookups can be done at O (2*logn) time complexity to find the leftmost value Lower, the second find finds the rightmost value Upper, and then count=upper-lower//Iterate int binarysearch (int array [],int len,int FindData, bool isleft) {    if (Array==null | | len<0)         RETURN-1;&N Bsp     int start=0;end=len-1;     while (start<=end)     {        int mid= (start+end)/2;//mid in the loop, middle=l eft +  (right-left) >>1);  //Prevent overflow, shift is also more efficient. At the same time, each cycle needs to be updated.            if (Array[mid=finddata])          {                Last=mid;                 if (isleft)//need to find lower             &NB Sp           end=mid-1;                 Other                 &NBSP ;       start=mid+1;     &NBSP;               ELSE if             end=mid-1;         else             start=mid+1;           return last>0?last:-1; }
7. How to calculate the intersection of two ordered arrays. a=0,1,2,3,4 b=1,3,5,7,9
int mixed (int array1[],int n1,int array2[],int n2,int *mixed) {int i=0,j=0,k=0;         while (i<n1 && j<n2);             if (Array1[i]==array2[j]) {mixed[k++]=array1[i];             i++;          j + +;         } else if (Array1[i]>array2[j]) {j + +;         } else if (Array1[i]<array2[j]) {i++; } return k; }
8. How to find the most repeated number in the array.
C:int Count[max] To record the number of occurrences of each element, where Max is the maximum value for the array C + +: Using the map table #include <map> using namespace std;         BOOL Findmostfrequentarray (int *a,int size,int &val) {if (size==0) return false;         map< int,int> m;         for (int i=0;i<size;i++) {if (+ m[a[i]]>= m[val]) val=a[i]; } return true; } 9. How to find the number of occurrences of more than half of the occurrences in the array in the time complexity of O (n). 2,1,1,2,3,1,1,3 method One, reduce the size: if each deletion of two different put the number (regardless of including the highest frequency words), in the remaining numbers, the highest frequency word is more than 50%, the last remaining must be high-frequency words
int Find (int array[],int len) {       int candidate=0;        int count=0; &nbsp ;      for (int i=0;i<len;i++)         {            if (c ount==0)             {           /        Candidate=array[i];                     count=1;            }             else         &NB Sp   {                    if (Candidate==num[i])                            count++;                     Other             &NBSP ;              count--;     &NBSP      }        }   return candidate;  }
10. How to find the unique repeating element in the array. Condition restriction: Array A[n], number range in [1,n-1] method: Sum of 1 to n in all arrays
Method 2: The number of distinct or duplicate is a, the remaining N-2 number is different or the result is B, then the n number of different or result is a^a^b. 1 to n-1 the number of different or result for a^b then (a^a^b) ^ (a^b) =a
Method Three: Bitmap method voidSetbit ( intN) {map[n>>5]=map[n>>5] | (1 << (n&31));//n>>5 is 32 for the whole, n&31 is to seek 32 more} intTestbit ( intN) { returnMAP[N&GT;&GT;5] & (1<< (n&31));//determine if the corresponding bit is 1} intFind intArray[], intLen) { inti,k=0; for(i=0;i<len;i++) { if(Testbit (Array[i])) return flase;      else Setbit (Array[i]);   } return ERROR; }

11. How to determine whether the values in an array are contiguous. 1) 5 number run disorder, such as 8 7 5 0 6 2) 0 can match more than one arbitrary number, 0 can appear multiple times.

12. How to find the elements in the array that appear odd times. Only one element odd number of times, find out to different or extension: n-2 elements appear even several times, two appear odd several times, how to Let in O (1) space complexity, find out these two numbers. 1) Set 2 number for a, B, x=a^b 2) How to ask a? The K-bit in X is 1, then the K-bit of a A/b is necessarily different, all the numbers of K-bits 1 are all XOR, i.e. a. So for (...) if ((a[i]>>k) &1) s=s^a[i]; S is a. 3) How to ask for B? X^a= (a^b) ^a=b
void Find (int a[],int length) {

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.