Find the minimum number of swaps required such that all the 0s and all the 1s are together
问题 I need to find the minimum number of swaps required such that all the 0s and all the 1s are together. Here is my code: class GFG { static int minSwaps(int arr[], int n) { int noOfOnes = 0; for (int i = 1; i <= n; i++) { if (arr[i] == 1) noOfOnes++; } int x = noOfOnes; int maxOnes = Integer.MIN_VALUE; int preCompute[] = new int[n]; if (arr[0] == 1) preCompute[0] = 1; for (int i = 2; i < n; i++) { if (arr[i] == 1) { preCompute[i] = preCompute[i - 1] + 1; } else { preCompute[i] = preCompute[i -