Find First and Last Position of an Element in a Sorted ArrayIn this tutorial, we will solve the Python problem in which we need to find the first and last position of an element in a sorted array. The problem statement is that we have given an array of integers sorted in non-decreasing order, and find the starting and ending position of a given target value. Example - Example 2: Example 3: Let's understand the following solutions. Solution - 1: A naïve ApproachIn this approach, we iterate on the elements of a given array and check elements in an array and keep track of the first and last occurrence of the found element's index. Let's understand the following example. Example - Output: [-1, -1] Explanation - In the above code, we define the find_first_and_last_occurrence() function that takes an array (arr) and the target element (target) as input. We initialize two variables, first_occurrence, and last_occurrence, to store the indices of the first and last occurrences of the target element in the array. We then iterate through the array using a for loop and check each element against the target. If we find a match, we update the first_occurrence index if it's the first time we encounter the target element. We keep updating the last_occurrence index to the latest occurrence of the target element as we traverse the array. Finally, we return the first_occurrence and last_occurrence indices. The time complexity will be O(n) and auxiliary space will be O(1). Solution - 2: Using Binary SearchLet's solve this problem using the binary search algorithm. Example - In the above code, we first define two binary search functions, find_first_occurrence and find_last_occurrence, to find the indices of the first and last occurrences of the target element in the sorted array, respectively. Then, the find_first_and_last_occurrence function uses these two binary search functions to get the first and last occurrence indices of the target element in the given array. The test code at the end demonstrates how to use the function with an example sorted array and target element, printing the first and last occurrence indices if the element is found or a message if the element is not present in the array. Solution - 3:To get the first occurrence, the first binary search looks for any occurrence of the target number in the array. When a match is found, we record the index and continue searching in the left sub-array, i.e., the elements on the left side of the current index. The same process will follow in right sub-array to find last occurrence of the number. Let's understand the following example. Example - Output: [3, 4] Solution - 3: Using Inbuilt FunctionsLet's understand the following example. Example - Output: [3, 4] Explanation - In this solution, the index() method is used to find the index of the first occurrence of the target element in the list. To find the last occurrence, we use index() again, but this time on a reversed version of the list (arr[::-1]) and then calculate the last occurrence's index relative to the original list. Next TopicFinger Search Tree Data Structure |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India