Find First/Last OccurrenceBinary Search Patterns

The Find First/Last Occurrence pattern applies when you need the Find First/Last Occurrence technique within the Binary Search Patterns pattern. Its time complexity is O(log n) and space complexity O(1). It is used in 2 problems on Thita, including Find First and Last Position of Element in Sorted Array and Find K Closest Elements. Common variations are On Sorted Array/List and Find Min/Max in Rotated Sorted Array.

Learn to find first and last occurrence of elements using binary search. Master lower bound and upper bound concepts.

Find First/Last Occurrence is one of the 5 subpatterns of the Binary Search Patterns pattern, which covers standard binary search, rotated arrays, first/last occurrence, and search on answer space. The whole pattern is about 2.5 hours of study. This subpattern is a core technique: expect it to come up directly in interviews.

What Find First/Last Occurrence covers

Master binary search algorithms including standard search, rotated array search, search on answer space, and finding boundaries. Essential for efficient O(log n) solutions. Problems in this subpattern are usually searched for as first occurrence, last occurrence, lower bound, upper bound, search range, leetcode 34.

How to practise Find First/Last Occurrence on Thita.ai

Read the theory for Find First/Last Occurrence, then work the problems attached to it in the browser editor. Your solution runs against the problem's test cases, and the AI coach offers a hint about the technique you are missing rather than a finished solution. Progress is tracked per subpattern, so the Binary Search Patterns tracker shows this one as covered once you have solved its problems.

Other subpatterns in Binary Search Patterns

Related DSA patterns

Where to go next

Find First/Last Occurrence is one lesson in a 16-pattern DSA course. If you are preparing end to end, work the interview-critical patterns first and use the pattern sheet as the checklist; if you are here for one technique, the Binary Search Patterns guide is the shortest path back to the rest of it.

Preparing this lesson and its course outline.