Binary Search | 2 pointer | C++ | O(nlogn)
Intuition
Try to calculate minimum moves for every possible sum
Every possible sum means every sum of pairs occuring in the array
Number of moves at any given p
# Intuition
- Try to calculate minimum moves for every possible sum
- Every possible sum means every sum of pairs occuring in the array
- Number of moves at any given point will never be greater than nums.size() / 2

# Approach
- Segregate the arrays into min value of each pair & max value of each pair
- For every sum, if limit > sum, segregate the min & max arrays into values smaller than sum (1 replacement) & greater(2 replacements) than sum. Calculate min for both the segregated arrays
- If limit <= sum, segregate the min & max arrays into values smaller than sum - limit ( 2 replacements), smaller than sum (1 replacement) & greater(2 replacements) .
- Subtract no. of occurences of each sum for each calculation
- Calculate global minimum & return min(min_ans, n/2)

# Complexity
- Time complexity:
- O(nlogn)


# Code
```cpp []
class Solution {
public:
    int minMoves(vector<int>& nums, int limit) {
        int left = 0;
        int right = nums.size() - 1;
        std::vector<int> sum_hash(200001, 0);
        std::vector<int> sum_min,sum_max;
        while(left<=right) {
            sum_hash[nums[left] + nums[right]]++;
            sum_min.push_back(min(nums[left], nums[right]));
            sum_max.push_back(max(nums[left], nums[right]));
            left++;
            right--;
        }
        std::sort(sum_min.begin(), sum_min.end());
        std::sort(sum_max.begin(), sum_max.end());
        int ans = INT_MAX;
        for(int i=0;i<sum_hash.size();i++) {
            if(sum_hash[i] > 0) {
                long sum = i;
                int idx = std::upper_bound(sum_min.begin(), sum_min.end(), sum - 1) - sum_min.begin();
                int temp_moves;
                if(limit<=sum) {
                    int idx_1 = std::lower_bound(sum_min.begin(), sum_min.end(), sum - limit) - sum_min.begin();
                    temp_moves = idx_1 * 2 + (idx - idx_1) + (sum_min.size() - idx) * 2 - sum_hash[i];
                } else{
                    temp_moves = idx + (sum_min.size() - idx) * 2 - sum_hash[i];
                }
                
                ans = min(ans, temp_moves);
            }
        }
        for(int i=0;i<sum_hash.size();i++) {
            if(sum_hash[i] > 0) {
                long sum = i;
                int idx = std::upper_bound(sum_max.begin(), sum_max.end(), sum - 1) - sum_max.begin();
                int temp_moves;
                if(limit<=sum) {
                    int idx_1 = std::lower_bound(sum_max.begin(), sum_max.end(), sum - limit) - sum_max.begin();
                    temp_moves = idx_1 * 2 + (idx - idx_1) + (sum_max.size() - idx) * 2 - sum_hash[i];
                } else{
                    temp_moves = idx + (sum_max.size() - idx) * 2 - sum_hash[i];
                }
                
                ans = min(ans, temp_moves);
            }
        }
        return min(ans, (int)sum_min.size());
    }
};
```
['Hash Table', 'Binary Search', 'C++']