Java solution with explaination
I did not come up with this idea myself, the solution is amazing though it is not straight foward. My suggestions is do not dive into the problem directly, but 
I did not come up with this idea myself, the solution is amazing though it is not straight foward. My suggestions is do not dive into the problem directly, but think about some important numbers.\n\nThanks to this post I began understanding the solution:https://ryansblog2718281.medium.com/leetcode-1674-minimum-moves-to-make-array-complementary-afa6e9fef054. It also has a very good demostration.\n\nThe other Java solotion was qutie a neat one: https://leetcode.com/problems/minimum-moves-to-make-array-complementary/discuss/956852/Java-simple-solution\n\n![image](https://assets.leetcode.com/users/images/aaf37201-7119-49de-8aa5-418e5e2443df_1607552287.7972598.png)\n\n\n```\npublic int minMoves(int[] nums, int limit) {        \n        \n        int length = nums.length;\n        int[] segments = new int[limit*2+2]; // record the start and end of the segment, green and red dots\n        Map<Integer, Integer> sums = new HashMap<>(); // record the sum frequence, they do not need any move\n        \n        for( int i = 0; i< nums.length/2; i++) {\n            if(sums.keySet().contains(nums[i] + nums[length-1-i])){\n                sums.put(nums[i] + nums[length-1-i], sums.get(nums[i] + nums[length-1-i]) + 1);\n            } else {\n                sums.put(nums[i] + nums[length-1-i], 1);\n            }\n            int a = Math.min(nums[i], nums[length-1-i]);\n            int b = Math.max(nums[i], nums[length-1-i]);\n            segments[a+1]--;\n            segments[b+limit+1]++;\n        }\n        \n        if(sums.keySet().size() == 1) {\n            return 0;\n        }        \n       \n        int change = length;\n        int res = change;\n        \n        for(int i=2; i<segments.length; i++) {\n            change = change + segments[i] - sums.getOrDefault(i, 0) + sums.getOrDefault(i-1, 0);\n            res = Math.min(res, change);\n        }           \n        \n        return res;\n        \n    }    \n```
['Java']