Hashing Flashcards

1
Q

Largest continuous sequence with 0 sum

A
Unordered map m
Int presum=0
Maxlen=0
Int left=-1 , right=-1
For i from 0 to n
Presum+=a(i)
If presum==0
  Left=0 right is i
   Maxlen =i+1
If(m.find(presum)!=m.end())
 If(i-m(presum) > maxlen)
 Then left=m(presum)+1
 Right=i
Maxlen=max(maxlen,i-m(presum))
If(m.find(presum))==m end ())
 M insert(presum,i))
If left is -1 and right is -1 return v
Else for i from left to right 
 Add a(i) to v 
And return v
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Longest subarray length
Given an array of 0s and 1s, find the length of the longest subarray such that the count of 1s is more than the count of 0s

A
Vector int a
 If v(i) is 0 then push -1 to v else 1
Int sum =1
Unordered map s
 Presum =0, res=0
For i from 0 to n
 Presum+=a(i)
If presum==sum, 
  Res=i+1
If(s.find(presum-sum)!=s.end())
 Res=max(res, i-s(presum-sum))
Else if it is not already in the map
 S.insert(presum,i)
Return res
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

2 sum

A
Unordered map m
Int second=infi
Int first=infi
 Stores first kccurances of different numbers.
For i from 0 to n
If m.find(b-a(i))!=m.end()
 Auto it=m find(b-a(i))
 Auto prev=it.second
 If (iprev)
   First=prev
If(m.find(a(i)==m.end())
 M.insert(a(i),i)
 If first and second are within bounds
Then push them res and return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4 sum
A+b+c+d = target?
Given num array and target

A
For i from 0 to n
 Int target3 =target-num(i)
 For j from i+1 to n
  Target2=target3 -num(j)
  Int front = j+1
  Int back =n-1
 While (front
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Valid soduku

A
Vector > rows, col, grid
Pair:: iterator,bool> ret
For i from 0 to 9
 For j from 0 to 9
 Charc= board(i,j)
 If c is . continue
Int gridIDx= coordtogrididx(i,j)
Ret=row(i).insert(c)
I(!ret.second) return false
Ret=col(j).insert(c)
If(!ret.second) return false
Ret=grids(gridIDx).insert(c)
If(!ret.second) return false

Return true

Coorto grid i/3*3+j/3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Pairs with given xors

A
If a xor b is c , then a xor c is b and b xor c is a. 
Now the problem is similar to finding pair with given sum
Unordered set s
Int count=0
For i from 0 to n
 If (s.find(a(i) xor b)!=s end())
   Count++
S.insert(a(i))

Return count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Anagrams

Given an rray of strings, return all groups of strings that are anagrams

A
Vector> Sol
For i from 0 to n
  Temp=a(i)
  Sort(temp begin(), temp.end())
  M.push_back(i+1)
Auto it=m.begin()
While (i!=m.end())
  Sol.push_back(it.second)
  It++
Return Sol
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Equal a+b=c+d? Multiple solutions, return lexographically smallest

A
Map(int, pairs(int,int)) sum
Vector int ans
For i from 0 to n
 For j from i+1 to n
  Auto sumab=a(i)+a(j)
  If(sum.find(sumab)==sum.end())
    Sum(sumab) =make_pair(i,j)
  Else
  Pair p = sum(sumab)
 If (p.first<i>temp(t)
  Ans.clear()
  Ans=temp
  Break

Return ans</i>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Check palindrome

A
Unordered map m
Stores frequencies of elements in map
Int count is 0
 For auto it: m
   If it.second%2 is 1
    Count++
If(n%2 is 1 and count<= 1 )
 Ret 1
Else if (n%2 is 0 and count is 0)
 Ret 1

Ret 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Increment problem

Given a stream of numbers a , on arrival of each number increase the first occurance by 1 and include in the stream

A
Unordered map m
For i from 0 to n
If a(i) is not already present in the map 
  M(a(i)+1)=m(a(i))
M(a(i))=i

For auto it in m
A(i.second)=i.first

Ret a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Subarray with given xor

A
Long long ans=0
Create xorarr
Unordered map m
Create a prefix xor sum array such that xorarr(i) has value equal to xor of all elements form arr(0,i)
Create a map that stores number of prefix array elements  corresponding to xor value
Xorarr(0)=are(0)
Computing xor array
 For i from 1 to 
 Xorarr(i)= xor(i-1) and a(i)
Calculate answer
For i from 0 to n
 Int temp=m xor xorarr(i)
If above xor exists in map, then there is another prefix with same xor , that is there is subarray ending at i with xor equal to m
Ans+=m(temp)
If this subarray has xor equal to m itself
 If (xorarr(i)==m)
  Ans++
Add xor of this subarray to map
M(xorarr(i))++

Return ans

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Subarray with b odd numbers

Find the total number of subarrays having exactly b odd number

A
Change even numbers to 0 and odd numbers to 1
Now problem reduces to finding largest subarray with sum b
Unordered map m
Int count=0
Int presum=0
For i from 0 to n
Presum+=a(i)
If presum==b
 Count++
If(m.find(presum-b)!=m.end())
 Count+=m(presum-b)
M(presum)++
Return count
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given a string find the length of the longest substring without any repeating char

A

Map char, int seen
Used to store last position of occurrence
Int max_len is 0
Int start=0

For end from 0 to n
If (seen.find(s(end))!=seen.end())
 Start=max(start,seen(s(end))+1)
Seen(s(end))=end
Max_len=max(max_len,end-start+1)

Return max_len

How well did you know this?
1
Not at all
2
3
4
5
Perfectly