Skip to content

Practice Minimum Operations to Make Array Values Equal to K

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#3375
LeetCode Problem

Minimum Operations to Make Array Values Equal to K

Problem Statement

An integer h is valid for the current nums when all values strictly greater than h are identical. In one operation, choose such an h and replace every value greater than h with h, leaving all other values unchanged. Return the fewest operations needed to make every element equal to k, or -1 if impossible.

Source: Minimum Operations to Make Array Values Equal to K on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [5,2,5,4,5], k = 2
Output
2

# Example 2

Input
nums = [2,1,2], k = 2
Output
-1

# Example 3

Input
nums = [9,7,5,3], k = 1
Output
4

Constraints

1 <= nums.length <= 100
1 <= nums[i] <= 100
1 <= k <= 100

Related LeetCode Problems

Practice more problems similar to Minimum Operations to Make Array Values Equal to K.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.