Maximize Sum Of Array After K Negations
EasyGreedygreedy
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given an integer array nums and an integer k. You must perform **exactly** k
operations. In each operation you pick any index i and replace nums[i] with -nums[i]. The same
index may be picked more than once.
Return the largest possible sum of nums after all k operations.
Examples
Input: nums = [4,2,3], k = 1
Output: 5
Negate `2` to get `[4,-2,3]`, whose sum is 5.
Input: nums = [3,-1,0,2], k = 3
Output: 6
Negate `-1` once, then negate `0` twice: `[3,1,0,2]` sums to 6.
Input: nums = [2,-3,-1,5,-4], k = 2
Output: 13
Negate `-3` and `-4` to get `[2,3,-1,5,4]`, sum 13.
Constraints
- 1 <= nums.length <= 10^4
- -100 <= nums[i] <= 100
- 1 <= k <= 10^4
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.