Leetcode 944 Solution

This article provides solution to leetcode question 944 (smallest-range-i)

https://leetcode.com/problems/smallest-range-i

Solution

class Solution {
public:
    int smallestRangeI(vector<int>& A, int K) {
        int min_val = 1000000;
        int max_val = 0;

        for (auto val: A)
        {
            min_val = min(val, min_val);
            max_val = max(val, max_val);
        }

        return max(max_val - min_val - 2 * K, 0);
    }
};