js 判断数组长度相等
Problem statement:
问题陈述:
Given an array which consists of 0's and 1's, your task is to find the maximum lengths of the largest subarray with an equal number of 0's and 1's. The test cases consist of array length and its elements. The first line of the input test cases is the array length N and the second line is the array elements.
给定一个由0和1组成的数组,您的任务是查找数量相等的0和1的最大子数组的最大长度。 测试用例由数组长度及其元素组成。 输入测试用例的第一行是数组长度N ,第二行是数组元素。
Input:
输入:
The first line of the input is T denoting the number of test cases. Then T test cases follow. Each test case contains two lines. The first line of each test case is a number N denoting the size of the array and in the next line are N space-separated values of A[].
输入的第一行是T,表示测试用例的数量。 然后是T测试用例。 每个测试用例包含两行。 每个测试用例的第一行是表示数组大小的数字N ,下一行是N个以空格分隔的A []值。
Output:
输出:
For each test case output in a new line the max length of the subarray.
对于换行中的每个测试用例输出,子数组的最大长度。
Example with explanation:
带有说明的示例:
Input:
T = 1
N = 3
A = [1,0,1]
Output:
2
As [1,0] or [0,1] is longest contigous subarray
with equal number of 0 and 1.
Input:
T = 1
N = 6
A = [1 1 0 0 1 0]
Output:
6
As [1 1 0 0 1 0],combines to form largest subarray
with equal number of zeros and ones.
Solution Approach
解决方法
1) Brute Force Approach
1)蛮力法
We will check every possible subarray within the given array and count the number of zeros and ones in each subarray. If the number of ones and zeros are equal then we store the length of that subarray in our temporary solution, simultaneously we would store the maxlength of the subarray by checking the maxlength with every temporary solution to return it as the final solution.
我们将检查给定数组中的每个可能的子数组,并计算每个子数组中零和一的数量。 如果一和零的数量是相等的,然后我们存储子数组的长度在我们临时的解决方案,同时我们将通过检查的最大长度与每一个临时解决方案,使其恢复为最终解决方案的子阵的最大长度存储。
Pseudo Code:
伪代码:
initialise, maxlen=0
for i in range((A)):
zerocnt=0,onecnt=0
for j=i in range(A):
if(A[j]==0)zerocnt++
if(A[j]==1)onecnt++
if(zerocnt==onecnt)
maxlen=(maxlen,j-i+1)
Finally return maxlen
The time complexity for the above method in the worst case of O(n*n), where n=length of the given Array.
在O(n * n)的最坏情况下,上述方法的时间复杂度,其中n =给定数组的长度。
C++ Implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Enter number of test cases: ";
int t;
cin >> t;
while (t--) {
int n;
cout << "Enter size of array: ";
cin >> n;
int A[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++)
cin >> A[i];
int maxlen = 0;
for (int i = 0; i < n; i++) {
int zerocnt = 0, onecnt = 0;
for (int j = i; j < n; j++) {
if (A[j] == 0)
zerocnt++;
if (A[j] == 1)
onecnt++;
if (zerocnt == onecnt)
maxlen = max(maxlen, j - i + 1);
}
}
cout << "Maximum size is: ";
cout << maxlen << "\n";
}
return 0;
}
Output
输出量
Enter number of test cases: 3
Enter size of array: 4
Enter elements: 1 0 0 1
Maximum size is: 4
Enter size of array: 6
Enter elements: 1 1 0 0 1 0
Maximum size is: 6
Enter size of array: 1
Enter elements: 0
Maximum size is: 0
2) Hashing Approach
2)散列方法
In this, we will use the hashing technique to beat the time in the best possible manner.
在这种情况下,我们将使用哈希技术以最佳方式击败时间。
First, we will convert all zeros in the array to -1 and we would not change the ones since we need to calculate the maxlength of the subarray which have an equal number of ones and zeros(now -1) that is subarray with overall sum equals to zero.
首先,我们将数组中的所有零转换为-1,并且不会更改它们,因为我们需要计算子数组的最大长度,该子数组具有相等的1和零(现在为-1),即总和的子数组等于零。
To find the subarray with sum zero we will use a counter which will count the cumulative sum of the array, the hash map key would the cumulative counter value and the hash map value for each key would be the index, if the counter value is already existing in the hash map then it means that it has occurred before, so we take the difference between the current index and the hash map value. We keep storing the temporary length of the subarray having zero-sum and simultaneously checking the maxlength of the subarray.
Here we will use unordered_map since it works efficiently in retrieval case.
为了找到总和为零的子数组,我们将使用一个计数器,该计数器将对数组的累加和进行计数,哈希计数器键将是累积计数器值,而每个键的哈希图值将是索引(如果计数器值已经存在)存在于哈希图中,则意味着它已经发生过,因此我们将当前索引与哈希图值之间的差值考虑在内。 我们不断保存具有零和,并同时检查子数组的最大长度的子数组的长度暂时。
这里我们将使用unordered_map,因为它在检索情况下有效地工作。
Pseudo Code:
伪代码:
Initialise, maxlen=0
hashmap ma
// since index start with 0 and we would avoid to add +1
// each time when we take the difference for length.
ma[0]=-1
counter=0
for i in range(A):
if(A[i]==0)
counter=counter+(-1)
else if(A[i]==1)
counter=counter+(1)
if(ma.find(counter)!=ma.end())
maxlen=max(maxlen,i-ma[counter])
else
ma[counter]=i
Finally return maxlen
The time complexity for the above case if O(n) in the worst case.
如果在最坏的情况下为O(n) ,则上述情况的时间复杂度。
C++ Implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Enter number of test cases: ";
int t;
cin >> t;
while (t--) {
int n;
cout << "Enter size of array: ";
cin >> n;
int A[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++)
cin >> A[i];
unordered_map<int, int> ma;
ma[0] = -1;
int maxlen = 0;
int counter = 0;
for (int i = 0; i < n; i++) {
if (A[i] == 0)
counter = counter + (-1);
else if (A[i] == 1)
counter = counter + (1);
if (ma.find(counter) != ma.end())
maxlen = max(maxlen, i - ma[counter]);
else {
ma[counter] = i;
}
}
cout << "Maximum size is: ";
cout << maxlen << "\n";
}
return 0;
}
Output
输出量
Enter number of test cases: 3
Enter size of array: 4
Enter elements: 1 0 0 1
Maximum size is: 4
Enter size of array: 6
Enter elements: 1 1 0 0 1 0
Maximum size is: 6
Enter size of array: 1
Enter elements: 0
Maximum size is: 0
翻译自: https://www.includehelp.com/icp/length-of-the-largest-subarray-with-equal-number-of-0s-and-1s.aspx
js 判断数组长度相等