Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #273

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Data Structure/HashTable/Intersection of two arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<bits/stdc++.h>
using namespace std;

class Solution{
public:
int NumberofElementsInIntersection (int a[], int b[], int n, int m )
{
unordered_set<int> s;
for(int i=0; i<n; i++){
s.insert(a[i]);
}
int count = 0;
for(int i=0; i<m; i++){
if(s.find(b[i])!=s.end()){
count++;
s.erase(b[i]);
}
}
return count;
}
};

int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int a[n], b[m];
for(int i=0; i<n; i++)
cin>>a[i];

for(int i=0; i<m; i++)
cin>>b[i];
Solution ob;
cout << ob.NumberofElementsInIntersection(a,b,n,m) << endl;
}
return 0;
}
52 changes: 52 additions & 0 deletions Data Structure/HashTable/Print Anagrams Together.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
vector<vector<string> > Anagrams(vector<string>& string_list) ;

vector<vector<string> > Anagrams(vector<string>& str)
{
// Your Code Here
int n = str.size();

unordered_map<string, vector<string>>m;

for(int i=0; i<n; i++){
string temp = str[i];
sort(str[i].begin(), str[i].end());

m[str[i]].push_back(temp);

}
vector<vector<string>> ans;
for(auto itr: m){
ans.push_back(itr.second);
}
return ans;
}


int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<string> string_list(n);
for (int i = 0; i < n; ++i)
cin>>string_list[i];
vector<vector<string> > result = Anagrams(string_list);
sort(result.begin(),result.end());
for (int i = 0; i < result.size(); i++)
{
for(int j=0; j < result[i].size(); j++)
{
cout<<result[i][j]<<" ";
}
cout<<"\n";
}
}

return 0;
}
80 changes: 80 additions & 0 deletions Data Structure/HashTable/Rat in a maze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <bits/stdc++.h>
using namespace std;


class Solution{

void Solve(int i, int j, vector<vector<int>> &m, string out, vector<string> &ans, int n, vector<vector<int>> &visited){
if(i==n-1 && j == n-1){
ans.push_back(out);
return;
}

if(i+1<n && !visited[i+1][j] && m[i+1][j]==1){
visited[i][j]=1;
out.push_back('D');
Solve(i+1, j, m, out, ans, n, visited);
out.pop_back();
visited[i][j]=0;
}
if(j-1>=0 && !visited[i][j-1] && m[i][j-1]==1){
visited[i][j]=1;
out.push_back('L');
Solve(i, j-1, m, out, ans, n, visited);
out.pop_back();
visited[i][j]=0;
}
if(j+1<n && !visited[i][j+1] && m[i][j+1]==1){
visited[i][j]=1;
out.push_back('R');
Solve(i, j+1, m, out, ans, n, visited);
out.pop_back();
visited[i][j]=0;
}
if(i-1>=0 && !visited[i-1][j] && m[i-1][j]==1){
visited[i][j]=1;
out.push_back('U');
Solve(i-1, j, m, out, ans, n, visited);
out.pop_back();
visited[i][j]=0;
}
}

public:
vector<string> findPath(vector<vector<int>> &m, int n) {
// Your code goes here
string out = "";
vector<string> ans;

vector<vector<int >> v(n, vector<int>(n, 0));

if(m[0][0]==1){
Solve(0, 0, m, out, ans, n, v);
}
return ans;
}
};


int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<vector<int>> m(n, vector<int> (n,0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> m[i][j];
}
}
Solution obj;
vector<string> result = obj.findPath(m, n);
if (result.size() == 0)
cout << -1;
else
for (int i = 0; i < result.size(); i++) cout << result[i] << " ";
cout << endl;
}
return 0;
} // } Driver Code Ends
42 changes: 42 additions & 0 deletions Data Structure/HashTable/Top K Frequent Elements in Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
vector<int> topK(vector<int>& nums, int k) {
unordered_map<int, int>m;
for(int i=0; i<nums.size(); i++){
m[nums[i]]++;
}
priority_queue<pair<int, int> >pq;
for(auto itr:m){
pq.push(make_pair(itr.second, itr.first));
}
vector<int> ans(k);
for(int i=0; i<k; i++){
auto x = pq.top();
pq.pop();
ans[i] = x.second;
}

return ans;
}
};

int main() {
int tc;
cin >> tc;
while (tc--) {
int n;
cin >> n;
vector<int> nums(n);
for (auto &i : nums) cin >> i;
int k;
cin >> k;
Solution obj;
vector<int> ans = obj.topK(nums, k);
for (auto i : ans) cout << i << " ";
cout << "\n";
}
return 0;
}
78 changes: 78 additions & 0 deletions Data Structure/HashTable/Triplet Sum in Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <bits/stdc++.h>
using namespace std;

class Solution{
public:
//Function to find if there exists a triplet in the
//array A[] which sums up to X.
bool find3Numbers(int nums[], int n, int X)
{
//Your Code Here
sort(nums, nums+n);

int ans = false; //stores the valid triplets

//loop for 'a' value
for(int i=0; i<n-2; i++){
//now increase a untill got a different number cz wanted unique triplets
if(i==0 || (i>0 && nums[i]!=nums[i-1])){
//applying only when now a is different element or it is our first element

//NOW 2 POINTER APPROACH
int low = i+1;
int high = n-1;
int sum = X-nums[i]; //target sum

//loop for 2 pointer approach
while(low<high){
if(nums[low]+nums[high] == sum){
/*if target sum acheived by duplet tehn push that duplet along
with a in ans i.e push that triplet in ans vector*/

ans = true;

//now inc. and dec. low and high simultaneously
low++;
high--;

//now inc. low until got diff element
while(low<high && nums[low]==nums[low-1]){
low++;
}
//dec high untill got diff element
while(low<high && nums[high]==nums[high+1]){
high--;
}

}
//if target sum not foung=d then inc. or dec. low and high accordingly
else if(nums[low]+nums[high] < sum){
low++;
}
else{
high--;
}
}
}
}
return ans;
}

};


int main()
{
int T;
cin>>T;
while(T--)
{
int n,X;
cin>>n>>X;
int i,A[n];
for(i=0;i<n;i++)
cin>>A[i];
Solution ob;
cout << ob.find3Numbers(A, n, X) << endl;
}
}