Skip to content

Commit 44cc9b6

Browse files
solved Minimum flips in circular array
1 parent 83c5089 commit 44cc9b6

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Minimum Flips in Circular Array.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,35 @@
3838
3939
Sample Explanation
4040
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
41-
"""
41+
"""
42+
43+
# Solution with explanation:
44+
45+
46+
def solve(arr, length, ones, zeros):
47+
ansO = ones - sum(arr[0:ones])
48+
ansZ = sum(arr[ones : ones + zeros])
49+
ans = ansO + ansZ
50+
print(ansO, ansZ, ans)
51+
for i in range(1, length):
52+
j = i + ones
53+
k = j + zeros
54+
55+
if arr[j - 1] and not arr[i - 1]:
56+
ansO = max(0, ansO - 1)
57+
if not arr[j - 1] and arr[i - 1]:
58+
ansO += 1
59+
60+
if arr[k - 1] and not arr[j - 1]:
61+
ansZ += 1
62+
if not arr[k - 1] and arr[j - 1]:
63+
ansZ = max(0, ansZ - 1)
64+
print(arr[i:k])
65+
print(ansO, ansZ)
66+
ans = min(ans, ansO + ansZ)
67+
return ans
68+
69+
70+
N, O, Z = [int(x) for x in input().split(" ")]
71+
A = [int(x) for x in input().split(" ")]
72+
print(solve(A, N, O, Z))

0 commit comments

Comments
 (0)