Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Sliding Window

Minimum Swaps to Group All 1s

Medium · Sliding Window · Asked at Amazon, Microsoft, Adobe

An array contains only 0s and 1s. In one swap you may exchange any two elements. Print the minimum number of swaps needed to bring all the 1s together into one contiguous block.

Input

First line: n. Second line: n space-separated integers.

Output

The minimum number of swaps.

Constraints

If there are ones 1s, the final block is some window of length ones. Each 0 inside that window needs exactly one swap, so slide a window of that length and minimise the number of 0s in it.

Example 1

Input

5
1 0 1 0 1

Output

1

Example 2

Input

6
1 0 1 0 1 1

Output

1

Hints

  1. If there are k ones, they will end up in some window of length k.
  2. Each 0 inside that window must be swapped with a 1 from outside — one swap each.
  3. So find the window of length k with the most 1s.

Approach

Count the ones, k. If k is 0 nothing needs to move. Otherwise slide a window of length k across the array, keeping the number of 1s in it (add the entering element, subtract the leaving one). The answer is k − (the most 1s any window holds), i.e. the fewest 0s that need swapping out.

Complexity: O(n) time · O(1) extra space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    int k = 0;
    for (auto &x : a) {
        cin >> x;
        k += x;
    }
    if (k == 0) {
        cout << 0 << "\n";
        return 0;
    }
    int cur = 0;
    for (int i = 0; i < k; i++) cur += a[i];
    int best = cur;
    for (int i = k; i < n; i++) {
        cur += a[i] - a[i - k];
        best = max(best, cur);
    }
    cout << k - best << "\n";
    return 0;
}

Python solution

import sys

data = sys.stdin.buffer.read().split()
n = int(data[0])
a = list(map(int, data[1 : 1 + n]))
k = sum(a)
if k == 0:
    print(0)
else:
    cur = sum(a[:k])
    best = cur
    for i in range(k, n):
        cur += a[i] - a[i - k]
        if cur > best:
            best = cur
    print(k - best)

Java solution

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        int[] a = new int[n];
        int k = 0;
        for (int i = 0; i < n; i++) {
            in.nextToken();
            a[i] = (int) in.nval;
            k += a[i];
        }
        if (k == 0) {
            System.out.println(0);
            return;
        }
        int cur = 0;
        for (int i = 0; i < k; i++) cur += a[i];
        int best = cur;
        for (int i = k; i < n; i++) {
            cur += a[i] - a[i - k];
            best = Math.max(best, cur);
        }
        System.out.println(k - best);
    }
}

Solve Minimum Swaps to Group All 1s online · All practice problems