3375. Minimum Operations to Make Array Values Equal to K LeetCode Solution
In this guide, you will get 3375. Minimum Operations to Make Array Values Equal to K LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make Array Values Equal to K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Operations to Make Array Values Equal to K solution in C++
- Minimum Operations to Make Array Values Equal to K solution in Java
- Minimum Operations to Make Array Values Equal to K solution in Python
- Additional Resources

Problem Statement of Minimum Operations to Make Array Values Equal to K
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.
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] <= 100
1 <= k <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
3375. Minimum Operations to Make Array Values Equal to K LeetCode Solution in C++
class Solution {
public:
int minOperations(vector<int>& nums, int k) {
const unordered_set<int> numsSet(nums.begin(), nums.end());
const int mn = ranges::min(nums);
if (mn < k)
return -1;
if (mn > k)
return numsSet.size();
return numsSet.size() - 1;
}
};
/* code provided by PROGIEZ */
3375. Minimum Operations to Make Array Values Equal to K LeetCode Solution in Java
class Solution {
public int minOperations(int[] nums, int k) {
Set<Integer> numsSet = Arrays.stream(nums).boxed().collect(Collectors.toSet());
final int mn = Arrays.stream(nums).min().getAsInt();
if (mn < k)
return -1;
if (mn > k)
return numsSet.size();
return numsSet.size() - 1;
}
}
// code provided by PROGIEZ
3375. Minimum Operations to Make Array Values Equal to K LeetCode Solution in Python
class Solution:
def minOperations(self, nums: list[int], k: int) -> int:
numsSet = set(nums)
mn = min(nums)
if mn < k:
return -1
if mn > k:
return len(numsSet)
return len(numsSet) - 1
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.