5. Minimum Cycles
Given an array of n
integers, arr
, make the values equal using the minimum number of operations.
- Either choose an element and apply the operation:
- If the operation number is odd (e.g., first, third, fifth,..), then increase the element by 1.
- If the operation number is even (e.g., second, fourth, sixth,...), then increase the element by 2.
- or do nothing.
Only one element can change in an operation.
Calculate the minimum number of operations required to make all elements equal.
Example
arr = [1, 2, 4]
Operation Number | Operation type | Resulting arr |
---|---|---|
1 | Choose the 1st element and add 1 since operation number is odd. | [2, 2, 4] |
2 | Choose the 2nd element and add 2. | [2, 4, 4] |
3 | Skip this day | [2, 4, 4] |
4 | Choose the 1st element and add 1. | [4, 4, 4] |
The minimum operations required is 4.
Function Description
Complete the function minimumCycles
in the editor below.
minimumCycles
has the following parameter:
int arr[n]
: the integers to equalize
Returns
long
: the minimum number of operations required to equalize the array
Constraints
1 ≤ n ≤ 2*10^5
1 ≤ arr[i] ≤ 10^9
Input Format For Custom Testing
[Custom Testing Input Format]
Sample Case 0
[Sample Input 0]
STDIN | Function |
---|---|
5 -> | arr[] size n = 5 |
4 -> | arr = [4, 3, 5, 5] |
4 | |
3 | |
5 | |
5 |
Sample Output 0
3
Explanation
Operation Number | Operation type | Resulting arr |
---|---|---|
1 | Choose the 1st component, add 1 | [4, 4, 5, 5] |
2 | Choose the 3rd component, add 2 | [4, 4, 7, 5] |
3 | Choose the 2nd component, add 1 | [4, 5, 7, 5] |
目前观察第五题所有岗位所有人都是同一题。祝大家都能够 AC。
我们还提供VO辅助,面试代面,如果有需要请联系我。
If you need a complete question bank or assistance, please contact us.
有任何OA均可以联系我,100%AC,错一个不收钱。如果害怕自己解决不了OA,请扫码联系我 or telegram
If you're afraid that you can't solve the OA on your own, please scan the code to contact me or telegram