#C4213. Insert Position in Sorted Array

    ID: 47727 Type: Default 1000ms 256MiB

Insert Position in Sorted Array

Insert Position in Sorted Array

You are given a sorted array of integers and a target integer. Your task is to determine the index at which the target is found in the array. If the target does not exist in the array, then return the index where it should be inserted in order to maintain the sorted order.

Formally, given an array \( nums \) sorted in non-decreasing order and an integer \( target \), find the smallest index \( i \) such that \( nums[i] \geq target \). If no such index exists, then \( target \) would be placed at the end of the array.

Note: The array is guaranteed to be sorted in non-decreasing order. You are expected to implement an algorithm with a runtime complexity of at most \( O(\log n) \).

inputFormat

The input consists of two lines:

The first line contains a list of space-separated integers representing a sorted array (in non-decreasing order). The second line contains a single integer representing the target value.

outputFormat

Output a single integer representing the index at which the target is found, or where it should be inserted to maintain the sorted order.## sample

1 3 5 6
5
2