Number of occurrences of the number in the sorted array

Source: Internet
Author: User

Title: Counts the number of occurrences of a number in a sorted array. For example input sort array {1,2,3,3,3,3,4,5} and number 3, because 3 appears in this array 4 times, so output 4.

The time complexity of the method of violence is O (n), there is a better solution, the use of binary search, the time complexity of O (LOGN):

1. First to find the first occurrence of the subscript value, set Left,mid,right, respectively, represents the beginning of the array, the middle, the end of the subscript.

If the number in the middle of the array A[mid] is greater than k, then right = mid-1;

If the number in the middle of the array A[mid] is less than k, then left = mid+1;

If the number of the middle of the array a[mid] is equal to K, the judge A[mid-1] is equal to K, if not equal to, the description is the first occurrence of the subscript, return mid subscript, if equal, then the first occurrence of the subscript is still on the left of mid, right = mid-1;

Recursion repeats the above process.

2. Find out the final subscript, the same principle.

Reference code:

 Packagetest;Importorg.junit.Test; Public classSolution { Public intGETNUMOFK (int[] A,intk) {if(A = =NULL|| A.length = = 0)            return0; intFirst = GETFIRSTK (A, 0, a.length-1, K); intLast = Getlastk (A, 0, a.length-1, K); if(First >-1 && last >-1)            returnLast-first + 1; return0; }    Private intGETFIRSTK (int[] A,intLeftintRightintk) {if(Left >Right )return-1; intMid = (left + right)/2; intMiddata =A[mid]; //the middle number equals K.        if(Middata = =k) {//The previous number in mid is not K, which indicates the number of first occurrences            if(Mid > 0 && a[mid-1]! = k | | mid = = 0)                returnmid; Else                //is not the first occurrence of the number, the first occurrence of the number again to the left of the arrayright = Mid-1; //The middle number is greater than k, indicating the left side of the array}Else if(Middata >k) right= Mid-1; Else            //The middle number is less than k, indicating the right side of the arrayleft = mid + 1; returnGetfirstk (A, left, right, K); }    Private intGETLASTK (int[] A,intLeftintRightintk) {if(Left >Right )return-1; intMid = (left + right)/2; intMiddata =A[mid]; if(Middata = =k) {if(Mid < A.length-1 && A[mid + 1]! = k | | mid = = A.LENGTH-1)                returnmid; Else Left= Mid + 1; } Else if(Middata >k) right= Mid-1; Else Left= Mid + 1; returnGetlastk (A, left, right, K); } @Test Public voidtestsolution () {int[] A = {1, 2, 3, 3, 3, 3, 4, 5 }; intK = 3; System.out.println (GETNUMOFK (A, k));//4    }}

Number of occurrences of the number in the sorted array

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.