# Min Amplitude

``````#include <iostream>  // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector>    // std::vector
#include <queue>    // std::priority_queue
#include <float.h>
#include <cstdlib>
using namespace std;

/**
* *
*
*  Given an Array A,
*  find the minimum amplitude you can get after changing up to 3 elements.
*  Amplitude is the range of the array
*  (basically difference between largest and smallest element).
*
* Input: [-1, 3, -1, 8, 5 4]
* Output: 2
* Explanation: we can change -1, -1, 8 to 3, 4 or 5
*
* Input: [10, 10, 3, 4, 10]
* Output: 0
* Explanation: change 3 and 4 to 10
* **/
int Amplitude(vector<int> nums) {
priority_queue<int, vector<int>, greater<int>> max_q;
priority_queue<int, vector<int>, less<int>> min_q;

for(auto n : nums) {
max_q.push(n);
min_q.push(n);
}

if(nums.size() < 3)
return 0;
int res = std::numeric_limits<int>::max();
int max_ary[4];
int min_ary[4];
for (int i = 0; i < 4; i++)
{
max_ary[i] = max_q.top();
min_ary[i] = min_q.top();
max_q.pop();
min_q.pop();
}
res = min(res, abs(min_ary[0] - max_ary[3]));
res = min(res, abs(min_ary[3] - max_ary[0]));
res = min(res, abs(min_ary[1] - max_ary[2]));
res = min(res, abs(min_ary[2] - max_ary[1]));

return res;
}
int main()
{
vector<int> test1 = {-1, 3, -1, 8, 5, 4};
vector<int> test2 = {10, 10, 3, 4, 10};
vector<int> test3 = {10, 3, 4, 10};

cout << Amplitude(test1) << endl;
cout << Amplitude(test2) << endl;
cout << Amplitude(test3) << endl;

return 0;
}``````