3375. Minimum Operations to Make Array Values Equal to K
You are given an integer array
nums
and an integerk
.An integer
h
is called valid if all values in the array that are strictly greater thanh
are identical.For example, if
nums = [10, 8, 10, 8]
, a valid integer ish = 9
because allnums[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 innums
.- For each index
i
wherenums[i] > h
, setnums[i]
toh
.Return the minimum number of operations required to make every element in
nums
equal tok
. If it is impossible to make all elements equal tok
, return -1.
1 | class Solution { |