GEEKS FOR GEEKS SOLUTIONS🫢
Open in Telegram
1 342
Subscribers
No data24 hours
-27 days
-2930 days
Posts Archive
Hello Guys!!
please check your mails(Inbox/Spam) if your name is present in this list.
class Solution
{
public:
vector v_v;
//Function to return list containing elements of right view of binary tree.
void Right_View(vector &v_v, int k, Node * root)
{
if(root == NULL)
{
return;
}
if(v_v.size() == k)
{
v_v.push_back(root->data);
}
Right_View(v_v, k + 1, root->right);
Right_View(v_v, k + 1, root->left);
}
vector rightView(Node *root)
{
// Your Code here
Right_View(v_v, 0, root);
return v_v;
}
};
⚡️Get 100% Free Scholarship ⚡️
Get Premium Courses Worth Lakhs For Free 📂
Get Free Bags🎒 / T-shirts 👕
Free Certification 📃
Eligibility: Anyone Can Join ✅
Fees: ZERO 😍
Last Date: 25th July 📥
Try your Luck Guys 👀
✅ Register Now:
https://www.codingninjas.com/studio/contests/scholarship-test-25th-to-26th-july-2023?utm_source=Growth-CS&utm_medium=RJ&utm_campaign=TechVineChannel_Hard_26July
A cash Giveaway will be organised for all those who register,
class Solution
{
public:
//Function to sort a linked list of 0s, 1s and 2s.
Node* segregate(Node *head) {
// Add code here
if(head == NULL || head ->next == NULL)
return head;
Node *temp = NULL;
Node *slow = head;
Node *fast = head;
// 2 pointer appraoach / turtle-hare Algorithm (Finding the middle element)
while(fast != NULL && fast -> next != NULL)
{
temp = slow;
slow = slow->next; //slow increment by 1
fast = fast ->next ->next; //fast incremented by 2
}
temp -> next = NULL; //end of first left half
Node* l1 = segregate(head); //left half recursive call
Node* l2 = segregate(slow); //right half recursive call
return mergelist(l1, l2); //mergelist Function call
}
//MergeSort Function O(n*logn)
Node* mergelist(Node *l1, Node *l2)
{
Node *ptr = new Node(0);
Node *curr = ptr;
while(l1 != NULL && l2 != NULL)
{
if(l1->data <= l2->data)
{
curr -> next = l1;
l1 = l1 -> next;
}
else
{
curr -> next = l2;
l2 = l2 -> next;
}
curr = curr ->next;
}
//for unqual length linked list
if(l1 != NULL)
{
curr -> next = l1;
l1 = l1->next;
}
if(l2 != NULL)
{
curr -> next = l2;
l2 = l2 ->next;
}
return ptr->next;
}
};
class Solution
{
public:
//Function to remove duplicates from unsorted linked list.
Node * removeDuplicates( Node *head)
{
if(head==NULL)
return NULL;
Node* curr=head;
unordered_map mp;
Node* anshead=NULL;
Node* anscurr=NULL;
while(curr!=NULL){
mp[curr->data]++;
if(mp[curr->data]<=1){
Node* newNode=new Node(curr->data);
if(anshead==NULL){
anshead=newNode;
anscurr=newNode;
}else{
anscurr->next=newNode;
anscurr=newNode;
}
}
curr=curr->next;
}
return anshead;
}
};
Are you a job Seeker? (data is collected to help out people who are actively looking for full time job in IT Sector)
class Solution{
public:
int solve(int i ,int j ,string s,string t,vector>&dp){
if(i<0 or j<0 )return 0;
if(dp[i][j]!=-1)return dp[i][j];
int match=0;
if(s[i]==t[j])match=1+solve(i-1,j-1,s,t,dp);
int nomatch=max(solve(i-1,j,s,t,dp),solve(i,j-1,s,t,dp));
return dp[i][j]=max(nomatch,match);
}
int longestPalinSubseq(string A) {
string t=A;
reverse(t.begin(),t.end());
// vector>dp(A.size()+1,(vector(A.size()+1,0)));
int n=A.size();
vectorprev(n+1,0),cur(n+1,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(A[i-1]==t[j-1])cur[j]=1+prev[j-1];
else cur[j]=max(prev[j],cur[j-1]);
}
prev=cur;
}
return prev[n];
}
};
class Solution {
public:
int LongestRepeatingSubsequence(string str){
int n = str.length();
int dp[n+1][n+1];
for (int i=0; i<=n; i++)
for (int j=0; j<=n; j++)
dp[i][j] = 0;
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
if (str[i-1] == str[j-1] && i != j)
dp[i][j] = 1 + dp[i-1][j-1];
else
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
return dp[n][n];
}
};
class Solution {
public:
string FirstNonRepeating(string A){
unordered_map mp;
queue q;
string ans="";
for(char c: A){
//1 increment the freq
mp[c]++;
if(mp[c]==1){
q.push(c);
}
//2 remove repeating element
while(!q.empty() && mp[q.front()]>1){
q.pop();
}
//3 store the element
if(q.empty()){
ans+='#';
}else{
ans+=q.front();
}
}
return ans;
}
};
