# Longest subarray having difference in the count of 1’s and 0’s equal to k

Given a binary array **arr[]** of size **n** and a value **k**. The task is to find the length of the longest subarray having difference in the count of 1’s and 0’s equal to **k**. The count of 1’s should be equal to or greater than the count of 0’s in the subarray according to the value of **k**.

Examples:

Input: arr[] = {0, 1, 1, 0, 1}, k = 2 Output: 4 The highlighted portion is the required subarray {0,1, 1, 0, 1}. In the subarray count of 1's is 3 and count of 0's is 1. Therefore, difference in count = 3 - 1 = 2. Input: arr[] = {1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1}, k = 0 Output: 6 The highlighted portion is the required subarray {1, 1,0, 1, 1, 1, 0, 0, 1, 1, 1}. In the subarray count of 1's is 3 and count of 0's is 3. Therefore, difference in count = 3 - 3 = 0.

**Naive Approach:** Consider the difference in the count of 1’s and 0’s of all the sub-arrays and return the length of the longest sub-array having required difference equal to ‘k’. Time Complexity will be O(n^2).

**Efficient Approach:** This problem is a variation of finding the longest sub-array having sum k. Replace all the 0’s in the **arr[]** with -1 and then find the **longest subarray of ‘arr’ having sum equal to ‘k’.**

Below is the implementation of above approach:

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find the length of the longest ` `// subarray having difference in the count ` `// of 1's and 0's equal to k ` `int` `lenOfLongSubarr(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// unordered_map 'um' implemented ` ` ` `// as hash table ` ` ` `unordered_map<` `int` `, ` `int` `> um; ` ` ` `int` `sum = 0, maxLen = 0; ` ` ` ` ` `// traverse the given array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// accumulate sum ` ` ` `sum += ((arr[i] == 0) ? -1 : arr[i]); ` ` ` ` ` `// when subarray starts from index '0' ` ` ` `if` `(sum == k) ` ` ` `maxLen = i + 1; ` ` ` ` ` `// make an entry for 'sum' if it is ` ` ` `// not present in 'um' ` ` ` `if` `(um.find(sum) == um.end()) ` ` ` `um[sum] = i; ` ` ` ` ` `// check if 'sum-k' is present in 'um' ` ` ` `// or not ` ` ` `if` `(um.find(sum - k) != um.end()) { ` ` ` ` ` `// update maxLength ` ` ` `if` `(maxLen < (i - um[sum - k])) ` ` ` `maxLen = i - um[sum - k]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// required maximum length ` ` ` `return` `maxLen; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 0, 1, 1, 0, 1 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 2; ` ` ` `cout << ` `"Length = "` ` ` `<< lenOfLongSubarr(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Length = 4

**
Time Complexity:** O(n)

**Auxiliary Space:**O(n)

## Recommended Posts:

- Longest subarray such that the difference of max and min is at-most one
- Longest Subarray with Sum greater than Equal to Zero
- Longest subarray having average greater than or equal to x
- Longest subarray with elements having equal modulo K
- Longest subarray having average greater than or equal to x | Set-2
- Longest subarray having count of 1s one more than count of 0s
- Longest Subarray with first element greater than or equal to Last element
- Count all distinct pairs with difference equal to k
- Longest subarray with sum divisible by k
- Longest subarray having maximum sum
- Longest subarray with only one value greater than k
- Longest increasing subarray
- Longest Subarray of non-negative Integers
- Length of the longest alternating subarray
- Longest subarray with elements divisible by k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.