let input = [ a1, a2, ... , aN ] // ordered ascending, N > 1
let k, P // assume k > 0, 1-based indexing
// remove at k, 0-based indexing
for (let i = k; i < N; ++i)
input[i - 1] = input[i]
// insert P preserving order
for (let i = N - 1; i >= 0; --i) {
if (i == 0 || P >= input[i - 1]) {
for (let j = N - 1; j > i; --j)
input[j] = input[j-1]
input[i] = P
break
} else continue
}
// finish
print(input)