Skip to main content
Back to problems
Leetcode
Medium
Arrays
Matrices
Math
Greedy
Maximum Matrix Sum

Maximize the total sum of a matrix after repeatedly flipping the signs of entries subject to the problem’s operation rules.

Acceptance 100%
Problem Statement

You are given an m×nm \times n matrix of integers. By applying the allowed operation any number of times, you may change the signs of values in the matrix according to the problem’s rules. Your goal is to make the final sum of all matrix entries as large as possible.

Think about what information actually matters after all operations are considered: the magnitudes of the values, how many negatives there are, and whether a zero can neutralize an unavoidable sign issue.

Input Format

  • A matrix of integers with mm rows and nn columns.
  • The exact operation details are determined by the problem statement.
  • Values may be positive, negative, or zero.

Output Format

  • Return the maximum possible sum of the matrix after performing the allowed operations.

Constraints

  • Matrix dimensions are finite and reasonably sized for an interview problem.
  • Entries are integers that may be negative, zero, or positive.
  • A greedy or parity-based observation is typically sufficient.
Examples
Sample cases returned by the problem API.

Example 1

Input

matrix = [[1,-1],[-1,1]]

Output

4

Explanation

All values can be made positive in the optimal configuration, so the maximum sum is 1 + 1 + 1 + 1 = 4.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.