Find First And Last Position Of Element In Sorted Array
Ever played hide and seek with numbers? Well, today we're going to track down some sneaky ones hiding in a sorted array. Buckle up, 'cause we're on a mission to find the first and last positions of an element in a sorted array. It's like a treasure hunt, but with math!
Why the fuss about sorted arrays?
Imagine you're at a library, but instead of books, it's filled with numbers. Now, you want to find a specific number, say, 5. In a regular library (or unsorted array), you'd have to check every shelf (or index). But in our special library (sorted array), the librarian (algorithm) knows exactly where to start and stop looking.
Meet Binary Search
Our trusty sidekick in this adventure is Binary Search. It's like having a superpower - with just a few steps, it can find the needle in the haystack. Here's how it works:
Must Read
- Start in the middle of the array.
- If the target element is smaller, search the left half.
- If it's larger, search the right half.
- Repeat until you find the element or the search space is empty.
But here's the twist - we want to find both the first and last positions of the element. And that's where things get interesting!
Finding the first occurrence
Alright, so we've found our element using Binary Search. But what if there are more of them hiding in the array? To find the first occurrence, we need to keep moving left until we can't move anymore. It's like finding the first clue in a treasure map - once you've got it, you can follow the trail to the rest.
Here's the secret sauce:

- Find the element using Binary Search.
- If the element is found, move to the left side of the array.
- Repeat step 2 until the element is not found.
- The last found index is the first occurrence of the element.
Pretty neat, huh? Now let's find the last one!
Finding the last occurrence
Finding the last occurrence is like finding the final clue in our treasure hunt. Once you've got it, you know you've found all the hidden treasures. To find the last occurrence, we keep moving right after finding the element.
Here's how:

- Find the element using Binary Search.
- If the element is found, move to the right side of the array.
- Repeat step 2 until the element is not found.
- The last found index is the last occurrence of the element.
And there you have it! With these steps, you can find the first and last positions of any element in a sorted array. It's like being a secret agent, sneaking past all the other numbers to find your target.
Why should you care?
Finding the first and last positions of an element in a sorted array isn't just a fun party trick. It's a powerful tool in data structures and algorithms. It helps us search, sort, and manipulate data more efficiently. Plus, it's a great way to impress your friends at the next game night!
So, the next time you're faced with a sorted array, don't just stop at finding the element. Go on a treasure hunt and find the first and last positions. Who knows, you might even find some hidden gems along the way!
