Back to practiceFind First and Last Position of Element in Sorted Array Find First and Last Position of Element in Sorted Array
Given a sorted array that may contain duplicates and a target, return the first and last index of the target, space-separated. Print -1 -1 if the target isn't present.
Example
Input: nums = 5 7 7 8 8 10, target = 8
Output: 3 4
Approach
Two binary searches: one biased to find the leftmost occurrence (keep searching left even after a match), one biased to find the rightmost.
Line 1: the array, space-separated
Line 2: target
Editor
nums = list (map (int , input ().split()))
target = int (input ())
Run your code to see output here.
Isolated sandbox · not executed on your device Powered by Judge0 CE (free, self-hosted). Runs in an isolated sandbox — not on your device.
Code Output
Editor
nums = list (map (int , input ().split()))
target = int (input ())
Run your code to see output here.
Isolated sandbox · not executed on your device Powered by Judge0 CE (free, self-hosted). Runs in an isolated sandbox — not on your device.
Sign in to track solved problems and earn XP.
Community Discussions Start a discussionNo discussions yet
Be the first to start a conversation.