diff --git a/GFG CHEATCODE/01) hashing/01)intersection of two arrays.cpp b/GFG CHEATCODE/01) hashing/01)intersection of two arrays.cpp new file mode 100644 index 0000000..89eede1 --- /dev/null +++ b/GFG CHEATCODE/01) hashing/01)intersection of two arrays.cpp @@ -0,0 +1,61 @@ +int NumberofElementsInIntersection (int a[], int b[], int n, int m ) + { + // Your code goes here + mapm; + int j=0; + int i=n; + for(j=0;j=1) + { + count++; + } + } + mapms; + mapmp; + int count=0; + int i; + for(i=0;im) + { + for(i=0;i=1) + { + count++; + mp[a[i]]=0; + mp[a[i]]=0; + } + } + } + else + { + for(i=0;i=1) + { + count++; + ms[b[i]]=0; + ms[b[i]]=0; + } + } + + } + return count; + } \ No newline at end of file diff --git a/GFG CHEATCODE/01) hashing/02)keypair.cpp b/GFG CHEATCODE/01) hashing/02)keypair.cpp new file mode 100644 index 0000000..746e379 --- /dev/null +++ b/GFG CHEATCODE/01) hashing/02)keypair.cpp @@ -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 topK(vector& nums, int k) { + // Code here + + vectorv; + int count=0; + int mx; + int res; + mapm; + int i; + for(i=0;i>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; + } \ No newline at end of file diff --git a/GFG CHEATCODE/02)Heap/01) merge_k_sorted_arrays.cpp b/GFG CHEATCODE/02)Heap/01) merge_k_sorted_arrays.cpp new file mode 100644 index 0000000..3b8ea47 --- /dev/null +++ b/GFG CHEATCODE/02)Heap/01) merge_k_sorted_arrays.cpp @@ -0,0 +1,20 @@ + vector mergeKArrays(vector> arr, int K) + { + //code here + vectorv; + int i,j; + priority_queue,greater>q; + for(i=0;i nearlySorted(int arr[], int num, int K){ + // Your code here + vectorv; + int i; + priority_queue,greater>q; + + for(i=0;iK) + { + v.push_back(q.top()); + q.pop(); + } + } + while(!q.empty()) + { + v.push_back(q.top()); + q.pop(); + } + return v; + + + + + + } \ No newline at end of file diff --git a/GFG CHEATCODE/02)Heap/03) kth largest element in stream.cpp b/GFG CHEATCODE/02)Heap/03) kth largest element in stream.cpp new file mode 100644 index 0000000..d32453d --- /dev/null +++ b/GFG CHEATCODE/02)Heap/03) kth largest element in stream.cpp @@ -0,0 +1,42 @@ +vector kthLargest(int k, int arr[], int n) { + // code here + int i; + vectorres,v; + priority_queue,greater>q; + if(k==1) + { + for(i=0;i,greater> q; + long long i; + for(i=0;i=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; + + + } \ No newline at end of file diff --git a/GFG CHEATCODE/02)Heap/05)adding_array_elements.cpp b/GFG CHEATCODE/02)Heap/05)adding_array_elements.cpp new file mode 100644 index 0000000..ac21f0b --- /dev/null +++ b/GFG CHEATCODE/02)Heap/05)adding_array_elements.cpp @@ -0,0 +1,40 @@ +int minOperations(int arr[], int n, int k) { + // code here + priority_queue,greater> q; + int i; + for(i=0;i=k) + { + return x-count; + } + } + q.push(sum); + count++; + } + + return count; + } \ No newline at end of file diff --git a/GFG CHEATCODE/03)Linkedlist/01)detection of loop in linkedlist.cpp b/GFG CHEATCODE/03)Linkedlist/01)detection of loop in linkedlist.cpp new file mode 100644 index 0000000..e2edc39 --- /dev/null +++ b/GFG CHEATCODE/03)Linkedlist/01)detection of loop in linkedlist.cpp @@ -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; + } + + } + } \ No newline at end of file diff --git a/GFG CHEATCODE/03)Linkedlist/02)intersection_point_in_linkedlist.cpp b/GFG CHEATCODE/03)Linkedlist/02)intersection_point_in_linkedlist.cpp new file mode 100644 index 0000000..12be447 --- /dev/null +++ b/GFG CHEATCODE/03)Linkedlist/02)intersection_point_in_linkedlist.cpp @@ -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; \ No newline at end of file diff --git a/GFG CHEATCODE/03)Linkedlist/03)rearrange_linkedlist.cpp b/GFG CHEATCODE/03)Linkedlist/03)rearrange_linkedlist.cpp new file mode 100644 index 0000000..38d9b0b --- /dev/null +++ b/GFG CHEATCODE/03)Linkedlist/03)rearrange_linkedlist.cpp @@ -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; + } + + } \ No newline at end of file diff --git a/GFG CHEATCODE/03)Linkedlist/04)rotate_linked_list.cpp b/GFG CHEATCODE/03)Linkedlist/04)rotate_linked_list.cpp new file mode 100644 index 0000000..831b427 --- /dev/null +++ b/GFG CHEATCODE/03)Linkedlist/04)rotate_linked_list.cpp @@ -0,0 +1,30 @@ +Node* rotate(Node* head, int k) + { + // Your code here + Node* temp=head; + int count=1; + + + if(k==0) + { + return head; + } + + + while(countnext; + count++; + } + Node*res=temp; + + + while(temp->next!=NULL) + { + temp=temp->next; + } + temp->next=head; + head=res->next; + res->next=NULL; + return head; + } \ No newline at end of file diff --git a/GFG CHEATCODE/04)queue/01)First negative integer in every window of size k .cpp b/GFG CHEATCODE/04)queue/01)First negative integer in every window of size k .cpp new file mode 100644 index 0000000..930bfca --- /dev/null +++ b/GFG CHEATCODE/04)queue/01)First negative integer in every window of size k .cpp @@ -0,0 +1,47 @@ +vector printFirstNegativeInteger(long long int A[], + long long int N, long long int K) { + + + vectorv; + queueq; + long long i=0; + long long j=0; + while(jq; + string b; + int count[26]={0}; + for(j=0;j1) + { + q.pop(); + } + else + { + b.push_back(q.front()); + break; + } + } + if(q.empty()) + { + b.push_back('#'); + } + } + return b; + } \ No newline at end of file diff --git a/GFG CHEATCODE/04)queue/03)maximum diamonds.cpp b/GFG CHEATCODE/04)queue/03)maximum diamonds.cpp new file mode 100644 index 0000000..7364d3b --- /dev/null +++ b/GFG CHEATCODE/04)queue/03)maximum diamonds.cpp @@ -0,0 +1,35 @@ +int maxDiamonds(int A[], int N, int K) { + // code here + int count=0; + int i; + int sum=0; + priority_queueq; + + + + for(i=0;i max_of_subarrays(int *arr, int n, int k) + { + // your code here + int i=0; + int j=0; + listq; + vectorv; + int mx=INT_MIN; + while(j0 && q.back()st; + st.push(-1); + int i; + string str; + int mx=0; + for(i=0;ix) + { + student++; + sum=arr[i]; + } + if(student>k) + { + return false; + } + } + + + return true; + + } + + + int findPages(int arr[], int n, int m) + { + //code here + int i; + int mx=INT_MIN; + int sum=0; + int res=-1; + if(m>n) + { + return res; + } + for(i=0;i0 && a[mid]>a[mid-1] && a[mid]>a[mid+1] || mida[mid-1] && a[mid]>a[mid+1]) + { + return a[mid]; + } + + if(a[mid] a, long long n, long long m){ + //code + sort(a.begin(),a.end()); + long long i=0; + long long j=m-1; + long long mn=INT_MAX; + long long res; + while(jres) + { + mn=res; + } + i++; + j++; + } + + + + return mn; + } \ No newline at end of file diff --git a/GFG CHEATCODE/05)searching and sorting/04)collecting wood binary search in forest.cpp b/GFG CHEATCODE/05)searching and sorting/04)collecting wood binary search in forest.cpp new file mode 100644 index 0000000..80405de --- /dev/null +++ b/GFG CHEATCODE/05)searching and sorting/04)collecting wood binary search in forest.cpp @@ -0,0 +1,38 @@ +int find_height(int tree[], int n, int k) + { + // code here + sort(tree,tree+n); + int left=0; + int right=tree[n-1]; + + + int i,mid=0; + while(left<=right) + { + mid=left+((right-left)/2); + int sum=0; + for(i=n-1;i>=0;i--) + { + + if(tree[i]k) + { + left=mid+1; + } + else if(sum indexes(vector a, long long x) + { + // code here + int left=0; + int high=a.size()-1; + long long firstoc=-1,last=-1; + long long mid; + pairp; + while(left<=high) + { + mid=left+(high-left)/2; + if(a[mid]==x) + { + firstoc=mid; + high=mid-1; + + } + else if(a[mid]>x) + { + high=mid-1; + } + else if(a[mid]x) + { + high=mid-1; + } + else if(a[mid]=0;j--) + { + R[j]=max(arr[j],R[j+1]); + } + i=0,j=0; + while(i vec, int K) { + //code here + + int res,mid; + int l,h,result; + int i; + int n=vec.size(); + l=0; + h=n-1; + while(l<=h) + { + res=l+(h-l)/2; + + if(vec[res]<=vec[(res+1)%n] && vec[res]<=vec[(res+n-1)%n]) + { + result=res; + break; + } + else if(vec[res]>vec[n-1]) + { + l=res+1; + } + else if(vec[res]A1,int x) + { + int mid; + while(l<=h) + { + mid=l+(h-l)/2; + if((A1[mid]==x) &&(mid==0 ||A1[mid-1]x) + { + + h=mid-1; + } + else if(A1[mid] sortA1ByA2(vector A1, int N, vector A2, int M) + { + //Your code here + vectorv; + int i,l,j,h,mid,res,ress; + bool present[N]; + sort(A1.begin(),A1.end()); + for(i=0;i0) + { + a[i++]=0; + countz--; + } + while(counto>0) + { + a[i++]=1; + counto--; + } + while(countt>0) + { + a[i++]=2; + countt--; + } + + } \ No newline at end of file diff --git a/GFG CHEATCODE/05)searching and sorting/10)square root of number.cpp b/GFG CHEATCODE/05)searching and sorting/10)square root of number.cpp new file mode 100644 index 0000000..f7ae9ac --- /dev/null +++ b/GFG CHEATCODE/05)searching and sorting/10)square root of number.cpp @@ -0,0 +1,35 @@ +long long int floorSqrt(long long int x) + { + // Your code goes here + + int i,l,h,mid; + + l=1; + h=x; + long long res; + if(x==0||x==1) + { + return x; + } + while(l<=h) + { + mid=l+(h-l)/2; + if(mid*mid==x) + { + return mid; + } + if(mid<=x/mid) + { + + res=mid; + l=mid+1; + } + + else + { + h=mid-1; + } + + } + return res; + } \ No newline at end of file diff --git a/GFG CHEATCODE/05)searching and sorting/11)wave_Array.cpp b/GFG CHEATCODE/05)searching and sorting/11)wave_Array.cpp new file mode 100644 index 0000000..84f67ac --- /dev/null +++ b/GFG CHEATCODE/05)searching and sorting/11)wave_Array.cpp @@ -0,0 +1,9 @@ + void convertToWave(vector& arr, int n){ + + // Your code here + int i; + for(i=0;i bottomView(Node *root) { + // Your Code Here + + + + + vectorp; + map>m; + int hd=0; + queue>q; + q.push({root,hd}); + if(root!=NULL) + { + + while(!q.empty()) + { + pairtemp= q.front(); + q.pop(); + hd=temp.second; + Node* node =temp.first; + m[hd].push_back(node->data); + if(node->left!=NULL) + { + q.push({node->left,hd-1}); + } + if(node->right!=NULL) + { + q.push({node->right,hd+1}); + } + + + } + vectort; + + for(auto it=m.begin();it!=m.end();it++) + { + t.clear(); + + for(int i=0;isecond.size();i++) + { + t.push_back(it->second[i]); + } + + p.push_back(t[t.size()-1]); + } + + + } + + return p; + } diff --git a/GFG CHEATCODE/06)Tree/02)check balanced binary tree.cpp b/GFG CHEATCODE/06)Tree/02)check balanced binary tree.cpp new file mode 100644 index 0000000..68a45ce --- /dev/null +++ b/GFG CHEATCODE/06)Tree/02)check balanced binary tree.cpp @@ -0,0 +1,48 @@ +int height(struct Node* node){ + // code here + int h; + if(node==NULL) + { + return 0; + } + + int left= height(node->left); + int right=height(node->right); + + if(left>right) + { + h=left+1; + } + else if(right>=left) + { + h=right+1; + } + return h; + } + +//Function to check whether a binary tree is balanced or not. +bool isBalanced(Node *root) +{ + // Your Code here + if(root==NULL) + { + return 1; + } + int l,r,h1,h2; + + l=height(root->left); + r=height(root->right); + + + if(abs(l-r)<=1 && isBalanced(root->left)&& isBalanced(root->right)) + { + return 1; + } + + + return 0; + + + + +} diff --git a/GFG CHEATCODE/06)Tree/03)diagonal traversal of binary tree.cpp b/GFG CHEATCODE/06)Tree/03)diagonal traversal of binary tree.cpp new file mode 100644 index 0000000..d7d37c8 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/03)diagonal traversal of binary tree.cpp @@ -0,0 +1,42 @@ +vector diagonal(Node *root) +{ + // your code here + queueq; + q.push(root); + q.push(NULL); + vectorv; + Node* p=root; + while(!q.empty()) + { + p=q.front(); + q.pop(); + if(p==NULL) + { + + q.push(NULL); + + p=q.front(); + q.pop(); + if(p==NULL) + { + break; + } + } + while(p!=NULL) + { + v.push_back(p->data); + if(p->left) + { + q.push(p->left); + + } + + p=p->right; + } + + } + return v; + + + +} diff --git a/GFG CHEATCODE/06)Tree/04)Diameter of binary tree.cpp b/GFG CHEATCODE/06)Tree/04)Diameter of binary tree.cpp new file mode 100644 index 0000000..3b956a7 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/04)Diameter of binary tree.cpp @@ -0,0 +1,38 @@ + int height(Node* node){ + // code here + int h; + if(node==NULL) + { + return 0; + } + + int left= height(node->left); + int right=height(node->right); + + if(left>right) + { + h=left+1; + } + else if(right>=left) + { + h=right+1; + } + return h; + } + int diameter(Node* root) { + // Your code here + if(root==NULL) + { + return 0; + } + + + int lheight= height(root->left); + int rheight=height(root->right); + int ldiameter= diameter(root->left); + int rdiameter=diameter(root->right); + + int res= max(lheight+rheight+1, max(ldiameter,rdiameter)); + return res; + + } diff --git a/GFG CHEATCODE/06)Tree/05)find a pair with given target in BST.cpp b/GFG CHEATCODE/06)Tree/05)find a pair with given target in BST.cpp new file mode 100644 index 0000000..10119e4 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/05)find a pair with given target in BST.cpp @@ -0,0 +1,54 @@ + ques-find the nodes whose sum is equal to given target + algorithm=step1: level order traversal + step2: two pointer algorithm + + + + int isPairPresent(struct Node *root, int target) + { + queueq; + if(root!=NULL) + { + q.push(root); + } + Node*temp; + + int sum; + vectorv; + while(!q.empty()) + { + temp=q.front(); + v.push_back(temp->data); + q.pop(); + if(temp->left!=NULL) + { + q.push(temp->left); + } + if(temp->right!=NULL) + { + q.push(temp->right); + } + } + sort(v.begin(),v.end()); + int i=0; + int j=v.size()-1; + + while(itarget) + { + j--; + } + else if(v[i]+v[j]==target) + { + return 1; + } + } + + return 0; + } diff --git a/GFG CHEATCODE/06)Tree/06)height of binary tree.cpp b/GFG CHEATCODE/06)Tree/06)height of binary tree.cpp new file mode 100644 index 0000000..dff577e --- /dev/null +++ b/GFG CHEATCODE/06)Tree/06)height of binary tree.cpp @@ -0,0 +1,21 @@ +int height(struct Node* node){ + // code here + int h; + if(node==NULL) + { + return 0; + } + + int left= height(node->left); + int right=height(node->right); + + if(left>right) + { + h=left+1; + } + else if(right>=left) + { + h=right+1; + } + return h; + } diff --git a/GFG CHEATCODE/06)Tree/07)K_distance_from_root.cpp b/GFG CHEATCODE/06)Tree/07)K_distance_from_root.cpp new file mode 100644 index 0000000..f3fbc88 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/07)K_distance_from_root.cpp @@ -0,0 +1,48 @@ +vector Kdistance(struct Node *root, int k) +{ + // Your code here + + queueq; + q.push(root); + int depth=0; + vectorv; + int i; + if(k==0) + { + v.push_back(root->data); + return v; + } + while(!q.empty()) + { + + int size=q.size(); + + for(i=0;ileft) + { + q.push(temp->left); + } + if(temp->right) + { + q.push(temp->right); + } + + + + } + depth++; + if(depth==k) + { + while(!q.empty()) + { + v.push_back(q.front()->data); + q.pop(); + } + return v; + } + } + return v; + diff --git a/GFG CHEATCODE/06)Tree/08)left view of binary tree.cpp b/GFG CHEATCODE/06)Tree/08)left view of binary tree.cpp new file mode 100644 index 0000000..34642ad --- /dev/null +++ b/GFG CHEATCODE/06)Tree/08)left view of binary tree.cpp @@ -0,0 +1,42 @@ +vector leftView(Node *root) +{ + // Your code here + vectorv1; + vectorv; + Node* temp; + int i; + queueq; + if(root!=NULL) + { + q.push(root); + while(!q.empty()) + { + int size=q.size(); + v.clear(); + for(i=0;idata); + if(temp->left!=NULL) + { + q.push(temp->left); + } + if(temp->right!=NULL) + { + q.push(temp->right); + + } + q.pop(); + } + v1.push_back(v[0]); + + + + + } + + } + return v1; + +} diff --git a/GFG CHEATCODE/06)Tree/09)Maximum depth of tree.cpp b/GFG CHEATCODE/06)Tree/09)Maximum depth of tree.cpp new file mode 100644 index 0000000..683d8d1 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/09)Maximum depth of tree.cpp @@ -0,0 +1,32 @@ + int maxDepth(TreeNode* root) { + queueq; + + int depth=0; + int i; + q.push(root); + if(root!=NULL) + { + while(!q.empty()) + { + int size=q.size(); + + for(i=0;ileft) + { + q.push(temp->left); + } + if(temp->right) + { + q.push(temp->right); + } + } + depth++; + + } + } + return depth; + + } diff --git a/GFG CHEATCODE/06)Tree/11)Min depth of tree.cpp b/GFG CHEATCODE/06)Tree/11)Min depth of tree.cpp new file mode 100644 index 0000000..857ef84 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/11)Min depth of tree.cpp @@ -0,0 +1,40 @@ + int minDepth(Node *root) { + // Your code here + queueq; + + int depth=0; + int i; + q.push(root); + if(root!=NULL) + { + while(!q.empty()) + { + int size=q.size(); + + for(i=0;ileft) + { + q.push(temp->left); + } + if(temp->right) + { + q.push(temp->right); + } + if(temp->right==NULL && temp->left==NULL) + { + return depth+1; + } + } + depth++; + + } + } + + + + + } + diff --git a/GFG CHEATCODE/06)Tree/12)Mirror tree.cpp b/GFG CHEATCODE/06)Tree/12)Mirror tree.cpp new file mode 100644 index 0000000..b6135f6 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/12)Mirror tree.cpp @@ -0,0 +1,16 @@ + void mirror(Node* node) { + // code here + if(node==NULL) + { + return; + } + swap(node->left, node->right); + mirror(node->left); + mirror(node->right); + + + + + + + } diff --git a/GFG CHEATCODE/06)Tree/13)right side view of binary tree.cpp b/GFG CHEATCODE/06)Tree/13)right side view of binary tree.cpp new file mode 100644 index 0000000..a41b1bc --- /dev/null +++ b/GFG CHEATCODE/06)Tree/13)right side view of binary tree.cpp @@ -0,0 +1,42 @@ + vector rightSideView(TreeNode* root) { + + + vectorv1; + vectorv; + TreeNode* temp; + int i; + queueq; + if(root!=NULL) + { + q.push(root); + while(!q.empty()) + { + int size=q.size(); + v.clear(); + for(i=0;ival); + if(temp->left!=NULL) + { + q.push(temp->left); + } + if(temp->right!=NULL) + { + q.push(temp->right); + + } + q.pop(); + } + v1.push_back(v[size-1]); + + + + + } + + } + return v1; + + } diff --git a/GFG CHEATCODE/06)Tree/14)same tree.cpp b/GFG CHEATCODE/06)Tree/14)same tree.cpp new file mode 100644 index 0000000..abbe25d --- /dev/null +++ b/GFG CHEATCODE/06)Tree/14)same tree.cpp @@ -0,0 +1,21 @@ + bool isSameTree(TreeNode* p, TreeNode* q) { + + if(p==NULL && q==NULL) + { + return 1; + } + if(p==NULL||q==NULL) + { + return 0; + } + + if(p!=NULL && q!=NULL) + { + if((p->val== q->val)&&(isSameTree(p->left,q->left) )&& (isSameTree(p->right,q->right))) + { + return 1; + } + } + return 0; + + } diff --git a/GFG CHEATCODE/06)Tree/15)Subtree of Another Tree.cpp b/GFG CHEATCODE/06)Tree/15)Subtree of Another Tree.cpp new file mode 100644 index 0000000..f1b3486 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/15)Subtree of Another Tree.cpp @@ -0,0 +1,41 @@ + bool identical(TreeNode*root,TreeNode*subRoot) + { + if(root==NULL&&subRoot==NULL) + { + return 1; + } + + if(root!=NULL && subRoot!=NULL) + { + if((root->val==subRoot->val) && (identical(root->left,subRoot->left))&&(identical(root->right,subRoot->right))) + { + return 1; + } + } + return 0; + } + bool isSubtree(TreeNode* root, TreeNode* subRoot) { + + if(root==NULL) + { + return 0; + } + if(subRoot==NULL) + { + return 1; + } + + if(identical(root,subRoot)) + { + return 1; + } + if(root!=NULL && subRoot!=NULL) + { + if((isSubtree(root->left,subRoot))||(isSubtree(root->right,subRoot))) + { + return 1; + } + } + return 0; + + } diff --git a/GFG CHEATCODE/06)Tree/16)Sum tree.cpp b/GFG CHEATCODE/06)Tree/16)Sum tree.cpp new file mode 100644 index 0000000..2e8a40d --- /dev/null +++ b/GFG CHEATCODE/06)Tree/16)Sum tree.cpp @@ -0,0 +1,47 @@ + int add(Node*root) + { + if(root==NULL) + { + return 0; + } + int sum=root->data+add(root->left)+add(root->right); + return sum; + + + + } + bool isSumTree(Node* root) + { + // Your code here + + + if(root==NULL) + { + return 1; + } + if(root->left==NULL && root->right==NULL) + { + return 1; + } + int leftsum= add(root->left); + + int rightsum=add(root->right); + + int total=leftsum+rightsum; + + if(total==root->data) + { + + + if((isSumTree(root->left))&& (isSumTree(root->right))) + { + return 1; + } + + + } + return 0; + + + } + diff --git a/GFG CHEATCODE/06)Tree/17)top view of binary tree.cpp b/GFG CHEATCODE/06)Tree/17)top view of binary tree.cpp new file mode 100644 index 0000000..e13a16e --- /dev/null +++ b/GFG CHEATCODE/06)Tree/17)top view of binary tree.cpp @@ -0,0 +1,49 @@ +vector topView(Node *root) + { + //Your code here + vectorp; + map>m; + int hd=0; + queue>q; + q.push({root,hd}); + if(root!=NULL) + { + + while(!q.empty()) + { + pairtemp= q.front(); + q.pop(); + hd=temp.second; + Node* node =temp.first; + m[hd].push_back(node->data); + if(node->left!=NULL) + { + q.push({node->left,hd-1}); + } + if(node->right!=NULL) + { + q.push({node->right,hd+1}); + } + + + } + vectort; + + for(auto it=m.begin();it!=m.end();it++) + { + t.clear(); + + for(int i=0;isecond.size();i++) + { + t.push_back(it->second[i]); + } + + p.push_back(t[0]); + } + + + } + + return p; + } + diff --git a/GFG CHEATCODE/06)Tree/18)Vertical order traversal.cpp b/GFG CHEATCODE/06)Tree/18)Vertical order traversal.cpp new file mode 100644 index 0000000..38dc06e --- /dev/null +++ b/GFG CHEATCODE/06)Tree/18)Vertical order traversal.cpp @@ -0,0 +1,46 @@ + vector> verticalTraversal(TreeNode* root) { + + vector>p; + map>m; + int hd=0; + queue>q; + q.push({root,hd}); + if(root!=NULL) + { + + while(!q.empty()) + { + pairtemp= q.front(); + q.pop(); + hd=temp.second; + TreeNode* node =temp.first; + m[hd].push_back(node->val); + if(node->left!=NULL) + { + q.push({node->left,hd-1}); + } + if(node->right!=NULL) + { + q.push({node->right,hd+1}); + } + + + } + vectort; + + for(auto it=m.begin();it!=m.end();it++) + { + t.clear(); + + for(int i=0;isecond.size();i++) + { + t.push_back(it->second[i]); + } + sort(t.begin(),t.end()); + p.push_back(t); + } + + + } + return p; + } diff --git a/GFG CHEATCODE/06)Tree/19)zig-zag traversal.cpp b/GFG CHEATCODE/06)Tree/19)zig-zag traversal.cpp new file mode 100644 index 0000000..43d5cd6 --- /dev/null +++ b/GFG CHEATCODE/06)Tree/19)zig-zag traversal.cpp @@ -0,0 +1,43 @@ +vector zigZagTraversal(Node* root) +{ + // Code here + stacks1; + stacks2; + s1.push(root); + vectorv; + while(!s1.empty()||!s2.empty()) + { + while(!s1.empty()) + { + Node*temp1=s1.top(); + v.push_back(temp1->data); + s1.pop(); + if(temp1->left!=NULL) + { + s2.push(temp1->left); + } + if(temp1->right) + { + s2.push(temp1->right); + } + + + } + while(!s2.empty()) + { + Node* temp2=s2.top(); + v.push_back(temp2->data); + s2.pop(); + if(temp2->right) + { + s1.push(temp2->right); + } + if(temp2->left) + { + s1.push(temp2->left); + } + } + } + return v; + +} diff --git a/Heap/01) merge_k_sorted_arrays.cpp b/Heap/01) merge_k_sorted_arrays.cpp new file mode 100644 index 0000000..3b8ea47 --- /dev/null +++ b/Heap/01) merge_k_sorted_arrays.cpp @@ -0,0 +1,20 @@ + vector mergeKArrays(vector> arr, int K) + { + //code here + vectorv; + int i,j; + priority_queue,greater>q; + for(i=0;i nearlySorted(int arr[], int num, int K){ + // Your code here + vectorv; + int i; + priority_queue,greater>q; + + for(i=0;iK) + { + v.push_back(q.top()); + q.pop(); + } + } + while(!q.empty()) + { + v.push_back(q.top()); + q.pop(); + } + return v; + + + + + + } \ No newline at end of file diff --git a/Heap/03) kth largest element in stream.cpp b/Heap/03) kth largest element in stream.cpp new file mode 100644 index 0000000..d32453d --- /dev/null +++ b/Heap/03) kth largest element in stream.cpp @@ -0,0 +1,42 @@ +vector kthLargest(int k, int arr[], int n) { + // code here + int i; + vectorres,v; + priority_queue,greater>q; + if(k==1) + { + for(i=0;i,greater> q; + long long i; + for(i=0;i=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; + + + } \ No newline at end of file diff --git a/Heap/05)adding_array_elements.cpp b/Heap/05)adding_array_elements.cpp new file mode 100644 index 0000000..ac21f0b --- /dev/null +++ b/Heap/05)adding_array_elements.cpp @@ -0,0 +1,40 @@ +int minOperations(int arr[], int n, int k) { + // code here + priority_queue,greater> q; + int i; + for(i=0;i=k) + { + return x-count; + } + } + q.push(sum); + count++; + } + + return count; + } \ No newline at end of file