-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMinimum-Flips-in-Circular-Array.py
More file actions
66 lines (50 loc) · 2.05 KB
/
Minimum-Flips-in-Circular-Array.py
File metadata and controls
66 lines (50 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Question: Minimum Flips in Circular Array
You are given a circular binary array Arr of size N (A circular array is an array where we consider the first and last element to be adjacent).
In one operation you can choose an index i(1<=i<=N) and flip the number at index i, i.e.if Arr[i]=1 you can make it 0 and vice versa
You are given two integers O and Z.Your aim is to make a subarray of size O+Z such that the first O digits of the subarray are 1s and the last Z digits of the subarray are 0s
What is the minimum number of operations required to do so?
Note: A subarray is a slice from a contiguous array (i.e., occupy consecutive positions) and inherently maintains the order of elements.
Input Format
The first line of the input contains 3 space-separated integers denoting N O and Z respectively
The next line contains N space separated integers denoting Arr[i](1<=i<=N)
Output Format
Print an integer denoting the minimum number of operations required to make a subarray of size O+Z such that the first O digits of the subarray are 1s and the last Z digits of the subarray are 0s
Constraints
1<=N<=10^5
0<=O,Z<=N
1<=O+Z<=N
0<=Arr[i](1<=i<=N)<=1
Sample Input
4 1 1
1 1 1 1
1
Sample Output
1
Sample Explanation
One of the solutions is to flip the digit at index 2 resulting in our array as 1 0 1 1 and 1 0(index 1 and 2) will be our required subarray
"""
# Solution with explanation:
def solve(arr, length, ones, zeros):
ansO = ones - sum(arr[0:ones])
ansZ = sum(arr[ones : ones + zeros])
ans = ansO + ansZ
j = ones
k = ones + zeros
for i in range(1, length):
j +=1
k +=1
if arr[j - 1] and not arr[i - 1]:
ansO = max(0, ansO - 1)
if not arr[j - 1] and arr[i - 1]:
ansO += 1
if arr[k - 1] and not arr[j - 1]:
ansZ += 1
if not arr[k - 1] and arr[j - 1]:
ansZ = max(0, ansZ - 1)
ans = min(ans, ansO + ansZ)
return ans
N, O, Z = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
A += A[: O + Z - 2]
print(solve(A, N, O, Z))