← Back to Hub

πŸ›οΈ Frequency Finder Temple

Count Occurrences Using Two Binary Searches

Target

-

First Index

-

Last Index

-

Count

-

Steps

0
πŸ“ The Sacred Formula
count = last - first + 1
First Occurrence
?
Last Occurrence
?
Total Count
?
Phase 1: Find First
Waiting...
Phase 2: Find Last
Waiting...
πŸ”₯ The Ancient Sorted Array πŸ”₯
? ━━━━ COUNT: ? ━━━━ ?
Generate an array to begin counting occurrences!

πŸ“– Count Element Algorithm

Problem: Count how many times a target appears in a sorted array.

Solution: Use TWO binary searches:

1. Find first occurrence (left-biased search)

2. Find last occurrence (right-biased search)

3. Calculate: count = last - first + 1

Time Complexity: O(log n) β€” Two O(log n) searches