

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- First element and last element in a JavaScript array?
- Find position of an element in a sorted array of infinite numbers in C++
- Java Program to Add Element at First and Last Position of a Linked list
- Find the position of the last removed element from the array using C++
- Finding first unique element in sorted array in JavaScript
- Find missing element in a sorted array of consecutive numbers in Python
- Maximum difference between first and last indexes of an element in array in C
- Finding the first unique element in a sorted array in JavaScript
- Find the index of first 1 in an infinite sorted array of 0s and 1s in C++
- Find missing element in a sorted array of consecutive numbers in C++
- Delete first and last element from a LinkedList in Java
- Get first and last elements of a list in Python
- Find element position in given monotonic sequence in Python
- MongoDB query to find property of first element of array?
- Find the element that appears once in sorted array - JavaScript
Advertisements