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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array nums and an integer k.
An integer h is called valid if all values in the array that are strictly greater than h are identical.
For example, if nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9 are equal to 10, but 5 is not a valid integer.
You are allowed to perform the following operation on nums:
h that is valid for the current values in nums.i where nums[i] > h, set nums[i] to h.Return the minimum number of operations required to make every element in nums equal to k. If it is impossible to make all elements equal to k, return -1.
Example 1:
Input: nums = [5,2,5,4,5], k = 2
Output: 2
Explanation:
The operations can be performed in order using valid integers 4 and then 2.
Example 2:
Input: nums = [2,1,2], k = 2
Output: -1
Explanation:
It is impossible to make all the values equal to 2.
Example 3:
Input: nums = [9,7,5,3], k = 1
Output: 4
Explanation:
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
Constraints:
1 <= nums.length <= 100 1 <= nums[i] <= 1001 <= k <= 100Problem summary: You are given an integer array nums and an integer k. An integer h is called valid if all values in the array that are strictly greater than h are identical. For example, if nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9 are equal to 10, but 5 is not a valid integer. You are allowed to perform the following operation on nums: Select an integer h that is valid for the current values in nums. For each index i where nums[i] > h, set nums[i] to h. Return the minimum number of operations required to make every element in nums equal to k. If it is impossible to make all elements equal to k, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[5,2,5,4,5] 2
[2,1,2] 2
[9,7,5,3] 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3375: Minimum Operations to Make Array Values Equal to K
class Solution {
public int minOperations(int[] nums, int k) {
Set<Integer> s = new HashSet<>();
int mi = 1 << 30;
for (int x : nums) {
if (x < k) {
return -1;
}
mi = Math.min(mi, x);
s.add(x);
}
return s.size() - (mi == k ? 1 : 0);
}
}
// Accepted solution for LeetCode #3375: Minimum Operations to Make Array Values Equal to K
func minOperations(nums []int, k int) int {
mi := 1 << 30
s := map[int]bool{}
for _, x := range nums {
if x < k {
return -1
}
s[x] = true
mi = min(mi, x)
}
if mi == k {
return len(s) - 1
}
return len(s)
}
# Accepted solution for LeetCode #3375: Minimum Operations to Make Array Values Equal to K
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
s = set()
mi = inf
for x in nums:
if x < k:
return -1
mi = min(mi, x)
s.add(x)
return len(s) - int(k == mi)
// Accepted solution for LeetCode #3375: Minimum Operations to Make Array Values Equal to K
impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashSet;
let mut s = HashSet::new();
let mut mi = i32::MAX;
for &x in &nums {
if x < k {
return -1;
}
s.insert(x);
mi = mi.min(x);
}
(s.len() as i32) - if mi == k { 1 } else { 0 }
}
}
// Accepted solution for LeetCode #3375: Minimum Operations to Make Array Values Equal to K
function minOperations(nums: number[], k: number): number {
const s = new Set<number>([k]);
for (const x of nums) {
if (x < k) return -1;
s.add(x);
}
return s.size - 1;
}
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.