Back to practiceSearch Insert Position
Given a sorted array of distinct integers and a target, return the index if found, or the index where it would be inserted to keep the array sorted.
Example
Input: nums = 1 3 5 6, target = 2
Output: 1
Approach
Standard binary search — when the loop ends without finding the target, low is exactly the correct insertion point.
Line 1: the array, space-separated
Line 2: target
Editor
nums = list(map(int, input().split()))
target = int(input())
low, high = 0, len(nums) - 1
Run your code to see output here.
Isolated sandbox · not executed on your devicePowered by Judge0 CE (free, self-hosted). Runs in an isolated sandbox — not on your device.
Editor
nums = list(map(int, input().split()))
target = int(input())
low, high = 0, len(nums) - 1
Run your code to see output here.
Isolated sandbox · not executed on your devicePowered 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
No discussions yet
Be the first to start a conversation.