[c++] explained, easy solution
the codes originally come from a previous post:\nhttps://leetcode.com/problems/minimum-moves-to-make-array-complementary/discuss/953321/C%2B%2B-or-easy-to-under
the codes originally come from a previous post:\nhttps://leetcode.com/problems/minimum-moves-to-make-array-complementary/discuss/953321/C%2B%2B-or-easy-to-understand\n\nBut it\'s unclear why it can solve the problem, I change the order of a few lines to show the main idea and add details for explaining:\n```\nclass Solution {\npublic:\n\tint minMoves(vector<int>& nums, int limit) {\n\t\tint n = nums.size();\n\t\tint statechangeArr[ 2*limit + 2 ];\n\t\tmemset(statechangeArr,0,sizeof(statechangeArr));\n\t\tint ans = INT_MAX;\n\t\tfor(int i = 0 ;i < n/2; i++){\n            \n            // for any i, at most 2 value adjusts can solve the problem;\n\t\t\tstatechangeArr[0]+=2;\n            \n\t\t\tint cur_sum = nums[i] + nums[n - i - 1];\n            \n            // assume we know the sum that referenced for adjusting the value within nums;\n            //(say x[i]<=x[j]), nums[i]+1 <= sum <  nums[i]+ nums[j] + 1; one less variable(x[j]) needs to adjust once; \n            statechangeArr[ min( nums[i] + 1, nums[n - i - 1] + 1 ) ] -= 1;\n            statechangeArr[ cur_sum + 1 ] += 1;\n            \n            //(say x[i]<=x[j]) nums[j]+limite+1 > sum >=  nums[i]+ nums[j]; one less variable(x[i]) needs to adjust; \n\t\t\tstatechangeArr[ cur_sum ] -= 1;\n\t\t\tstatechangeArr[ max( nums[i] + limit, nums[n - i - 1] + limit ) + 1 ] += 1;\n\t\t\t\n            // the case (x[j]>=x[j]) is similar and included in above cases.\n\t\t}\n\t\tint cur_num = 0;\n\t\tfor(int state: statechangeArr){\n\t\t\tcur_num += state;\n\t\t\tans = min(ans,cur_num);\n\t\t}\n\t\treturn ans;\n\t}\n};\n```
[]