Skip to content

3375. Minimum Operations to Make Array Values Equal to K #1538

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to determine the minimum number of operations required to make all elements of an array equal to a given integer k. Each operation involves selecting a valid integer h such that all elements greater than h are reduced to h. The solution must also check if it is impossible to achieve this goal and return -1 in such cases.

Approach

  1. Check for Impossibility: If any element in the array is less than k, it is impossible to make all elements equal to k because we cannot increase values. In this case, return -1.
  2. Collect Distinct Values: Identify all distinct values in the array that are strictly greater than k. Each distinct value represents a step where we can reduce the elements to the…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Apr 9, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Apr 9, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants