längste Subtarray mit gleichen 0 und 1
private static int longestSubarrayWithEqualNumberOfZeroAndOne(int[]arr){
// AyushG Notes
// replace 0 with -1 int the given array and the problem will be converted to
// longestSubarraySum problem with given sum = 0
int currSum=0;
int sum=0;
int maxLen=0;
for(int i=0; i<arr.length; i++){
if(arr[i]==0){
arr[i]=-1;
}
}
HashMap<Integer,Integer> map = new HashMap<>();
for(int i =0; i<arr.length; i++){
currSum=currSum+arr[i];
if(currSum==sum){
maxLen=i+1;
}
if(!map.containsKey(currSum)){
map.put(currSum,i);
}
if(map.containsKey(currSum-sum)){
maxLen = Math.max(maxLen, i-map.get(currSum-sum));
}
}
return maxLen;
}
Techie Ash