- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to count minimum k length sublist that can be flipped to make all items of list to 0 in Python

Suppose we have a list of numbers called nums that stored 0s and 1s. We have another value k.

Now consider there is an operation where we flip a sublist of length k such that all 1s will be 0s and all 0s will be 1s. We have to find the minimum number of operations required to change nums into all 1s to 0s. If we cannot change it return -1.

So, if the input is like nums = [1,1,1,0,0,1,1,1], k = 3, then the output will be 2, as we can flip the first three numbers to zero and then flip the last three numbers to zero.

To solve this, we will follow these steps −

n := size of nums

res := 0, flipped := 0

to_conv := a list of size n and fill with 0

for i in range 0 to n, do

flipped := flipped XOR to_conv[i]

cur := nums[i]

cur := cur XOR flipped

if cur is same as 1, then

flipped := flipped XOR 1

res := res + 1

if i + k - 1 >= n, then

return -1

if i + k < n, then

to_conv[i + k] := 1

return res

## Example

Let us see the following implementation to get better understanding −

class Solution: def solve(self, nums, k): n = len(nums) res = 0 flipped = 0 to_conv = [0] * n for i in range(n): flipped ^= to_conv[i] cur = nums[i] cur ^= flipped if cur == 1: flipped ^= 1 res += 1 if i + k - 1 >= n: return -1 if i + k < n: to_conv[i + k] = 1 return res ob = Solution() nums = [1,1,1,0,0,1,1,1] k = 3 print(ob.solve(nums, k))

## Input

[1,1,1,0,0,1,1,1], 3

## Output

2

- Related Articles
- Program to find length of smallest sublist that can be deleted to make sum divisible by k in Python
- Program to count minimum invalid parenthesis to be removed to make string correct in Python
- Program to find length of longest sublist whose sum is 0 in Python
- Program to count average of all special values for all permutations of a list of items in Python
- C++ Program to count minimum problems to be solved to make teams
- Program to find length of longest sublist containing repeated numbers by k operations in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Program to count minimum number of operations to flip columns to make target in Python
- Program to find shortest sublist so after sorting that entire list will be sorted in Python
- Program to find length of longest distinct sublist in Python
- Program to count number of on lights flipped by n people in Python
- Program to count minimum deletions needed to make character frequencies unique in Python
- Program to count subsets that sum up to k in python
- Program to find minimum distance that needs to be covered to meet all person in Python
- Program to count minimum semesters to cover all different courses in Python