Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make Array Values Equal to K

med
30 min
Try to solve the Minimum Operations to Make Array Values Equal to K problem.

Statement

You are given an integer array nums and an integer k.

An integer h is considered valid if all elements in nums that are strictly greater than h are equal to each other.

In one operation, you can:

  • Select a valid integer h.

  • Change every element in nums that is strictly greater than h to h.

Return the minimum number of operations required to make all elements in nums equal to k, or −1-1 if it is impossible to do so.

Constraints:

  • 1≤1 \leq nums.length ≤100\leq 100

  • 1≤1 \leq nums[i] ≤100\leq 100

  • 1≤1 \leq k ≤100\leq 100

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make Array Values Equal to K

med
30 min
Try to solve the Minimum Operations to Make Array Values Equal to K problem.

Statement

You are given an integer array nums and an integer k.

An integer h is considered valid if all elements in nums that are strictly greater than h are equal to each other.

In one operation, you can:

  • Select a valid integer h.

  • Change every element in nums that is strictly greater than h to h.

Return the minimum number of operations required to make all elements in nums equal to k, or −1-1 if it is impossible to do so.

Constraints:

  • 1≤1 \leq nums.length ≤100\leq 100

  • 1≤1 \leq nums[i] ≤100\leq 100

  • 1≤1 \leq k ≤100\leq 100