Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps:
nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.nums strictly smaller than largest. Let its value be nextLargest.nums[i] to nextLargest.Return the number of operations to make all elements in nums equal.
Example 1:
Input: nums = [5,1,3] Output: 3 Explanation: It takes 3 operations to make all elements in nums equal: 1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3]. 2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3]. 3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].
Example 2:
Input: nums = [1,1,1] Output: 0 Explanation: All elements in nums are already equal.
Example 3:
Input: nums = [1,1,2,2,3] Output: 4 Explanation: It takes 4 operations to make all elements in nums equal: 1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2]. 2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2]. 3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2]. 4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].
Constraints:
1 <= nums.length <= 5 * 1041 <= nums[i] <= 5 * 104Problem summary: Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps: Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i. Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest. Reduce nums[i] to nextLargest. Return the number of operations to make all elements in nums equal.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[5,1,3]
[1,1,1]
[1,1,2,2,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1887: Reduction Operations to Make the Array Elements Equal
class Solution {
public int reductionOperations(int[] nums) {
Arrays.sort(nums);
int ans = 0, cnt = 0;
for (int i = 1; i < nums.length; ++i) {
if (nums[i] != nums[i - 1]) {
++cnt;
}
ans += cnt;
}
return ans;
}
}
// Accepted solution for LeetCode #1887: Reduction Operations to Make the Array Elements Equal
func reductionOperations(nums []int) (ans int) {
sort.Ints(nums)
cnt := 0
for i, x := range nums[1:] {
if x != nums[i] {
cnt++
}
ans += cnt
}
return
}
# Accepted solution for LeetCode #1887: Reduction Operations to Make the Array Elements Equal
class Solution:
def reductionOperations(self, nums: List[int]) -> int:
nums.sort()
ans = cnt = 0
for a, b in pairwise(nums):
if a != b:
cnt += 1
ans += cnt
return ans
// Accepted solution for LeetCode #1887: Reduction Operations to Make the Array Elements Equal
/**
* [1887] Reduction Operations to Make the Array Elements Equal
*
* Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps:
* <ol>
* Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
* Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
* Reduce nums[i] to nextLargest.
* Return the number of operations to make all elements in nums equal.
*
* Example 1:
*
* Input: nums = [5,1,3]
* Output: 3
* Explanation: It takes 3 operations to make all elements in nums equal:
* 1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [<u>3</u>,1,3].
* 2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [<u>1</u>,1,3].
* 3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,<u>1</u>].
*
* Example 2:
*
* Input: nums = [1,1,1]
* Output: 0
* Explanation: All elements in nums are already equal.
*
* Example 3:
*
* Input: nums = [1,1,2,2,3]
* Output: 4
* Explanation: It takes 4 operations to make all elements in nums equal:
* 1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,<u>2</u>].
* 2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,<u>1</u>,2,2].
* 3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,<u>1</u>,2].
* 4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,<u>1</u>].
*
*
* Constraints:
*
* 1 <= nums.length <= 5 * 10^4
* 1 <= nums[i] <= 5 * 10^4
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/reduction-operations-to-make-the-array-elements-equal/
// discuss: https://leetcode.com/problems/reduction-operations-to-make-the-array-elements-equal/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn reduction_operations(nums: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1887_example_1() {
let nums = vec![5, 1, 3];
let result = 3;
assert_eq!(Solution::reduction_operations(nums), result);
}
#[test]
#[ignore]
fn test_1887_example_2() {
let nums = vec![1, 1, 1];
let result = 0;
assert_eq!(Solution::reduction_operations(nums), result);
}
#[test]
#[ignore]
fn test_1887_example_3() {
let nums = vec![1, 1, 2, 2, 3];
let result = 4;
assert_eq!(Solution::reduction_operations(nums), result);
}
}
// Accepted solution for LeetCode #1887: Reduction Operations to Make the Array Elements Equal
function reductionOperations(nums: number[]): number {
nums.sort((a, b) => a - b);
let [ans, cnt] = [0, 0];
for (let i = 1; i < nums.length; ++i) {
if (nums[i] !== nums[i - 1]) {
++cnt;
}
ans += cnt;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.