`{"id":"bcfaca22-86aa-472d-8a34-647a949bb3ea","name":"Maximum Consecutive Ones - 2","description":"1. You are given an array(arr) which contains only 0's and 1's and a number K.\r\n2. You have to find the maximum number of consecutive 1's in the given array if you can flip at most K zeroes.","inputFormat":"A number N\r\narr1\r\narr2... N numbers\r\nA number K","outputFormat":"A number representing maximum number of consecutive 1's after flipping at most K zeroes.","constraints":"1 &lt;= N &lt;= 10^5\r\n0 &lt;= arr[i] &lt;= 1\r\n1 &lt;= K &lt;= N","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nint solution(vector<int> arr, int k) {\n //write your code here\n}\n\nint main()\n{\n int n, k;\n cin >> n;\n vector<int>arr(n, 0);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n };\n cin >> k;\n cout << solution(arr, k);\n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int[] arr, int k){\r\n // write your code here\r\n\r\n return 0;\r\n }\r\n \r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n arr[i] = scn.nextInt();\r\n }\r\n int k = scn.nextInt();\r\n System.out.println(solution(arr,k));\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n1 1 0 0 1 1\r\n1","sampleOutput":"3\r\n","questionVideo":"https://www.youtube.com/embed/QPfalDbqa4A?end=220","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"b8135ba5-16c5-4946-8959-323221636b7a","name":"Maximum Consecutive Ones - 2","slug":"maximum-consecutive-ones-2","type":1}],"next":{"id":"fea236bd-7df8-4fb4-9b38-1083470c96e0","name":"Maximum Consecutive Ones - 2 MCQ","type":0,"slug":"maximum-consecutive-ones-2-mcq"},"prev":{"id":"3df74c44-a7b0-48d4-bac7-e3e1010ad198","name":"Maximum Consecutive Ones - 1","type":3,"slug":"maximum-consecutive-ones-1"}}}`

# Maximum Consecutive Ones - 2

1. You are given an array(arr) which contains only 0's and 1's and a number K. 2. You have to find the maximum number of consecutive 1's in the given array if you can flip at most K zeroes.

`{"id":"bcfaca22-86aa-472d-8a34-647a949bb3ea","name":"Maximum Consecutive Ones - 2","description":"1. You are given an array(arr) which contains only 0's and 1's and a number K.\r\n2. You have to find the maximum number of consecutive 1's in the given array if you can flip at most K zeroes.","inputFormat":"A number N\r\narr1\r\narr2... N numbers\r\nA number K","outputFormat":"A number representing maximum number of consecutive 1's after flipping at most K zeroes.","constraints":"1 &lt;= N &lt;= 10^5\r\n0 &lt;= arr[i] &lt;= 1\r\n1 &lt;= K &lt;= N","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nint solution(vector<int> arr, int k) {\n //write your code here\n}\n\nint main()\n{\n int n, k;\n cin >> n;\n vector<int>arr(n, 0);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n };\n cin >> k;\n cout << solution(arr, k);\n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int[] arr, int k){\r\n // write your code here\r\n\r\n return 0;\r\n }\r\n \r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n arr[i] = scn.nextInt();\r\n }\r\n int k = scn.nextInt();\r\n System.out.println(solution(arr,k));\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n1 1 0 0 1 1\r\n1","sampleOutput":"3\r\n","questionVideo":"https://www.youtube.com/embed/QPfalDbqa4A?end=220","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"b8135ba5-16c5-4946-8959-323221636b7a","name":"Maximum Consecutive Ones - 2","slug":"maximum-consecutive-ones-2","type":1}],"next":{"id":"fea236bd-7df8-4fb4-9b38-1083470c96e0","name":"Maximum Consecutive Ones - 2 MCQ","type":0,"slug":"maximum-consecutive-ones-2-mcq"},"prev":{"id":"3df74c44-a7b0-48d4-bac7-e3e1010ad198","name":"Maximum Consecutive Ones - 1","type":3,"slug":"maximum-consecutive-ones-1"}}}`

Editor

# Maximum Consecutive Ones - 2

easy

1. You are given an array(arr) which contains only 0's and 1's and a number K. 2. You have to find the maximum number of consecutive 1's in the given array if you can flip at most K zeroes.

## Constraints

1 <= N <= 10^5 0 <= arr[i] <= 1 1 <= K <= N

## Format

### Input

A number N arr1 arr2... N numbers A number K

### Output

A number representing maximum number of consecutive 1's after flipping at most K zeroes.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 1 1 0 0 1 1 1```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}3 ```

Question Video

Discussions

Show Discussion

Related Resources