GeeksForGeeks - POTD | GFG POTD Answer
Closed channel
1 218
Subscribers
No data24 hours
-97 days
-5730 days
Posts Archive
πGet βΉ250 Cashback Now !!!
1. Install The App https://g.navi.com/GcIXf
2. Register And Complete KYC.
3. Buy Digital Gold For βΉ2 Or Mutual Invest For βΉ10.
4. You Will Get Upto βΉ250 Rewards.
5. Refer Your Friends And Get βΉ100 Per Successful Referral.π
Download Navi β‘οΈ Earn Reward
You read it right π―
You can now earn an extra CASHBACK
on using the Navi App ππ¨β‘οΈπ°
How?π€·ββοΈ
β
Just install the app through the referral link below
β
Earn cashback of βΉ500 on taking Cash Loan
β
Earn reward of upto βΉ500 on doing an Investment
Don't miss out nowπ
ββοΈ
Download Now https://g.navi.com/GcIXf
class Solution{
public:
Node* ans;
int find(Node *root, int n1, int n2){
if(!root)return 0;
int x=find(root->left,n1,n2);
x+=find(root->right,n1,n2);
if(root->data==n1)x++;
if(root->data==n2)x++;
if(x==2 && ans==NULL)ans=root;
return x;
}
Node* LCA(Node *root, int n1, int n2)
{
ans=NULL;
find(root,n1,n2);
return ans;
}
};
class Solution
{
public:
//Heapify function to maintain heap property.
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if(l < n && arr[l] > arr[largest])
{
largest = l;
}
if(r < n && arr[r] > arr[largest])
{
largest = r;
}
if(largest != i)
{
swap(arr[i] , arr[largest]);
heapify(arr , n , largest);
}
}
void buildHeap(int arr[], int n)
{
for(int i = n/2 -1 ; i >= 0 ; i--)
{
heapify(arr , n , i);
}
}
public:
//Function to sort an array using Heap Sort.
void heapSort(int arr[], int n)
{
buildHeap(arr , n);
for(int i = n-1;i>=0;i--)
{
swap(arr[0] , arr[i]);
heapify(arr , i , 0);
}
}
};
int kthAncestor(Node *root, int k, int node)
{
map mp;
queue q;
q.push(root);
while(q.empty()==false)
{
int c=q.size();
for(int i=0;ileft)
{
mp[f->left->data]=f->data;
q.push(f->left);
}
if(f->right)
{
q.push(f->right);
mp[f->right->data]=f->data;
}
}
}
int i=0;
int c=node;
for(i=0;i
vector findSpiral(Node *root)
{
queue q;
q.push(root);
vector ans;
bool res = true;
while(!q.empty()){
vector v;
int n = q.size();
for(int i=0; idata);
if(curr->left){
q.push(curr->left);
}
if(curr->right){
q.push(curr->right);
}
}
if(res){
reverse(v.begin(), v.end());
}
res = !res;
for(auto it: v){
ans.push_back(it);
}
}
return ans;
}
class Solution
{
public:
//Function to return list containing elements of right view of binary tree.
vector rightView(Node *root)
{
queue> q;
q.push({root, 0});
vector v;
map mp;
while(!q.empty()){
auto it = q.front();
q.pop();
Node* temp = it.first;
int index = it.second;
if(mp.find(index) == mp.end()){
mp[index] = temp->data;
}
if(temp->right){
q.push({temp->right, index+1});
}
if(temp->left){
q.push({temp->left, index+1});
}
}
for(auto it: mp){
v.push_back(it.second);
}
return v;
}
};
class Solution
{
public:
//Function to sort a linked list of 0s, 1s and 2s.
Node* segregate(Node *head) {
int count0 = 0, count1 = 0, count2 = 0;
Node* curr = head;
while(curr){
if(curr -> data == 0)
count0++;
else if(curr -> data == 1)
count1++;
else
count2++;
curr = curr -> next;
}
curr = head;
while(count0--){
curr -> data = 0;
curr = curr -> next;
}
while(count1--){
curr -> data = 1;
curr = curr -> next;
}
while(count2--){
curr -> data = 2;
curr = curr -> next;
}
return head;
}
};
class Solution
{
public:
//Function to remove duplicates from unsorted linked list.
Node * removeDuplicates( Node *head)
{
Node* par = NULL;
Node* temp = head;
set st;
while(temp!=NULL){
if(st.find(temp->data)!=st.end()){
par->next = temp->next;
}
else{
st.insert(temp->data);
par = temp;
}
temp = temp->next;
}
return head;
}
};
class Solution
{
public:
struct node *reverse (struct node *head, int k)
{
struct node *ptr=head;
vector v;
while(ptr!=NULL){
v.push_back(ptr->data);
ptr=ptr->next;
}
int n=v.size();
for(int i=0; in) j=n;
std::reverse(v.begin()+i, v.begin()+j);
i=j;
}
ptr=head;
for(auto val: v){
ptr->data = val;
ptr=ptr->next;
}
return head;
}
};
class Solution
{
public:
//Function to find the first non-repeating character in a string.
char nonrepeatingCharacter(string S)
{
unordered_mapmp;
char nonRepeating;
for(int i=0;i
