Skip to content

2033. Minimum Operations to Make a Uni-Value Grid #1481

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 in a grid equal by either adding or subtracting a given value x each time. If it's not possible to make all elements equal, we should return -1.

Key Points

  • Divisibility Check: All elements must have the same remainder when adjusted by x relative to a base element.
  • Median Optimization: The median minimizes the sum of absolute differences, making it the optimal target value.
  • Efficiency: Avoid inefficient operations like repeated array merging.

Approach

  1. Flatten the Grid: Convert the 2D grid into a 1D array efficiently.
  2. Check Divisibility: Verify if all elements can reach the same value using modulo checks.
  3. F…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Mar 26, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Mar 26, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
invalid This doesn't seem right medium Difficulty
2 participants