Minimum Total Operations - Problem
Given an array of integers nums, you can perform any number of operations on this array. In each operation, you can:
- Choose a prefix of the array
- Choose an integer
k(which can be negative) and addkto each element in the chosen prefix
A prefix of an array is a subarray that starts from the beginning of the array and extends to any point within it.
Return the minimum number of operations required to make all elements in the array equal.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,3,2]
›
Output:
2
💡 Note:
Target is 2 (last element). Element at index 1 (value 3) differs from 2, needs 1 operation. Element at index 0 (value 1) differs from 2, needs 1 operation. Total: 2 operations.
Example 2 — Already Equal
$
Input:
nums = [2,2,2]
›
Output:
0
💡 Note:
All elements are already equal to 2, so no operations needed.
Example 3 — Single Element
$
Input:
nums = [5]
›
Output:
0
💡 Note:
Single element array is already equal, requires 0 operations.
Constraints
- 1 ≤ nums.length ≤ 105
- -109 ≤ nums[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code