Data Structures & AlgorithmsVery hardLeetCode-style · Codeforces Div2 B

You must perform exactly 3 operations on the array [4, -2, 5, -7, 3, -1]; each operation replaces one element by its negation (the same element may be negated again later). What is the largest possible sum after all operations?

  1. A. 22
  2. B. 20
  3. C. 26
  4. D. 2

Correct answer: A. 22

Flip the most negative numbers first, turning them positive. If operations remain after all negatives are flipped, spend the parity on the smallest absolute value: flipping it twice cancels out, flipping it once costs the least. Flipping the largest positive element instead is a common error.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic