Maximum sum after k flips
You are given a non-empty integer array and a number k >= 1. You must perform exactly k operations; each operation picks one index and flips the sign of that element, and the same index may be picked any number of times. Return the largest possible array sum after the k operations. Example: nums [3, -1, 0, 2] with k = 3 gives 6 — flip -1 once, then burn the remaining two flips on the 0.
Implement
max_sum_after_flips(nums: list[int], k: int) → intExamples
in
[[3,-1,0,2],3]out6What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 14 min
solution.py
InputExpectedGot
[[3,-1,0,2],3]6not run yetsample