Skip to content

Commit fb62069

Browse files
Hashing Important Questions Added
1 parent f26337d commit fb62069

23 files changed

+1081
-0
lines changed

Hashing/1. ApplicationsOfHashing.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Applications of hashing:
2+
3+
// 1. Dictionaries Implementation
4+
// 2. Database indexing
5+
// 3. Cryptography
6+
// 4. Caches
7+
// 5. Symbol Tables in Compilers/Interpreters
8+
// 6. Routers
9+
// 7. Getting data from databases

Hashing/10. Unordered_map.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
**** Unordered_map: ****
2+
3+
Used to store key-value pairs
4+
Uses Hashing Algorithms
5+
It has no specific order of keys.
6+
7+
8+
#include<iostream>
9+
#include<unordered_map> m;
10+
11+
using namespace std;
12+
13+
int main()
14+
{
15+
unordered_map<string ,int> m;
16+
m["gfg"]= 20;
17+
m["ide"]=30;
18+
m.insert(["courses", 15]);
19+
20+
for(auto x: m)
21+
cout<<x.first<<" "<<x.second<<endl;
22+
23+
return 0;
24+
}
25+
26+
27+
// Program 2
28+
29+
#include<iostream>
30+
#include<unordered_map> m;
31+
32+
using namespace std;
33+
34+
int main()
35+
{
36+
unordered_map<string, int> m;
37+
m["gfg"]= 20;
38+
m["ide"]= 30;
39+
m["courses"]= 15;
40+
if(m.find("ide")!=m.end())
41+
cout<<"Found\n";
42+
43+
else
44+
cout<<"Not Found\n";
45+
46+
for(auto it = m.begin(); it!=m.end(); it++)
47+
cout<<(it->first)<<" "<<(it->second)<<endl;
48+
49+
50+
51+
if(m.count("ide") > 0)
52+
cout<<"Found";
53+
54+
else
55+
cout<<"Not Found";
56+
}
57+
58+
59+
#include<iostream>
60+
#include<unordered_map>m;
61+
62+
using namespace std;
63+
int main()
64+
{
65+
unordered_map<string , int> m;
66+
m["gfg"]=20;
67+
m["ide"]=30;
68+
m["courses"]=15;
69+
70+
cout<<m.size()<<" ";
71+
m.erase("ide");
72+
m.erase(m.begin());
73+
cout<<m.size()<<" ";
74+
75+
return 0;
76+
}

Hashing/11. CountDistinctElements.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//Naive Solution
2+
// Time: Theta(n^2)
3+
//space: O(1)
4+
5+
6+
int countDist(int arr[], int n)
7+
{
8+
int res=0;
9+
for(int i=0;i<n;i++)
10+
{
11+
bool flag= false;
12+
13+
for(int j=0;j<i;j++)
14+
{
15+
if(arr[i]==arr[j])
16+
{
17+
flag= true;
18+
break;
19+
}
20+
}
21+
if(flag==false)
22+
res++;
23+
}
24+
return res;
25+
}
26+
27+
28+
//Optimized Solution : using Unordered_set
29+
// Time: theta(n)
30+
// space: O(n)
31+
int countDistinct(int arr[], int n)
32+
{
33+
unordered_set<int>s;
34+
35+
for(int i=0;i<n;i++)
36+
s.insert(arr[i]);
37+
38+
return s.size();
39+
40+
}
41+
42+
43+
44+
//More Optimized way of coding
45+
46+
// int countDistinct(int arr[], int n)
47+
// {
48+
// unordered_set<int> s(arr, arr+n);
49+
// return s.size();
50+
// }
51+
52+
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//Naive Approach
2+
//Time Complexity : O(n^2)
3+
//Aux Space : O(1)
4+
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
8+
void prinFreq (int arr[], int n)
9+
{
10+
for(int i=0;i<n;i++)
11+
{
12+
bool flag= false;
13+
for(int j=0;j<i;j++)
14+
if(arr[i]==arr[j]) { flag = true; break; }
15+
16+
if(flag== true)
17+
continue;
18+
19+
int freq = 1;
20+
for(int j= i+1; j<n; j++)
21+
if(arr[i]==arr[j])
22+
freq++;
23+
24+
cout<<arr[i]<<" "<<freq<<endl;
25+
}
26+
}
27+
28+
29+
//Efficient Solution
30+
int countFreq(int arr[], int n)
31+
{
32+
unordered_map<int , int> h;
33+
for(int i=0;i<n;i++)
34+
h[arr[i]]++;
35+
36+
for(auto e: h)
37+
cout<<e.first<<" "<<e.second<<endl;
38+
}
39+
40+
//Aux Space: O(n)
41+
//TIme Complexity: Theta(n)
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//Naive Solution:
2+
//Time Complexity: O(m*(m+n))
3+
#include<bits/stdc++.h>
4+
int intersection(int a[], int b[], int m , int n)
5+
{
6+
int res=0;
7+
for(int i=0;i<m;i++)
8+
{
9+
bool flag= false;
10+
for(int j=0;j<i-1;j++)
11+
{
12+
if(a[j]==a[i]) { flag= true; break;}
13+
14+
}
15+
16+
if(flag== true)
17+
{
18+
continue;
19+
}
20+
21+
for(int j=0;j<n;j++)
22+
{
23+
if(a[i]==b[j]) { res++; break;}
24+
}
25+
}
26+
}
27+
28+
29+
30+
31+
//Efficient Solution:
32+
//Time Complexity: O(m+n)
33+
//Aux Space: o(m)
34+
35+
int intersection (int a[], int b[], int m , int n)
36+
{
37+
unordered_set<int> s;
38+
for(int i=0; i<m;i++)
39+
{
40+
s.insert(a[i]);
41+
}
42+
43+
int res=0;
44+
for(int j=0;j<n;j++)
45+
{
46+
if(s.find(b[j])!= s.end())
47+
{
48+
res++;
49+
s.erase(b[i]);
50+
}
51+
}
52+
53+
return res;
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Naive Approach
2+
//Time Complexity: O((m+n) * (m+n))
3+
//Aux Space: O(m+n)
4+
5+
int findUnion(int a[], int b[], int m , int n)
6+
{
7+
int c[m+n];
8+
for(int i=0; i<m;i++)
9+
c[i]=a[i];
10+
11+
for(int i=0; i<n;i++)
12+
c[m+i] = b[i];
13+
14+
int res=0;
15+
for(int i=0; i<m+n; i++)
16+
{
17+
bool flag= false;
18+
for(int j=0; j<i;j++)
19+
{
20+
if(c[i]==c[j]) { flag= true; break; }
21+
}
22+
23+
if(flag== false) res++;
24+
}
25+
26+
return res;
27+
28+
}
29+
30+
//Efficient solution
31+
//Time Complexity: O(m+n)
32+
//Aux Space: O(m+n)
33+
int findUnion(int a[], int b[], int m , int n)
34+
{
35+
unordered_set<int> s;
36+
37+
for(int i=0; i<m ;i++)
38+
s.insert(a[i]);
39+
40+
for(int j=0;j<n;j++)
41+
s.insert(b[i]);
42+
43+
return s.size();
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//Pair with given sum in an unsorted array
2+
3+
//Naive Solution
4+
5+
bool isPair(int arr[], int n, int sum)
6+
{
7+
for(int i=0; i<n;i++)
8+
for(int j=i+1; j<n;j++)
9+
if(arr[i]+arr[j] == sum)
10+
return true;
11+
12+
return false;
13+
}
14+
15+
16+
//Efficient Solution
17+
18+
bool isPair(int arr[], int n, int sum)
19+
{
20+
unordered_set<int>s;
21+
for(int i=0;i<n;i++)
22+
{
23+
if(s.find(sum-arr[i]) != s.end())
24+
return true;
25+
26+
s.insert(arr[i]);
27+
}
28+
29+
return false;
30+
}

Hashing/16. SubArray with Sum 0.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Naive Solution
2+
// Time Complexity: O(n^2)
3+
4+
bool is0SubArray(int arr[], int n)
5+
{
6+
for(int i=0;i<n;i++)
7+
{
8+
int curr_sum = 0;
9+
for(int j=i; j<n;j++)
10+
{
11+
curr_sum+= arr[j];
12+
if(curr_sum ==0)
13+
return true;
14+
}
15+
}
16+
17+
return false;
18+
}
19+
20+
21+
//Efficient Solution Idea : Use Prefix Sum and Hashing
22+
//time complexity: O(n)
23+
bool is0SubArray(int arr[], int n)
24+
{
25+
unordered_set<int> h;
26+
27+
int pre_sum=0;
28+
for(int i=0;i<n;i++)
29+
{
30+
pre_sum+=arr[i];
31+
if(h.find(pre_sum)!= h.end())
32+
return true;
33+
34+
if(pre_sum == 0)
35+
return true;
36+
37+
h.insert(pre_sum);
38+
39+
}
40+
41+
return false;
42+
}
43+
44+
45+
46+
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//Naive Solution :
2+
3+
bool isSum(int arr[], int n, int sum)
4+
{
5+
unordered_set<int> s;
6+
7+
int pre_sum=0;
8+
for(int i=0; i<n;i++)
9+
{
10+
if(pre_sum == sum)
11+
{
12+
return true;
13+
}
14+
15+
if(s.find(pre_sum - sum)!=s.end())
16+
return true;
17+
18+
s.insert(pre_sum);
19+
}
20+
21+
return false;
22+
}
23+
//Time Complexity: O(n)
24+
//Aux Space: O(n)

0 commit comments

Comments
 (0)