
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find First and Last Position of Element in Sorted Array in Python
Suppose we have an array of integers A. This is sorted in ascending order, we have to find the starting and ending position of a given target value. When the target is not found in the array, return [-1, -1]. So if the array is like [2,2,2,3,4,4,4,4,5,5,6], and target is 4, then the output will be [4,7]
To solve this, we will follow these steps −
- Initially res := [-1,-1], set low := 0, high := length of array A
- while low < high
- mid := low + (high – low)/2
- if A[mid] is target, then
- high := mid, res[0] := mid and res[1] := mid
- otherwise when A[mid] < target, then low := mid + 1, else high := mid
- if res[0] = -1, then return res
- low := res[0] + 1, high := length of nums
- while low < high
- mid := low + (high – low)/2
- if A[mid] is target, then
- low := mid + 1, res[1] := mid
- otherwise when A[mid] < target, then low := mid + 1, else high := mid
- return res
Example(Python)
Let us see the following implementation to get better understanding −
class Solution(object): def searchRange(self, nums, target): res = [-1,-1] low = 0 high = len(nums) while low<high: mid = int(low + (high-low)//2) if nums[mid] == target: high = mid res[0]=mid res[1]=mid elif nums[mid]<target: low = mid+1 else: high = mid if res[0] == -1: return res low = res[0]+1 high = len(nums) while low<high: mid = int(low + (high-low)//2) if nums[mid] == target: low = mid+1 res[1] = mid elif nums[mid] < target: low = mid + 1 else: high = mid return res ob1 = Solution() print(ob1.searchRange([2,2,2,3,3,4,4,4,4,5,5,6], 4))
Input
[2,2,2,3,4,4,4,4,5,5,6] 4
Output
[5, 8]
- Related Articles
- First element and last element in a JavaScript array?
- Python Program to add element to first and last position of linked list
- Find position of an element in a sorted array of infinite numbers in C++
- Finding first unique element in sorted array in JavaScript
- Find missing element in a sorted array of consecutive numbers in Python
- Find the position of the last removed element from the array using C++
- Maximum difference between first and last indexes of an element in array in C
- Java Program to Add Element at First and Last Position of a Linked list
- Finding the first unique element in a sorted array in JavaScript
- Find missing element in a sorted array of consecutive numbers in C++
- Find the index of first 1 in an infinite sorted array of 0s and 1s in C++
- Find the element that appears once in sorted array - JavaScript
- Find index of an extra element present in one sorted array in C++
- Missing Element in Sorted Array in C++
- How to find last occurence of element in the array in TypeScript?

Advertisements