Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make a Uni-Value Grid

Minimum Operations to Make a Uni-Value Grid lets you add or subtract x from grid values to make them all equal. Return the fewest operations, or negative one if impossible.

Statement

You are given a 22D integer grid grid of size m ×\times n, and an integer x.

In one operation, you can choose any element in grid and either add x to it or subtract x from it.

A grid is considered uni value if all of its elements are equal.

Return the minimum number of operations needed to make grid uni value. If it is impossible to make all elements equal, return −1-1.

Constraints:

  • m ==== grid.length

  • n ==== grid[i].length

  • 1≤1 \leq m, n ≤105\leq 10^5

  • 1≤1 \leq m ×\times n ≤105\leq 10^5

  • 1≤1 \leq x, grid[i][j] ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make a Uni-Value Grid

Minimum Operations to Make a Uni-Value Grid lets you add or subtract x from grid values to make them all equal. Return the fewest operations, or negative one if impossible.

Statement

You are given a 22D integer grid grid of size m ×\times n, and an integer x.

In one operation, you can choose any element in grid and either add x to it or subtract x from it.

A grid is considered uni value if all of its elements are equal.

Return the minimum number of operations needed to make grid uni value. If it is impossible to make all elements equal, return −1-1.

Constraints:

  • m ==== grid.length

  • n ==== grid[i].length

  • 1≤1 \leq m, n ≤105\leq 10^5

  • 1≤1 \leq m ×\times n ≤105\leq 10^5

  • 1≤1 \leq x, grid[i][j] ≤104\leq 10^4