Binary Deque (Codeforces 1692E)
Tags: Implement, two pointers

Slavic has an array of length $$$n$$$ consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array. What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to $$$s$$$ after performing all the operations? In case the sum $$$s$$$ can't be obtained after any amount of operations, you should output -1.

Input: The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.The first line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \leq n, s \leq 2 \cdot 10^5$$$) — the length of the array and the needed sum of elements.The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 \leq a_i \leq 1$$$) — the elements of the array.It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to $$$s$$$, or -1 if obtaining an array with sum $$$s$$$ isn't possible.

Note: In the first test case, the sum of the whole array is $$$1$$$ from the beginning, so we don't have to make any operations.In the second test case, the sum of the array is $$$2$$$ and we want it to be equal to $$$1$$$, so we should remove the first element. The array turns into $$$[1, 0]$$$, which has a sum equal to $$$1$$$.In the third test case, the sum of the array is $$$5$$$ and we need it to be $$$3$$$. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into $$$[0, 1, 1, 1, 0, 0]$$$, which has a sum equal to $$$3$$$.

Sample Inputs:
7
3 1
1 0 0
3 1
1 1 0
9 3
0 1 0 1 1 1 0 0 1
6 4
1 1 1 1 1 1
5 1
0 0 1 1 0
16 2
1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1
6 3
1 0 1 0 0 0

Sample Outputs:
0
1
3
2
2
7
-1

Solution (Python):
""""""
from sys import stdin
get_int = lambda : int(stdin.readline())
get_ints = lambda : map(int, stdin.readline().split())
get_list = lambda : list(get_ints())
get_str = lambda : stdin.readline()[:-1]
get_list_str = lambda : list(map(str, stdin.readline()[:-1].split()))

def solve():
    (n, s) = get_ints()
    a = get_list()
    total = sum(a)
    if total == s:
        print(0)
        return
    if total < s:
        print(-1)
        return
    (res, cur, l) = (0, 0, 0)
    for (i, val) in enumerate(a):
        cur += val
        while cur > s:
            cur -= a[l]
            l += 1
        res = max(res, i - l + 1)
    print(n - res)

def main():
    test_case = 1
    test_case = get_int()
    while test_case:
        solve()
        test_case -= 1
    return
if __name__ == '__main__':
    main()

Solution (Python):
def main() -> None:
    for _ in range(ii()):
        (num, s) = mi()
        l = li()
        answer = num + 1
        (ll, rr) = (0, 0)
        while ll < num:
            if s == 0:
                answer = min(answer, ll + num - rr)
            if s < 0 or rr == num:
                s += l[ll]
                ll += 1
            else:
                s -= l[rr]
                rr += 1
        p(-1 if answer > num else answer)
if __name__ == '__main__':
    master_flag = False
    L = lambda string: exec('from hq import L\nL(string)') if not master_flag else False
    LT = lambda tc, custom, string: exec('from hq import LT\nLT(tc, custom, string)') if not master_flag else False
    import os, sys, math, itertools
    from collections import deque, defaultdict, OrderedDict, Counter
    from bisect import bisect, bisect_left, bisect_right, insort
    from heapq import heapify, heappush, heappop, nsmallest, nlargest, heapreplace, heappushpop
    (ii, si) = (lambda : int(input()), lambda : input())
    (mi, msi) = (lambda : map(int, input().strip().split(' ')), lambda : map(str, input().strip().split(' ')))
    (li, lsi) = (lambda : list(mi()), lambda : list(msi()))
    (out, export, p, pp) = ([], lambda : print('\n'.join(map(str, out)), end=''), lambda x: out.append(x), lambda array: p(' '.join(map(str, array))))
    try:
        exec('from hq import see,info,cmdIO,_generator_\nline=[cmdIO(),main(),export(),_generator_()]\nfor l in line:l')
    except (FileNotFoundError, ModuleNotFoundError):
        master_flag = True
        main()
        export()

Solution (Python):
from collections import defaultdict

def main():
    (n, s) = map(int, input().split())
    l = list(map(int, input().split()))
    lst = [-1]
    for i in range(n):
        if l[i] == 1:
            lst.append(i)
    lst.append(n)
    i = 0
    maxi = 0
    for j in range(s + 1, len(lst)):
        maxi = max(maxi, lst[j] - lst[i] - 1)
        i += 1
    if s > sum(l):
        print(-1)
        return
    print(n - maxi)
for i in range(int(input())):
    main()