Game (Codeforces 1649A)
Tags: Implement, implementation

You are playing a very popular computer game. The next level consists of $$$n$$$ consecutive locations, numbered from $$$1$$$ to $$$n$$$, each of them containing either land or water. It is known that the first and last locations contain land, and for completing the level you have to move from the first location to the last. Also, if you become inside a location with water, you will die, so you can only move between locations with land.You can jump between adjacent locations for free, as well as no more than once jump from any location with land $$$i$$$ to any location with land $$$i + x$$$, spending $$$x$$$ coins ($$$x \geq 0$$$).Your task is to spend the minimum possible number of coins to move from the first location to the last one.Note that this is always possible since both the first and last locations are the land locations.

Input: There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. This is followed by the test cases description.The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of locations.The second line of the test case contains a sequence of integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 1$$$), where $$$a_i = 1$$$ means that the $$$i$$$-th location is the location with land, and $$$a_i = 0$$$ means that the $$$i$$$-th location is the location with water. It is guaranteed that $$$a_1 = 1$$$ and $$$a_n = 1$$$.

Output: For each test case print a single integer — the answer to the problem.

Note: In the first test case, it is enough to make one free jump from the first location to the second one, which is also the last one, so the answer is $$$0$$$.In the second test case, the only way to move from the first location to the last one is to jump between them, which will cost $$$4$$$ coins.In the third test case, you can jump from the first location to the third for $$$2$$$ coins, and then jump to the fourth location for free, so the answer is $$$2$$$. It can be shown that this is the optimal way.

Sample Inputs:
3
2
1 1
5
1 0 1 0 1
4
1 0 1 1

Sample Outputs:
0
4
2

Solution (Python):
def solve():
    input()
    xs = list(map(int, input().split()))
    if 0 not in xs:
        return 0
    return len(xs) - xs[::-1].index(0) - xs.index(0) + 1

def main():
    t = int(input())
    for i in range(t):
        print(solve())
if __name__ == '__main__':
    main()

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    l = [int(x) for x in input().split()]
    i = 1
    r = n - 2
    while i <= n - 1:
        if l[i] == 1:
            i += 1
        else:
            break
    while r >= 0:
        if l[r] == 1:
            r -= 1
        else:
            break
    if r - i + 2 <= 0:
        print(0)
    else:
        print(r - i + 2)

Solution (Python):
t = int(input())
for zz in range(t):
    n = int(input())
    l = list(map(int, input().split()))
    i = 0
    j = n - 1
    for _ in range(n):
        while i < j and l[i + 1] == 1:
            i += 1
        while j > i and l[j - 1] == 1:
            j -= 1
    print(max(j - i, 0))