Professional Documents
Culture Documents
return list;
}
7
1 2 1 3 4 2 3
4
ans: 3 4 4 3
---------------------------------------------------------------------
//Largest Subarray With Zero Sum
if(map.containsKey(sum))
{
mlen = Math.max(mlen, i-map.get(sum));
}
else
map.put(sum,i);
}
return mlen;
}
8
15 -2 2 -8 1 7 10 23
ans: 5
-----------------------------------------------------------------------
//Count Of All Subarrays With Zero Sum
Note: only those will make sum subarray whoes freq repeated;
sum repeated means after becoming zero again.
if(map.containsKey(sum)){
count += map.get(sum); //stored freq of sum
map.put(sum, map.get(sum)+1);
}else{
map.put(sum,1);
}
}
return count;
8
15 -2 2 -8 1 7 10 23
ans: 3
---------------------------------------------------------------------
//Largest Subarray With Contiguous Elements
note: (maxele - minele = i - j); then sequence must be contiguous.
if repeat ele found skip or break;
if(max-min==j-i)
{
len = j-i+1;
ans = Math.max(len,ans);
}
}
return ans;
3
10 12 11
ans: 3
----------------------------------------------------------------------------------
//Smallest Substring Of A String Containing All Characters Of Another String
int mct = 0;
int dmct = s2.length(); //s2 len substring we want
while(true)
{
boolean f1 = false;
boolean f2 = false;
//aquire from s1
f1 = true;
}
j++;
char ch = s1.charAt(j);
if(map1.get(ch)==1){
map1.remove(ch);
}
else{
map1.put(ch, map1.get(ch)-1);
}
if(map1.getOrDefault(ch,0) < map2.getOrDefault(ch,0))
mct--;
f2 = true;
}
if(f1==false && f2==false)
break;
}
return ans;
}
timetopractice
toc
ans: toprac
-------------------------------------------------------------------------------
Note: Hashset takes unique elements. (creates map of unique elements)
//Smallest Substring Of A String Containing All Unique Characters Of Itself
int i = -1;
int j = -1;
HashMap<Character, Integer>map = new HashMap<>();
while(true)
{ boolean f1 = false;
boolean f2 = false;
f1 = true;
}
j++;
char ch = str.charAt(j);
if(map.get(ch)==1){
map.remove(ch);
}else{
map.put(ch, map.get(ch)-1);
}
f2 = true;
}
if(f1==false && f2==false)
break;
}
return ans;
}
i/p : aabcbcdbca
o/t : 4
---------------------------------------------------------------------
//Longest Substring With Non Repeating Characters
while(true)
{
boolean f1 = false;
boolean f2 = false;
//aquire
while(i<str.length()-1)
{ f1 = true;
i++;
char ch = str.charAt(i);
map.put(ch, map.getOrDefault(ch,0)+1);
if(map.get(ch)==2)
{
break;
}
else{
int len = i-j;
if(len>ans)
ans = len;
}
}
//release
while(j<i)
{ f2 = true;
j++;
char ch = str.charAt(j);
map.put(ch, map.get(ch)-1);
if(map.get(ch)==1)
break;
}
if(f1==false && f2==false)
break;
}
return ans;
i/p : aabcbcdbca
o/p : 4