← 返回 amazon 的题目列表Maximum Number of Indices with arr[i] < arr[i+1]
类型:online_judge
Given an integer array, rearrange the array to maximize the number of indices such that arr[i] < arr[i+1]. Output the maximum number of such indices.
Input
An integer array arr of length n.
Output
An integer representing the maximum number of indices where the condition is satisfied.
Example
Example 1:
Input: arr = [2, 1, 3] Output: 2 Explanation: The array can be rearranged as [1, 2, 3], where indices 0 and 1 satisfy arr[i] < arr[i+1].
Example 2:
Input: arr = [5, 1, 3, 4, 2] Output: 3 Explanation: The array can be rearranged as [1, 2, 3, 4, 5], where indices 0, 1, and 2 satisfy arr[i] < arr[i+1].
Constraints
1 <= n <= 100,000
-10^9 <= arr[i] <= 10^9
Example
Input
2,1,3