C++ | prefix array
\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int limit) \n    {\n        int res=INT_MAX,n=nums.size(),N=2*limit;\n        vector<int>count(
```\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int limit) \n    {\n        int res=INT_MAX,n=nums.size(),N=2*limit;\n        vector<int>count(N+1,0),premin(N+1,0),premax(N+1,0);\n        for(int i=0,j=n-1;i<j;i++,j--)\n        {\n            int sum=nums[i]+nums[j];\n            int mn=min(nums[i],nums[j]);\n            int mx=max(nums[i],nums[j]);\n            count[sum]++;\n            premin[mn]++;\n            premax[mx]++;\n        }\n        for(int i=1;i<=N;i++)\n        {\n            premin[i]+=premin[i-1];\n            premax[i]+=premax[i-1];\n        }\n        for(int sum=2;sum<=N;sum++) //checking for all possible sum\n        {\n            int x=count[sum];   //no change\n            int y1=premin[N]-premin[sum-1]; // 2 change for pairs having minimum >=sum\n            int y2=sum-limit>=1?premax[sum-limit-1]:0;// 2 change for pairs having maximum < sum-limit\n            int y=y1+y2; // 2 change\n            int z=(n/2)-x-y;  // 1 change for remaining pairs\n            res=min(res,2*y+z);\n        }\n        return res;\n    }\n};\n```
[]