0ms C++ | sort and sum
Intuition & Approach
sort array
If no negative number, apply k operations on nums[0].
Loop and sum the numbers, applying k-- operations on negative numbers. Not
# Intuition & Approach

1. sort array
2. If no negative number, apply k operations on `nums[0]`.
3. Loop and sum the numbers, applying `k--` operations on negative numbers. Note the minimum number on the way.
4. If any k operation left apply it on the minimum and add to the sum (twice).

# Complexity

- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int largestSumAfterKNegations(vector<int> nums, int k) {
        sort(nums.begin(), nums.end());

        if (nums[0] >= 0 && k % 2) {
            nums[0] *= -1;
            k = 0;
        }

        int l = nums.size(), s = 0, m = 100;

        for (int i = 0; i < l; i++) {
            int e = nums[i];

            if (k) {
                if (e < 0)
                    e = abs(e), k--;

                // minimum is only needed if any operation k is left
                m = min(m, abs(e));
            }

            s += e;
        }

        if (k % 2)
            s -= 2 * m;

        return s;
    }
};
```
['C++']