Ctrl K

Sign Flips for the Largest Sum

Easy
You are given an integer array values and an integer flips. One operation picks any index i and replaces values[i] with -values[i]. You must perform exactly flips operations; the same index may be picked any number of times. Return the largest possible sum of the array after all the operations.
Example 1
values=[-6, 2, -1, 4], flips=2 → 13

Flip -6 and -1 to get [6, 2, 1, 4], which sums to 13.

Example 2
values=[3, 5, 1], flips=3 → 7

There are no negatives, and an odd number of flips must leave one number negative. The cheapest is 1: [3, 5, -1], sum 7.

Example 3
values=[-2, 0, 7], flips=4 → 9
Constraints
  • 1 ≤ len(values) ≤ 10⁴
  • -100 ≤ values[i] ≤ 100
  • 1 ≤ flips ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCake Slices for Guests EasyStrength of the Weaker Partner EasyTriangle From Sticks EasyPack the Backpack EasyThe Heavier Share of the Loot Easy
loading Python…
Input
values=[-6, 2, -1, 4], flips=2
Expected
13

Run checks the first 3 tests in your browser. Submit checks all 121 tests on the server, hidden ones included.