Question 1 in the array)

Source: Internet
Author: User

Arrays are common content for both the interview and the test, and the knowledge of arrays is often combined with interestingAlgorithm. Here, let's take a look at the online materials and relevant books we have read and summarize them as our interview review materials. I know that there must be some omissions or errors in this summary. I hope you can see the areas that can be optimized and corrected. Please help me with some new ideas. Thank you ~~

1. Calculate the maximum value (minimum value) in the array)

Problem description:Specify an integer array arr to find the maximum value (minimum value ).

Solution:The traditional method is to traverse an array to obtain the maximum value (minimum value), and the time complexity is O (n). Now another idea is to use binary, divide the array into two parts with nearly the same number of elements, find the maximum value (minimum value) of the two parts respectively, and combine them to compare the maximum value (minimum value) of the two parts ), the final maximum value (minimum value) is obtained ).

Code:

Findmax

 1   Int Findmax ( Int * Arr, Int L, Int  R)  2   {  3       If (L = R)  4  {  5           Return  Arr [l];  6   }  7       Int Mid = (L + r )/ 2  ;  8       Int  Lmax, rmax;  9 Lmax = Findmax (ARR, L, mid );  10 Rmax = findmax (ARR, Mid +1  , R );  11       Return  Max (Lmax, rmax );  12 }

2. Find more than half of the elements in the array

Problem description:There must be a number X in the integer array arr, and the number of times X appears in the array arr is more than half. Please find this number X.

Solution:Since X contains more than half of the number in the array, if the array arr is sorted, obviously, the ARR [n/2] in the array must be the X to be searched (the number of elements in the ARR of the n Array ). The time complexity of this idea depends on the time complexity of the sorting algorithm. Therefore, there is no problem if the time complexity of the entire algorithm reaches O (N * logn. Since x appears more than half the number of times in the array arr, in the process of traversing the array, if each encounter a different number than X, the number of X appears-1, x, then the number of occurrences of X is + 1, and the remaining value is X. In this case, we only need to traverse the array once. The time complexity is O (n). However, the difficulty is that we do not know what X is. How can we implement the algorithm? Look at the code ~~

Code:

Find

 1   Int Find ( Int * Arr, Int  N)  2   { 3       Int TMP = arr [ 0  ];  4       Int Count = 1 ; //  Counter  5       For ( Int I = 1 ; I <n; I ++ )  6   { 7           If (COUNT = 0  )  8   {  9 TMP = Arr [I];  10 Count = 1  ;  11   }  12           Else   If (TMP =Arr [I])  13   {  14 Count ++ ;  15   }  16           Else  17   {  18 Count -- ;  19   }  20  }  21       Return  TMP;  22 }

3. Find the nearest distance between elements in the array (1)

Problem description:There are n elements in the array arr [] (n elements in the array have no range). find such a and B to minimize the value of ABS (A, B.

Solution:This is a simple problem, and the idea is also easy to think about. You only need to sort the ARR. At this time, the shortest distance of the elements in the array can only be between adjacent two elements, the time complexity of this algorithm is:O (N * logn ). If the array is not sorted, we need to enumerate every two elements in the array and find the minimum distance. The time complexity is O (n * n ).

Code:

Mindistance

 1   Int Mindistance ( Int * Arr, Int  N)  2   {  3       Int Tmp_min = INF;  4       //  Assume that INF is the maximum value obtained by adding two numbers in arr. 5 Sort (ARR, arr + N );  6       For ( Int I = 0 ; I <n- 1 ; I ++ )  7   {  8           If (ARR [I + 1 ]-Arr [I] < Tmp_min)  9  {  10 Tmp_min = arr [I + 1 ]- Arr [I];  11   }  12   }  13       Return  Tmp_min;  14 }

4. Find the nearest distance between elements in the array (2)

Problem description:Similar to the previous problem, the difference is that the value range of array elements in arr.

Solution:Since a condition is added, it seems that this condition is used for optimization. Since the element in the array has a range, assume that the value of the array is 0 ~ 100, then we can do this and apply for an array bool fuzhu [100]. In this way, we traverse the array and record the elements that appear in arr. Assume that arr contains 4, then, arr [4] = 1, and then fuzhu is traversed. The value of fuzhu [I] is 1, indicating that I exists in arr, in this way, the element of "adjacent" fuzhu [] = 1 is calculated, and the minimum distance is calculated. A word that you don't want to see when reading a book: After analysis, the time complexity is O (n + M), where M is the Inter-region size of the elements in Aar.

Code:

Mindistance

 1   Int Mindistance ( Int * Arr, Int N)  2   {  3       Bool Fuzhu [ 101  ];  4       Int  First, second;  5       Int Tmp_min = INF;  6 Memset (fuzhu, False , Sizeof (Fuzhu ));  7       //  Traverse to find the number of ARR entries  8       For ( Int I = 0 ; I <n; I ++ )  9   {  10 Fuzhu [arr [I] = True  ;  11   } 12       Int J = 0  ;  13       While (J < 101 &&! Fuzhu [J])  14   {  15 J ++ ;  16   }  17 First = J ++; // Obtain the first number not 0 in fuzhu.  18       While (J < 101 &&! Fuzhu [J])  19   {  20 J ++ ;  21   }  22 Second = J ++; //  Obtain the second value not 0 in fuzhu.  23      While ( 1  )  24   {  25           If (Second-first < Tmp_min)  26   {  27 Tmp_min = second- First;  28   }  29           While (J < 101 &&! Fuzhu [J])  30   {  31 J ++ ;  32   }  33           If (J> 100 ) Break  ;  34 First = Second; 35 Second = J ++ ;  36   }  37       Return  Tmp_min;  38 }

Summary:After reading this, we will certainly say that the array range in arr may also be negative. What should we do? Note that this question requires the nearest distance. Although the subscript cannot be negative, it is enough to shift the storage! After all, we only need the minimum distance. parallel movement does not affect the minimum distance. For example, the range of elements in arr is-100 ~~ 100, then only when-100 appears, the corresponding fuzhu [-100 + 100] = fuzhu [0] = true. Is that all right?

5. Find the same element in the two Arrays

Problem description:Now you haveTwo sorted integer arrays, arr1 and arr2, locate the same elements in the two arrays.

Solution:Now that the order has been sorted, the problem is simple. Assume that arr1 [2] is equal to arr2 [4, to search for arr1 [3], you only need to compare it with subscripts 4 in arr2 and later. If the number of elements in arr1 and arr2 is N and M, the time complexity of this algorithm is O (n + M). Let's look at the code first.

Code:

 

Findsame

 1   //  Find the same element of the two Arrays  2   Void Findsame (Int * Arr1, Int * Arr2, Int N, Int  M)  3   {  4       Int I = 0  ;  5       Int J = 0  ;  6      While (I <n & J < M)  7   {  8           If (Arr1 [I] < Arr2 [J])  9 ++ I;  10           Else   If (Arr1 [I] = Arr2 [J])  11  {  12 Cout <arr1 [I] < Endl;  13 ++ I;  14 ++ J;  15   }  16           Else  //  Arr1 [I]> arr2 [J]  17 ++J;  18   }  19 }

Summary:For this problem, the arr1 and arr2 arrays are already sorted. What if the two arrays are not sorted ?? Later, I checked some information on the Internet and found a hash idea: traverse arr1, create a hash table, and traverse arr2 to see which element exists in the hash table. In this way, the time complexity is still O (N + M), but the space complexity of O (n) is introduced. Here:Please give the hash C ++ code !!!

Let's take a look at some of the lessons learned.

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.