The sword refers to the offer face question (Java Edition): Number of occurrences in a sorted array __ Sword refers to offer (Java edition)

Source: Internet
Author: User

Topic: Statistics The number of times a number appears in a sorted array. For example, the input sort array is

{1,2,3,3,,3,3,4,5} and number 3, because 3 appears in this array 4 times, so output 4

Since the input array is sorted, we naturally think of using a binary lookup algorithm. In the example given in the topic, We can first use the binary lookup algorithm to find the first 3. Since 3 can occur several times, so we found about 3 of the two times is probably 3, so we find 3 of the left and right in the order of scanning, respectively, find the first 3 and the last 3. Because the number you are looking for may appear in an array of length n, O (n) times, So the time complexity of sequential scans is O (n). Therefore, the efficiency of this algorithm is the same as the method of scanning the entire array of statistics 3 occurrences directly from beginning to end. Obviously, the interviewer is not satisfied with this algorithm, it will prompt us to have a faster algorithm.

Next we think about how to better use the binary lookup algorithm. Let's say we count the number of times K has appeared in the sorted array. In the previous algorithm the time is mainly consumed in how to determine the recurrence of the first k and last K position, there is no available binary lookup algorithm directly to find the first K and the last K.

We first analyze how to use the binary lookup in the array to find the first k, the binary lookup algorithm is always first take the middle of the array of numbers and K comparison. If the middle number is larger than K, then K can only appear in the first half of the array, and the next round we are looking for the first half of the array. If the middle number is smaller than k, then K can only appear in the second half of the array, and in the next round we'll only look in the second half of the array. If the middle number and k are equal. Let's first judge if this number is the first K. If the number in front of the middle number is not k, then the middle number is just the first K. If the first number in the middle of the number is also K, that is to say that the initial k is definitely in the first half of the array, we still need to look in the first half of the array in the next round.

In the same way we use the above ideas to find the last K.

Once you find the first k and the last K, you know how many times K appears.

The implementation code is as follows:

/** * Statistics the number of times a number appears in a sorted array.

* For example, input sort array {1,2,3,3,3,3,4,5} and number 3, since 3 appears in this array 4 times, so the output 4 */package swordforoffer; /** * @author Jinshuangqi * * * * August 9, 2015/public class E38NUMBEROFK {private int getfirstk (int[] arr,int k,int le
		Ft,int right) {if (Left > right) return-1;
		int middleindex = (left+right)/2;
		int middledata = Arr[middleindex]; if (Middledata = = k) {if (Middleindex >0 && arr[middleindex-1]!=k) | | middleindex = 0) return Middleinde
			X
		else right = middleIndex-1;
		else if (Middledata > k) right = MiddleIndex-1;
		else left = Middleindex +1;
	Return Getfirstk (arr,k,left,right);
		private int Getlastk (int[] arr,int k,int left,int right) {if (Left > right) return-1;
		int Middleindex = (left + right)/2;
		int middledata = Arr[middleindex]; 
				if (Middledata = = k) {if (Middleindex <arr.length-1 && arr[middleindex+1]!=k) | | Middleindex ==arr.length-1)
			return middleindex; else left = Middleindex+1;
		else if (Middledata <k) {left = Middleindex +1;
		}else right = middleIndex-1;
	Return Getlastk (arr,k,left,right);
		public int GETNUMBEROFK (int[] arr,int k) {int number = 0;
			if (arr.length >0) {int-A-GETFIRSTK (arr,k,0,arr.length-1);
			int last = GETLASTK (arr,k,0,arr.length-1);
		if (>-1 && last >-1) number =last-first+1;
	return number;
		public static void Main (string[] args) {int[] arr= {1,2,3,3,3,3,4,5};
		E38NUMBEROFK test = new E38NUMBEROFK ();
	System.out.println (TEST.GETNUMBEROFK (arr, 3));
 }
}
In the above code, GETFIRSTK and GETLASTK are using the binary lookup method in the array to find a desirable number, their time complexity is O (logn), so GETNUMBEROFK total time complexity is only O (Logn).

Related Article

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.