Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn)
Examples:
Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 times in arr[] Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 3 Output: 1 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 1 Output: 2 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 4 Output: -1 // 4 doesn't occur in arr[]
Method 1 (Linear Search)
Linearly search for x, count the occurrences of x and return the count.
Time Complexity: O(n)
Method 2 (Use Binary Search)
1) Use Binary search to get index of the first occurrence of x in arr[]. Let the index of the first occurrence be i.
2) Use Binary search to get index of the last occurrence of x in arr[]. Let the index of the last occurrence be j.
3) Return (j – i + 1);
public int countNumbers(int[] a, int t) { if(a==null || a.length==0) return 0; int left = getLeftIndex(a, t); int right = getRightIndex(a, t); if(left == -1 || right == -1) return 0; return right-left+1; } private int getLeftIndex(int[] a, int t) { int start = 0, end = a.length; while(start <= end) { int mid = (start + end) / 2; if((mid == 0 || a[mid-1] < t) && a[mid] == t) { return mid; } else if(a[mid] < t) { start = mid + 1; } else { end = mid - 1; } } return -1; } private int getRightIndex(int[] a, int t) { int start = 0, end = a.length; while(start <= end) { int mid = (start + end) / 2; if((mid == a.length-1 || a[mid+1] > t) && a[mid] == t) { return mid; } else if(a[mid] > t) { end = mid - 1; } else { start = mid + 1; } } return -1; }
Method 3:
下面这种递归的方法更好理解。
private int count(int[] A, int s, int e, int x) { if(s > e) return 0; int mid = (s + e)/2; if(A[mid] == x) { return 1 + count(A, s, mid - 1, x) + count(A, mid + 1, e, x); } else if(A[mid] > x) { return count(A, s, mid - 1, x); } else { return count(A, mid + 1, e, x); } } public int countNumber(int[] A, int x) { return count(A, 0, A.length-1, x); }
Reference:
http://www.geeksforgeeks.org/count-number-of-occurrences-in-a-sorted-array/
相关推荐
Write a class that displays the number of occurrences of each letter in a string using histogram. The class header should be: public class Histogram extends JPanel { … } Write a test program ...
Write a class that displays the number of occurrences of each letter in a string using histogram. The class header should be: public class Histogram extends JPanel { … } Write a test program that ...
sub Substitute occurrences of a pattern found in a string. subn Same as sub, but also return the number of substitutions made. split Split a string by the occurrences of a pattern. findall Find ...
However, if we are searching for multiple rows, such as duplicate values, or keys in a range, anything more than a small number of rows will make the nonclustered index search very inefficient. ...
and 2) when a user specifies a keyword or phrase, the application should search the displayed description for occurrences of that keyword or phrase and display a count of the number of occurrences ...
having some data file consisting of three different symbols, and their total number of occurrences in that file is s1(1000), s2(200), s3(30), so the total length of the file is 1000+200+30=1230 bytes...
Highlight all occurrences of selected word插件,与选中的代码相同的会自动高亮,方便查找。最新版支持VisualStudio2017,实际验证,完全可以使用。
The book concludes with Part V: directed networks with plenty of examples, including a network of qualitative adjectives that you could use in computer games or fiction. When you finish your journey, ...
All of the strings in this range will share a growing prefix. Each time the prefix grows, we can output a character. y +--------------------------+ Uncomp- | V ressed | +---------+ p +----------+...
This extension will highlight all occurrences of a selected word in the current document and display a glyph on the left margin. This allows for fast visualization of a specific word used throughout ...
Replacing occurrences of a UTF-8 string in a UTF-8 string 143 Accessing characters in a UTF-8 string by position 145 This material is copyright and is licensed for the sole use by Vadim Kudria on 4...
In this paper we present a new scheme for detecting copies based on compar¬ing the word frequency occurrences of the new docu¬ment against those of registered documents. We also report on an ...
It first generates a random SID for the computer, and proceeds to update instances of the existing computer SID it finds in the Registry and in file security descriptors, replacing occurrences with ...
In this paper we present a new scheme for detecting copies based on compar¬ing the word frequency occurrences of the new docu¬ment against those of registered documents. We also report on an ...
计算发生次数 开始之前请阅读 执行描述的任务
The editor now highlights all table.column occurrences in a select statement when clicking on the table name in the statement: Editor now highlights all field occurrences in a select statement when ...
The lists lines option can be a handy tool when searching because it presents all occurrences of the find string in a floating dialog box. You can use the dialog to navigate to each instance by double...
问题:AVL树目的:了解平衡二叉搜索树的端到端知识,以及如何将其有效地用于解决各种问题。 任务:通过以下操作实现AVL树。...9. Count the number of elements in the tree whose values fall into a given range.
n is the number of symbols in the low dimensional approximation of the sub sequence. alphabet_size is the number of discrete symbols. 2 , although alphabet_size = 2 is a special "useless" case. ...