Given a sorted array of integers nums, count the number of occurrences of a given value.
Example:
Input: nums = [1, 2, 2, 2, 4, 5]
, value = 2
Output: 3
Explanation: the value 2 appears 3 times in the array
Your algorithm should run in O(log n) time and use O(1) extra space.
public class NumberOfOccurrences {
// Function to find the first occurrence of the value
public static int findFirstOccurrence(int[] nums, int value) {
int left = 0, right = nums.length - 1;
int result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] >= value) {
if (nums[mid] == value) {
result = mid;
}
right = mid - 1;
} else {
left = mid + 1;
}
}
return result;
}
// Function to find the last occurrence of the value
public static int findLastOccurrence(int[] nums, int value) {
int left = 0, right = nums.length - 1;
int result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] <= value) {
if (nums[mid] == value) {
result = mid;
}
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
// Function to count the number of occurrences of the value
public static int countOccurrences(int[] nums, int value) {
int first = findFirstOccurrence(nums, value);
if (first == -1) {
return 0; // Value not found
}
int last = findLastOccurrence(nums, value);
return last - first + 1;
}
public static void main(String[] args) {
int[] nums = {1, 2, 2, 2, 4, 5};
int value = 2;
System.out.println("Number of occurrences of " + value + ": " + countOccurrences(nums, value));
}
}
## Complexity Analysis
- **Time Complexity**: O(log n) for both finding the first and last occurrences, resulting in an overall time complexity of O(log n).
- **Space Complexity**: O(1) as we are using a constant amount of extra space.
## Edge Cases
- **Value Not Present**: If the value is not present in the array, the function should return 0.
- **All Elements Same**: If all elements in the array are the same as the value, the function should return the length of the array.
- **Empty Array**: If the array is empty, the function should return 0.
## Testing
To test the solution comprehensively, consider the following test cases:
1. **Simple Case**: `nums = [1, 2, 2, 2, 4, 5]`, `value = 2` (Expected output: 3)
2. **Value Not Present**: `nums = [1, 3, 4, 5]`, `value = 2` (Expected output: 0)
3. **All Elements Same**: `nums = [2, 2, 2, 2]`, `value = 2` (Expected output: 4)
4. **Empty Array**: `nums = []`, `value = 2` (Expected output: 0)
## Conclusion
Understanding and solving this problem helps in mastering binary search and its applications in efficient searching algorithms. Practice similar problems to improve your problem-solving skills and deepen your understanding of binary search.
## Additional Resources
- [Binary Search Algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm)
- [LeetCode Binary Search Problems](https://leetcode.com/tag/binary-search/)
- [GeeksforGeeks Binary Search](https://www.geeksforgeeks.org/binary-search/)
By practicing and exploring further, you can enhance your algorithmic thinking and problem-solving abilities.