Arrays · Easy
An export editor receives values already materialized in an array. The user identifies one entry by its one-based position from the end. The editor must return a new array without that entry and leave the original export untouched.
Return the values after removing exactly the nth entry from the end while preserving the order of the others.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "values": number[], "n": number }.
Fields
- values is a non-empty integer array. n is a one-based distance from the last entry: n=1 removes the last entry.
Output
Return value
- Return a new array of length values.length-1.
- Remove by index, not by value; duplicates at other indexes remain.
- Do not change the input array. A one-entry input returns [].
Constraints
- 1 <= values.length <= 100000; 1 <= n <= values.length.
- Values are integers in [-1000000000,1000000000].
Examples
Example 1
{
"n": 2,
"values": [
1,
2,
3,
4
]
}Returns
[
1,
2,
4
]Remove index 2.
Example 2
{
"n": 1,
"values": [
5,
5,
5
]
}Returns
[
5,
5
]Only the last occurrence is removed.
Required complexity
O(length) time to construct the output; O(1) working space excluding the returned array