Simple Java code
\n\n# Code\n\nclass Solution {\n    ArrayList<Long> psa = new ArrayList<>();\n    ArrayList<Integer> orig = new ArrayList<>();\n    long dist(int l, int r, long
\n\n# Code\n```\nclass Solution {\n    ArrayList<Long> psa = new ArrayList<>();\n    ArrayList<Integer> orig = new ArrayList<>();\n    long dist(int l, int r, long mid) {\n        long res = 0;\n        if (r >= 0 && r < psa.size()) res += psa.get(r);\n        if (l > 0 && l - 1 < psa.size()) res -= psa.get(l - 1);\n        return Math.abs(mid * (r - l + 1) - res);\n    }\n    public long minimumMoves(int[] nums, int k, int maxChanges) {\n        for (int i = 0; i < nums.length; i++) {\n            if (nums[i] != 0) {\n                psa.add(i + (psa.size() != 0? psa.get(psa.size() - 1): 0));\n                orig.add(i);\n            }\n        }\n        if (psa.size() == 0) return k * 2;\n\n        long min = Long.MAX_VALUE;\n        int slen = Math.max(k - maxChanges, 1);\n        for (int l = slen; l <= Math.min(k, Math.max(slen, 3)); l++) {\n            for (int i = 0; i <= psa.size() - l; i++) {\n                int midx = i + l / 2;\n                long median = orig.get(midx);\n                min = Math.min(min, dist(midx + 1, i + l - 1, median) + dist(i, midx - 1, median) + (k - l) * 2);\n            }\n        }\n    \n        return min;\n    }\n}\n```
['Java']