<p>You are given an integer array <code>nums</code>.</p>

<p>In one move, you can choose one element of <code>nums</code> and change it to <strong>any value</strong>.</p>

<p>Return <em>the minimum difference between the largest and smallest value of <code>nums</code> <strong>after performing at most three moves</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,3,2,4]
<strong>Output:</strong> 0
<strong>Explanation:</strong> We can make at most 3 moves.
In the first move, change 2 to 3. nums becomes [5,3,3,4].
In the second move, change 4 to 3. nums becomes [5,3,3,3].
In the third move, change 5 to 3. nums becomes [3,3,3,3].
After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,5,0,10,14]
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can make at most 3 moves.
In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
It can be shown that there is no way to make the difference 0 in 3 moves.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,100,20]
<strong>Output:</strong> 0
<strong>Explanation:</strong> We can make at most 3 moves.
In the first move, change 100 to 7. nums becomes [3,7,20].
In the second move, change 20 to 7. nums becomes [3,7,7].
In the third move, change 3 to 7. nums becomes [7,7,7].
After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int minDifference(vector<int>& nums) 
    {
        if(nums.size() <= 4) return 0;

        vector<int> greatest_nums{};
        vector<int> smallest_nums{};

        int greatest_num_index=1, smallest_num_index=1;

        for(int i = 0; i < 4; i++)
        {
            greatest_num_index = -1;
            smallest_num_index = -1;
            for(int j = 0; j < nums.size(); j++)
            {
                if(!count(greatest_nums.begin(), greatest_nums.end(), j) && (greatest_num_index == -1 || nums[greatest_num_index] < nums[j])) greatest_num_index = j;
                if(!count(smallest_nums.begin(), smallest_nums.end(), j) && (smallest_num_index == -1 || nums[smallest_num_index] > nums[j])) smallest_num_index = j;
            }

            if(greatest_num_index != -1) greatest_nums.push_back(greatest_num_index);
            if(smallest_num_index != -1) smallest_nums.push_back(smallest_num_index);
        }

        int result = nums[greatest_nums[0]] - nums[smallest_nums[3]];

        result = min(result, nums[greatest_nums[1]] - nums[smallest_nums[2]]); 
        result = min(result, nums[greatest_nums[2]] - nums[smallest_nums[1]]); 
        result = min(result, nums[greatest_nums[3]] - nums[smallest_nums[0]]); 
        return result;
    }
};
auto f = [] {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    return 0;
}();
38100