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

added gfg cheatcode few dsa soln #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 61 additions & 0 deletions GFG CHEATCODE/01) hashing/01)intersection of two arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
int NumberofElementsInIntersection (int a[], int b[], int n, int m )
{
// Your code goes here
map<int,int>m;
int j=0;
int i=n;
for(j=0;j<m;j++)
{
a[i++]=b[j];
}
for(i=0;i<n+m;i++)
{
m[a[i]]++;

}
for(i=0;i<n+m;i++)
{
if(m[arr[i]]>=1)
{
count++;
}
}
map<int,int>ms;
map<int,int>mp;
int count=0;
int i;
for(i=0;i<n;i++)
{
ms[a[i]]++;
}
for(i=0;i<m;i++)
{
mp[b[i]]++;
}
if(n>m)
{
for(i=0;i<n;i++)
{
if(mp[a[i]]>=1)
{
count++;
mp[a[i]]=0;
mp[a[i]]=0;
}
}
}
else
{
for(i=0;i<m;i++)
{
if(ms[b[i]]>=1)
{
count++;
ms[b[i]]=0;
ms[b[i]]=0;
}
}

}
return count;
}
24 changes: 24 additions & 0 deletions GFG CHEATCODE/01) hashing/02)keypair.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
bool hasArrayTwoCandidates(int arr[], int n, int x) {
// code here
sort(arr,arr+n);
int i=0;
int j=n-1;
while(i<j)
{
if(arr[i]+arr[j]==x)
{
return 1;
}
else if(arr[i]+arr[j]<x)
{
i++;
}
else
{
j--;
}
}
return 0;

}

30 changes: 30 additions & 0 deletions GFG CHEATCODE/01) hashing/03)top k frequent number in array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
vector<int> topK(vector<int>& nums, int k) {
// Code here

vector<int>v;
int count=0;
int mx;
int res;
map<int,int>m;
int i;
for(i=0;i<nums.size();i++)
{
m[nums[i]]++;
}
priority_queue<pair<int,int>>q;
for(auto i=m.begin();i!=m.end();i++)
{
q.push({i->second,i->first});
}
while(k)
{
v.push_back(q.top().second);
q.pop();

k--;

}


return v;
}
20 changes: 20 additions & 0 deletions GFG CHEATCODE/02)Heap/01) merge_k_sorted_arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
vector<int> mergeKArrays(vector<vector<int>> arr, int K)
{
//code here
vector<int>v;
int i,j;
priority_queue<int,vector<int>,greater<int>>q;
for(i=0;i<K;i++)
{
for(j=0;j<K;j++)
{
q.push(arr[i][j]);
}
}
while(!q.empty())
{
v.push_back(q.top());
q.pop();
}
return v;
}
27 changes: 27 additions & 0 deletions GFG CHEATCODE/02)Heap/02) nearly_sorted_arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
vector <int> nearlySorted(int arr[], int num, int K){
// Your code here
vector<int>v;
int i;
priority_queue<int,vector<int>,greater<int>>q;

for(i=0;i<num;i++)
{
q.push(arr[i]);
if(q.size()>K)
{
v.push_back(q.top());
q.pop();
}
}
while(!q.empty())
{
v.push_back(q.top());
q.pop();
}
return v;





}
42 changes: 42 additions & 0 deletions GFG CHEATCODE/02)Heap/03) kth largest element in stream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
vector<int> kthLargest(int k, int arr[], int n) {
// code here
int i;
vector<int>res,v;
priority_queue<int,vector<int>,greater<int>>q;
if(k==1)
{
for(i=0;i<n;i++)
{
v.push_back(arr[i]);
}
return v;
}

for(i=0;i<n;i++)
{
if(q.size()<k)
{
q.push(arr[i]);

}
else
{
if(q.top()<arr[i])
{

q.pop();
q.push(arr[i]);

}
}
if(q.size()<k)
{
v.push_back(-1);
}
else
{
v.push_back(q.top());
}
}
return v;
}
29 changes: 29 additions & 0 deletions GFG CHEATCODE/02)Heap/04) minimum _cost_of_ropes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
long long minCost(long long arr[], long long n) {
// Your code here
priority_queue<long long,vector<long long>,greater<long long>> q;
long long i;
for(i=0;i<n;i++)
{
q.push(arr[i]);
}

long long sum=0;


while(q.size()>=2)
{
long long x=0;
for(i=0;i<2;i++)
{

x=x+q.top();
q.pop();

}
q.push(x);
sum=sum+x;
}
return sum;


}
40 changes: 40 additions & 0 deletions GFG CHEATCODE/02)Heap/05)adding_array_elements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
int minOperations(int arr[], int n, int k) {
// code here
priority_queue<int,vector<int>,greater<int>> q;
int i;
for(i=0;i<n;i++)
{
q.push(arr[i]);
}
int count=0;
int sum;
if(q.top()<0)
{
return 0;
}
int x=0;
while(!q.empty())
{
sum=0;

for(i=0;i<2;i++)
{
if(q.top()<k)
{
sum=sum+q.top();
x++;
q.pop();


}
else if(q.top()>=k)
{
return x-count;
}
}
q.push(sum);
count++;
}

return count;
}
20 changes: 20 additions & 0 deletions GFG CHEATCODE/03)Linkedlist/01)detection of loop in linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
int detectLoop(Node* head)
{
// code here

Node*p=head;
Node*q=head;

while(p&& q && q->next)
{

p=p->next;
q=q->next->next;
Node*ptr1=head;
if(p==q)
{
return 1;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
int intersectPoint(Node* head1, Node* head2)
{
// Your Code Here
Node*temp1=head1;
Node*temp2=head2;
int count1=0;
int count2=0;
int res;
if(head1==NULL ||head2==NULL)
{
return 0;
}
while(temp1!=NULL)
{
temp1=temp1->next;
count1++;
}
while(temp2!=NULL)
{
temp2=temp2->next;
count2++;
}
temp1=head1;
temp2=head2;
if(count1>count2)
{
res=count1-count2;
while(res)
{
temp1=temp1->next;
res--;
}
while(temp1!=temp2)
{
temp1=temp1->next;
temp2=temp2->next;

}
return temp1->data;
}
else
{
res=count2-count1;
while(res)
{
temp2=temp2->next;
res--;
}
while(temp1!=temp2)
{
temp2=temp2->next;
temp1=temp1->next;

}
return temp2->data;
}
return 0;
28 changes: 28 additions & 0 deletions GFG CHEATCODE/03)Linkedlist/03)rearrange_linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
void rearrangeEvenOdd(Node *head)
{
// Your Code here
Node*slow=head;
Node*fast=head->next;
Node*temp=fast;
while(1)
{
if(!slow || !fast ||!(fast->next))
{
slow->next=temp;
break;
}
slow->next=fast->next;
slow=fast->next;


if(slow->next==NULL)
{
fast->next=NULL;
slow->next=temp;
break;
}
fast->next=slow->next;
fast=slow->next;
}

}
Loading