DS Lab Assign
DS Lab Assign
Lab File
2023 - 2024
Week 1 Lab B
Q1.
#include<bits/stdc++.h>
int main(){
int n;
cout<<"Enter n: ";
cin>>n;
int sum=0;
for(int i=0;i<n;i++){
cin>>arr[i];
sum+=arr[i];
float avg=((float)sum)/n;
Q2.
#include<bits/stdc++.h>
int main(){
map<int,int>mp;
int n;
cout<<"Enter n: ";cin>>n;
int temp=0;
for(int i=0;i<n;i++){
cin>>temp;
mp[temp]++;
for(auto i:mp){
Q3.
#include<bits/stdc++.h>
int main(){
int n;
cout<<"Enter n: ";
cin>>n;
vector<int>v(n-1);
int temp;
cin>>temp;
for(auto &i:v){
cin>>i;
v.push_back(temp);
for(auto &i:v){
cout<<i<<" ";
Q4.
#include<bits/stdc++.h>
int main(){
int n;
cout<<"Enter n: ";
cin>>n;
if(n<2){
cout<<"Invalid";
vector<int>v(n);
for(auto &i:v){
cin>>i;
sort(v.begin(),v.end());
Week 2 Lab A
Q1.
(a)
Output –
Size of o1 : 4
Size of o2 : 16
(b)
Output –
Size of o1 :4
Size of o2 : 24
(c)
Output-
Size of o1 : 4
Size of o2 : 24
(d)
Output-
Size of o1 : 4
Size of o2 : 24
(e)
Size of o1 : 4
Size of o2 : 16
(f)
Size of o1 : 4
Size of o2 : 20
Q2.
(a) 4.5
(b) 5
(c) 44
(d) error: invalid conversion from 'int*' to 'int'
(e) 5
(f) 4
(g) 5
(h) 1391661613893824345
Q3.
#include<bits/stdc++.h>
int main(){
int n;
cout<<"Enter n:";
cin>>n;
a[i]=rand()%100;
for(int i=0;i<n;i++){
if(i%2==0)
cout<<i<<" ";
for(int i=0;i<n;i++){
if(i%2!=0)
cout<<i<<" ";
Week 2 Lab-B
1
#include<bits/stdc++.h>
class node{
public:
int val;
node* next;
node(int v){
val=v;
next = NULL;
};
int main(){
int v;
for(int i=0;i<10;i++){
cin>>v;
temp=temp->next;
head = head->next;
temp=head;
for(int i=0;i<10;i++){
cout<<temp->val<<" ";
temp=temp->next;
}
2
#include<bits/stdc++.h>
class node{
public:
int val;
node* next;
node(int v){
val=v;
next = NULL;
};
int main(){
int n;
cin>>n;
int v;
for(int i=0;i<n;i++){
cin>>v;
temp=temp->next;
head = head->next;
int maxi=INT_MIN,mini=INT_MAX;
temp=head;
for(int i=0;i<n;i++){
mini=min(mini,temp->val);
maxi=max(maxi,temp->val);
temp=temp->next;
}
3
#include<bits/stdc++.h>
class node{
public:
int val;
node* next;
node(int v){
val=v;
next = NULL;
};
int main(){
int n;
cin>>n;
int v;
for(int i=0;i<n;i++){
cin>>v;
if(head==NULL){
head=temp;
continue;
temp->next=head;
head =temp;
for(int i=0;i<n;i++){
cout<<temp->val<<" ";
temp=temp->next;
4
#include<bits/stdc++.h>
class node{
public:
int val;
node* next;
node(int v){
val=v;
next = NULL;
}
};
int main(){
int n;
cin>>n;
int v;
for(int i=0;i<n;i++){
cin>>v;
temp = temp->next;
head = head->next;
temp = head;
int x;
int y;
cout<<endl;
cin>>y;
cin>>x;
for(int i=1;i<x-1;i++){
temp=temp->next;
newNode->next=temp->next;
temp->next = newNode;
cout<<"Now the linkedList is -> ";
temp = head;
while(temp!=NULL){
cout<<temp->val<<" ";
temp = temp->next;
5
#include<bits/stdc++.h>
class node{
public:
int val;
node* next;
node(int v){
val=v;
next = NULL;
};
int main(){
int n;
cin>>n;
int v;
while(n!=0){
v = n%10;
n/=10;
if(head==NULL){
head=temp;
continue;
temp->next= head;
head = temp;
temp = head;
while(temp!=NULL){
temp = temp->next;
cout<<"NULL";
6
#include<bits/stdc++.h>
public:
char val;
node* next;
node(char v){
val=v;
next = NULL;
};
int main(){
string s;
cin>>s;
char v;
while(s.length()!=0){
v = s[s.length()-1];
s = s.substr(0,s.length()-1);
if(head==NULL){
head=temp;
continue;
temp->next= head;
head = temp;
temp = head;
while(temp!=NULL){
temp = temp->next;
cout<<"NULL";
temp = head;
temp = temp->next;
delete del;
while(temp!=NULL){
temp->next = temp->next->next;
delete del;
continue;
temp = temp->next;
temp = head1;
while(temp!=NULL){
temp = temp->next;
}
cout<<"NULL";
7
#include<bits/stdc++.h>
class node{
public:
char val;
node* next;
node(char v){
val=v;
next = NULL;
};
int j = -1;
for(int i=0;i<5;i++){
if(temp1->val == temp2->val){
if(j==-1){
j=i;
count++;
if(count==3){
break;
else{
j=-1;
count=0;
temp1 = head1;
if(count==3){
if(j==0){
head1 = head1->next->next->next;
else{
for(int i=0;i<j-1;i++){
temp1=temp1->next;
temp1->next=temp1->next->next->next->next;
temp1 = head1;
while(temp1!=NULL){
cout<<temp1->val<<" -> ";
temp1 = temp1->next;
cout<<"NULL";
int main(){
string s1;
cin>>s1;
char v;
while(s1.length()!=0){
v = s1[s1.length()-1];
s1 = s1.substr(0,s1.length()-1);
if(head1==NULL){
head1=temp1;
continue;
temp1->next= head1;
head1 = temp1;
string s2;
cin>>s2;
char v2;
while(s2.length()!=0){
v2 = s2[s2.length()-1];
s2 = s2.substr(0,s2.length()-1);
node* temp2 =new node(v2);
if(head2==NULL){
head2=temp2;
continue;
temp2->next= head2;
head2 = temp2;
remove(head1,head2);
8
#include<bits/stdc++.h>
struct node{
int data;
node* next;
node* prev;
node(int d){
data=d;
next=NULL;
prev=NULL;
};
int main(){
node* tail;
int n;
cin>>n;
int t;
for(int i=0;i<n;i++){
cin>>t;
if(head==NULL){
head = temp;
tail = head;
else{
tail->next=temp;
temp->prev=tail;
tail = tail->next;
}
}
node* tt = head;
while(tt){
cout<<tt->data<<"<->";
tt=tt->next;
cout<<"NULL"<<endl;
int p;
cin>>p;
int x;
cin>>x;
for(int i=1;i<p-1;i++){
newNode->next=temp->next;
temp->next->prev=newNode;
temp->next = newNode;
while(head){
cout<<head->data<<"<->";
head=head->next;
cout<<"NULL";
}
9
#include<bits/stdc++.h>
struct node{
int data;
node* next;
node* prev;
node(int d){
data=d;
next=NULL;
prev=NULL;
};
int main(){
node* tail;
int n;
cin>>n;
int t;
cin>>t;
if(head==NULL){
head = temp;
tail = head;
else{
tail->next=temp;
temp->prev=tail;
tail = tail->next;
node* tt = head;
while(tt){
cout<<tt->data<<"<->";
tt=tt->next;
cout<<"NULL"<<endl;
while(temp->next!=tail){
temp = temp->next;
};
temp->next=NULL;
delete tail;
tail = temp;
while(head){
cout<<head->data<<"<->";
head=head->next;
cout<<"NULL";
10
#include<bits/stdc++.h>
struct node{
int data;
node* next;
node* prev;
node(int d){
data=d;
next=NULL;
prev=NULL;
};
int main(){
node* tail;
int n;
cin>>n;
int t;
cout<<"Enter Node Values : ";
for(int i=0;i<n;i++){
cin>>t;
if(head==NULL){
head = temp;
tail = head;
else{
tail->next=temp;
temp->prev=tail;
tail = tail->next;
node* tt = head;
while(tt){
cout<<tt->data<<"<->";
tt=tt->next;
cout<<"NULL"<<endl;
int i=1;
while(temp1!=temp2){
int t = temp1->data;
temp1->data=temp2->data;
temp2->data=t;
cout<<temp->data<<"<->";
temp=temp->next;
cout<<"NULL"<<endl;
temp1=temp1->next;
if(temp1==temp2){break;}
temp2=temp2->prev;
Week 3 Lab A
1.
#include<bits/stdc++.h>
struct node{
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
if(rear==NULL){
rear=curr;
rear->next=curr;
return;
curr->next=rear->next;
rear->next=curr;
rear=curr;
return;
if(loc==0){
rear->next=rear->next->next;
delete toDel;
return;
node*temp=rear->next;
int trav=1;
while(trav<loc){
temp=temp->next;
trav++;
if(temp->next==rear){
temp->next=temp->next->next;
delete rear;
rear=temp;
return;
temp->next=temp->next->next;
return;
node*temp=rear->next;
do{
cout<<temp->data<<"->";
temp=temp->next;
}while(temp!=rear->next);
int main(){
node* rear=NULL;
int n;
cout<<"Enter n: ";
cin>>n;
int temp;
while(n--){
cin>>temp;
addNode(rear,temp);
printL(rear);
cout<<endl;
cin>>temp;
delet(rear,temp);
cout<<endl;
printL(rear);
2.
#include<bits/stdc++.h>
struct node{
int data;
node* next;
node(int val){
data=val;
next=NULL;
};
if(rear==NULL){
rear=curr;
rear->next=curr;
return;
}
curr->next=rear->next;
rear->next=curr;
rear=curr;
return;
if(!rear1 || !rear2){
return NULL;
node* head=rear1->next;
rear1->next=rear2->next;
rear2->next=head;
return rear2;
node*temp=rear->next;
do{
cout<<temp->data<<"->";
temp=temp->next;
}while(temp!=rear->next);
int main(){
node* rear1=NULL;
int n;
cin>>n;
int temp;
cin>>temp;
addNode(rear1,temp);
printL(rear1);
node* rear2=NULL;
cout<<endl;
cin>>n;
while(n--){
cin>>temp;
addNode(rear2,temp);
printL(rear2);
cout<<endl;
node* rear=concat(rear1,rear2);
printL(rear);
}
4.
#include<bits/stdc++.h>
struct node{
int data;
node* next;
node(int val){
data=val;
next=NULL;
};
node*curr=new node(val);
if(head==NULL){
head=curr;
return;
node* temp=head;
while(temp->next){
temp=temp->next;
temp->next=curr;
return;
node* temp=head;
while(temp){
cout<<temp->data<<"->";
temp=temp->next;
cout<<endl;
int main(){
node* head1=NULL;
int n;
cin>>n;
int x;
while(n--){
cin>>x;
addNode(head1,x);
node* head2=NULL;
cout<<endl;
cin>>n;
cout<<"Enter n2 nums: "<<endl;
while(n--){
cin>>x;
addNode(head2,x);
int num1=0,num2=0;
node* temp=head1;
while(temp){
num1=((num1*10)+temp->data);
temp=temp->next;
temp=head2;
while(temp){
num2=((num2*10)+temp->data);
temp=temp->next;
cout<<num1*num2;
}
5.
#include<bits/stdc++.h>
struct node{
int data;
node* next;
node(int d){
data=d;
next=NULL;
};
class List{
public:
node* head;
node* tail;
List(){
head=NULL;
tail=NULL;
if(!head){
head=curr;
tail=curr;
tail->next=curr;
tail=tail->next;
}
void display(){
node* temp=head;
cout<<endl;
while(temp){
cout<<temp->data;
temp=temp->next;
if(temp){
void rotateRight(){
node* temp=head;
while(temp->next!=tail){
temp=temp->next;
tail->next=head;
temp->next=NULL;
head=tail;
tail=temp;
};
int main(){
cout<<"Enter n: ";
int n;cin>>n;
int temp;
List l;
while(n--){
cin>>temp;
l.Addback(temp);
l.display();
l.rotateRight();
l.display();
6.
#include<bits/stdc++.h>
struct Node
int data;
Node *next;
};
return;
fast_ptr->next->next != head)
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
if(fast_ptr->next->next == head)
fast_ptr = fast_ptr->next;
head1_ref = head;
if(head->next != head)
head2_ref = slow_ptr->next;
fast_ptr->next = slow_ptr->next;
slow_ptr->next = head;
ptr1->data = data;
ptr1->next = head_ref;
if(head_ref != NULL)
{
while(temp->next != head_ref)
temp = temp->next;
temp->next = ptr1;
else
ptr1->next = ptr1;
head_ref = ptr1;
if(head != NULL)
printf("\n");
do {
temp = temp->next;
} while(temp != head);
int main()
int list_size, i;
push(head, 12);
push(head, 56);
push(head, 2);
push(head, 11);
printList(head);
printList(head1);
printList(head2);
return 0;
Week 6 A
1.
#include<bits/stdc++.h>
int main(){
int n;cin>>n;
vector<int>v;
int temp;
while(n--){
cin>>temp;
v.push_back(temp);
int x;cin>>x;
for(int i=0;i<v.size();i++){
if(v[i]==x){
cout<<"True"<<endl;
exit(0);
cout<<"False";
2.
#include<bits/stdc++.h>
int main(){
int n;cin>>n;
vector<int>v;
int temp;
while(n--){
cin>>temp;
v.push_back(temp);
sort(v.begin(),v.end());
int x;cin>>x;
int left=0;
int right=v.size()-1;
int mid;
while(left<=right){
mid=(left+right)/2;
if(v[mid]==x){
cout<<"True";
exit(0);
else if(v[mid]>x){
right=mid-1;
else{
left=mid+1;
cout<<"False";
}
3.
#include<bits/stdc++.h>
int n=v.size();
if(n<k){
return -1;
if(n==1){
return v[0];
vector<int>v1,v2,v3;
int ran=v[n/2];
for(int i=0;i<n;i++){
if(v[i]==ran){
v2.push_back(v[i]);
else if(v[i]>ran){
v3.push_back(v[i]);
else{
v1.push_back(v[i]);
}
}
if(v1.size()>=k){
return ms(v1,k);
else if((v1.size()+v2.size())>=k){
return ms(v2,k-v1.size());
else{
return ms(v3,k-v1.size()-v2.size());
int main(){
vector<int>v;
int n;cin>>n;
int temp;
while(n--){
cin>>temp;
v.push_back(temp);
int k;
cin>>k;
cout<<ms(v,k);
4.
#include<bits/stdc++.h>
int main(){
int n;cin>>n;
vector<int>v;
int temp;
while(n--){
cin>>temp;
v.push_back(temp);
sort(v.begin(),v.end());
int x;cin>>x;
int left=0;
int right=v.size()-1;
int mid;
while(left<=right){
mid=left+((x-v[left])*(right-left))/(v[right]-v[left]);
if(v[mid]==x){
cout<<"True";
exit(0);
else if(v[mid]>x){
right=mid-1;
else{
left=mid+1;
cout<<"False";
}
5.
#include<bits/stdc++.h>
int main(){
int n;cin>>n;
vector<string>v;
string temp;
while(n--){
cin>>temp;
v.push_back(temp);
string x;cin>>x;
int left=0;
int right=v.size()-1;
int mid;
sort(v.begin(),v.end());
while(left<=right){
mid=(left+right)/2;
if(v[mid]==x){
cout<<"True";
exit(0);
}
else if(v[mid]>x){
right=mid-1;
else{
left=mid+1;
cout<<"False";
Week 3 Lab-B
Stack Implementation:-
struct node{
int data;
node* next;
node(int d){
data=d;
next=NULL;
};
class Stack{
node* t;
int n=0;
public:
Stack(){
t=NULL;
n++;
if(!t){
t=curr;
return;
curr->next=t;
t = curr;
bool empty(){
return n==0;
void pop(){
if(!t){
cout<<"Stack UnderFlow";
exit(0);
n--;
t=t->next;
int top(){
if(!t){
cout<<"Stack UnderFlow";
exit(0);
}
return t->data;
int size(){
return n;
};
Queue Implementation:-
struct node{
int data;
node* next;
node(int d){
data=d;
next=NULL;
};
class Queue{
node* tail;
node* head;
int n=0;
public:
Queue(){
tail=NULL;
head=NULL;
n++;
if(!head){
head=curr;
tail=curr;
return;
tail->next=curr;
tail=tail->next;
bool empty(){
return n==0;
void pop(){
if(n==0){
cout<<"Stack UnderFlow";
exit(0);
n--;
head=head->next;
int front(){
if(n==0){
cout<<"Stack UnderFlow";
exit(0);
return head->data;
int size(){
return n;
};
1.
x=3 y=9 7 13 4 7
2.
#include<bits/stdc++.h>
for(int i=2;i*i<=x;i++){
if(x%i==0){
return false;
return true;
int main(){
Stack st;
int x;cin>>x;
for(int i=2;i<=x/2;i++){
st.push(i);
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
}
3.
#include<bits/stdc++.h>
int main(){
Stack st;
Stack st1;
Stack st2;
cout<<"Enter n: ";
int n;cin>>n;
int temp;
for(int i=0;i<n;i++){
cin>>temp;
st.push(temp);
for(int i=0;i<n/2;i++)
st1.push(st.top());
st.pop();
while(!st.empty()){
st2.push(st.top());
st.pop();
while(!st1.empty()){st.push(st1.top());st1.pop();}
while(!st2.empty()){st.push(st2.top());st2.pop();}
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
4.
#include<bits/stdc++.h>
int main(){
int n;cin>>n;
int x;cin>>x;
Stack st;
while(n!=0){
st.push(n%x);
n/=x;
while(!st.empty()){
cout<<st.top();
st.pop();
}
5.
#include<bits/stdc++.h>
int main(){
string s;cin>>s;
Stack st;
for(auto &i:s){
st.push(i);
else if(i==')'){
if(st.top()!='('){break;}
else{st.pop();}
else if(i=='}'){
if(st.top()!='{'){break;}
else{st.pop();}
else if(i==']'){
if(st.top()!='['){break;}
else{st.pop();}
if(!st.empty()){
cout<<"Wrong";
else{
cout<<"Right";
6.
#include<bits/stdc++.h>
int main(){
string s;getline(cin,s);
Queue q;
for(int i=0;i<s.length();i++){
q.push(s[i]);
string ans="";
while(!q.empty()){
if(ans.length()==0){
ans+=q.front();
q.pop();
else{
int cnt=1;
cnt++;
q.pop();
if(cnt!=1){
ans+=to_string(cnt);
else{
ans+=q.front();
q.pop();
cout<<ans;
8.
#include<bits/stdc++.h>
int main(){
Queue q;
Queue temp;
int n;cin>>n;
int t;
int cnt=0;
while(cnt<n){cin>>t;q.push(t);cnt++;}
int x;cin>>x;
x--;
cnt=0;
int g;
while(cnt<n){
temp.push(q.front());
q.pop();
cnt++;
if(cnt==x){
g=q.front();
q.pop();
cnt++;
q.push(g);
while(!temp.empty()){
q.push(temp.front());
temp.pop();
while(!q.empty()){
cout<<q.front()<<"";
q.pop();
9.
#include<bits/stdc++.h>
int main(){
Queue q;
Stack st;
string s;
cin>>s;
char x;
for(auto &i:s){
x=tolower(i);
q.push(x);
st.push(x);
}
while(!q.empty()){
if(q.front()!=st.top()){
return 0;
st.pop();q.pop();
cout<<"It is a Palindrome";
10.
#include<bits/stdc++.h>
int main(){
string s;
cin>>s;
int xl,xf,yf,yl;
bool fly=0,flx=0;
for(int i=0;i<s.length();i++){
if(s[i]=='X'){
if(!flx){
xf=i;
xl=i;
flx=1;
else{
xl=i;
if(s[i]=='Y'){
if(!fly){
yf=i;
yl=i;
fly=1;
else{
yl=i;
int sta=max(xf,yf);
int ed=min(yl,xl);
Stack st;
for(int i=sta+1;i<ed;i++){
st.push(s[i]);
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
}
Week 4 Lab-A
1.
#include<bits/stdc++.h>
struct node{
int val;
node* next;
node* prev;
node(){}
node(int x){
val = x;
next=NULL;
};
class Stack{
public:
node* top;
Stack(){
top=NULL;
if(top==NULL){
}
else{
top = temp;
void pop(){
top = top->next;
delete del;
int Top(){
return top->val;
bool isEmpty(){
if(top==NULL){return 1;}return 0;
};
if(st.isEmpty() || x>st.Top()){
st.push(x);
else{
int t = st.Top();
st.pop();
sortedInsert(st,x);
st.push(t);
int n = st.Top();
st.pop();
sortStack(st);
sortedInsert(st,n);
int main(){
Stack st;
int n;
cin>>n;
int temp;
while(n--){
cin>>temp;
st.push(temp);
sortStack(st);
while(!st.isEmpty()){
cout<<st.Top()<<" ";
st.pop();
}
2.
#include<bits/stdc++.h>
#include<vector>
int n =s.length();
for(int i=0;i<n/2;i++){
if(s[i]!=s[n-i-1]){
return false;
return true;
if(s.length()==idx){
ans.push_back(v);
return;
string temp;
for(int i=idx;i<s.length();i++){
temp+=s[i];
if(isPalindrome(temp)){
v.push_back(temp);
rec(s,v,i+1,ans);
v.pop_back();
}}}
int main(){
vector<vector<string> >ans;
string s;
cin>>s;
vector<string>v;
rec(s,v,0,ans);
for(int i=0;i<ans.size();i++){
for(int j=0;j<ans[i].size();j++){
cout<<ans[i][j]<<" ";
cout<<endl;
3.
#include<bits/stdc++.h>
struct node{
char val;
node* next;
node* prev;
node(){}
node(char x){
val = x;
next=NULL;
};
class Stack{
public:
node* top;
Stack(){
top=NULL;
if(top==NULL){
else{
top = temp;
void pop(){
top = top->next;
delete del;
char Top(){
return top->val;
bool isEmpty(){
if(top==NULL){return 1;}return 0;
};
int main(){
Stack st;
string s;
cin>>s;
for(int i=0;i<s.length();i++){
st.push(s[i]);
s="";
while(!st.isEmpty()){
s+=st.Top();
st.pop();
4.
#include <iostream>
if (n == 1) {
cout << "Move Disk " << n << " from " << Sour << " to " << Des << endl;
return;
cout << "Move Disk " << n << " from " << Sour << " to " << Des << endl;
TOH(n - 1, Aux, Sour, Des);
int main()
int n;
cin >> n;
return 0;
5.
#include<bits/stdc++.h>
int main(){
int n;
cin>>n;
while(n!=1){
cout<<n<<" ";
if(n%2==0){
n/=2;
}
else{
n*=3;
n++;
cout<<1;
Week 6 B
1.
#include<bits/stdc++.h>
int main(){
int n;
cin>>n;
vector<int>arr;
int temp;
int x = 0;
for(int i=0;i<n;i++){
cin>>temp;
arr.push_back(temp);
x=x^temp;
x=x^(i+1);
int a;
a=(x&~(x-1));
int xa=0;
int xb=0;
for(int i=0;i<arr.size();i++){
if((arr[i]&a)!=0){
xa=xa^arr[i];
else{
xb=xb^arr[i];
if ((i & a) != 0) {
xa=xa^i;
else {
xb=xb^i;
int c=0;
for(int i=0;i<arr.size();i++){
if(arr[i]==xa){
c++;
if(c==2){
cout<<"Missing = "<<xb<<endl;
cout<<"Repeating = "<<xa;
else{
cout<<"Missing = "<<xa<<endl;
cout<<"Repeating = "<<xb;
2.
#include<bits/stdc++.h>
int main(){
int n;
cin>>n;
vector<int>arr;
int temp;
for(int i=0;i<n;i++){
cin>>temp;
arr.push_back(temp);
int x;
bool flag=0;
cout<<"Enter X -> ";
cin>>x;
for(int i=0;i<n;i++){
if(arr[i]>=x){
flag = 1;
if(arr[i]==x){
else if(i>0){
else{
break;
if(!flag){
}
3.
#include<bits/stdc++.h>
int main(){
int x;
cin>>x;
int n;
cin>>n;
bool flag=0;
int temp;
unordered_set<int>s;
for(int i=0;i<n;i++){
cin>>temp;
s.insert(temp);
auto it = s.find(abs(x-temp));
if(it!=s.end()){
flag =1;
if(!flag){
4.
#include<bits/stdc++.h>
if(m==0){
return 0;
return n+rec(n,m-1);
int main(){
int n , m;
cin>>n>>m;
}
5.
#include<bits/stdc++.h>
if (n2 != 0)
else
return n1;
int main(){
int n , m;
cin>>n>>m;
6.
#include<bits/stdc++.h>
return 0;
int main(){
int n;
cin>>n;
Week 7 A
1.
#include<bits/stdc++.h>
vector<string>ans;
if(idx==s.length()){
ans.push_back(s);
return;
for(int i=idx;i<s.size();i++){
swap(s[i],s[idx]);
permu(s,idx+1);
swap(s[i],s[idx]);
int main(){
string s;
cin>>s;
permu(s,0);
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<" ";
2.
#include<bits/stdc++.h>
vector<int>temp;
int i = low;
int j = mid+1;
int c=0;
if(v[i]<=v[j]){
temp.push_back(v[i]);
i++;
else{
c+=(mid-i+1);
temp.push_back(v[j]);
j++;
while(i<=mid){
temp.push_back(v[i]);
i++;
while(j<=high){
temp.push_back(v[j]);
j++;
for(int k=low;k<=high;k++){
v[k]=temp[k-low];
return c;
}
int mergeSort(vector<int>&v,int low,int high){
if(low>=high){
return 0;
int c=0;
c+=mergeSort(v,low,mid);
c+=mergeSort(v,mid+1,high);
c+=merge(v,low,mid,high);
return c;
int main(){
int n;
cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++){
cin>>v[i];
cout<<mergeSort(v,0,n-1);
}
3.
#include<bits/stdc++.h>
int n=arr.size()-1;
int m = n/2;
vector<int>a1;
vector<int>a2;
vector<int>a3;
for(int i=0;i<=n;i++){
if(arr[i]<arr[m]){
a1.push_back(arr[i]);
else if(arr[i]>arr[m]){
a3.push_back(arr[i]);
else{
a2.push_back(arr[i]);
int x =a1.size()+a2.size();
if(a1.size()>=k){
return median(a1,k);
else if(x>=k){
return arr[m];
else{
return median(a3,k-x);
}
}
int main(){
int n;
cin>>n;
vector<int>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
int k;
cin>>k;
4.
#include<bits/stdc++.h>
int minSwaps(vector<int>&arr){
vector<int>temp(arr.size());
map< int,int >m;
for(int i=0;i<temp.size();i++){
m[arr[i]]=i;
temp[i]=arr[i];
sort(temp.begin(),temp.end());
int ans=0;
for(int i=0;i<arr.size();i++){
if(arr[i]!=temp[i]){
ans++;
swap(arr[i],arr[m[temp[i]]]);
m[temp[i]]=i;
m[arr[i]]=m[temp[i]];
return ans;
int main(){
int n;
cin>>n;
vector<int>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
Week 7 B
1.
#include<bits/stdc++.h>
struct node{
int data;
node* down;
node* next;
node(int v){
data=v;
down=NULL;
next=NULL;
};
if(!head || !head->next){
return;
node*start =head;
while(tail->next){
tail=tail->next;
while(start!=tail){
if(start->down){
tail->next=start->down;
while(tail->next){
tail=tail->next;
start=start->next;
return;
if(n==1){
return head;
for(int i=1;i<n;i++){
temp->next=new node(arr[i]);
temp=temp->next;
return head;
int main(){
int arr1[]={1,2,3};
int arr2[]={4,5};
int arr3[]={6};
int arr4[]={7};
int arr5[]={8};
int arr6[]={9};
node*head1=listf(arr1,3);
node*head2=listf(arr2,2);
node*head3=listf(arr3,1);
node*head4=listf(arr4,1);
node*head5=listf(arr5,1);
node*head6=listf(arr6,1);
head1->down=head2;
head1->next->next->down=head3;
head2->down=head4;
head2->next->down=head5;
head3->down=head6;
flatten(head1);
while(head1){
cout<<head1->data<<"->";
head1=head1->next;
2.
class Solution {
public:
int r1 = A.size();
int r2 = B.size();
int c1 = A[0].size();
int c2 = B[0].size();
int x = sparseA[i][j].first;
if(B[x][k] != 0){
return ret;
};
3.
#include<bits/stdc++.h>
using namespace std;
bool rat(int i,int j,int n,int m,int **arr,int **ans){
if(ans[i][j]){return false;}
if(i>=n || i<0 || j<0 || j>=m){
return 0;
}
if(i==n-1 && j==m-1){
if(arr[i][j]){
ans[i][j]=1;
return 1;
}
return 0;
}
if(arr[i][j]){
ans[i][j]=1;
if(rat(i,j+1,n,m,arr,ans)){
return 1;
}
if(rat(i+1,j,n,m,arr,ans)){
return 1;
}
if(rat(i,j-1,n,m,arr,ans)){
return 1;
}
if(rat(i-1,j,n,m,arr,ans)){
return 1;
}
ans[i][j]=0;
}
return 0;
}
int main(){
int n,m;cin>>n>>m;
int **arr=new int*[n];
int **ans=new int*[n];
for(int i=0;i<n;i++){
arr[i]=new int[m];
ans[i]=new int[m];
for(int j=0;j<m;j++){
cin>>arr[i][j];
ans[i][j]=0;
}
}
if(rat(0,0,n,m,arr,ans)){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
}
else{
cout<<"Not possible";
}}
4.
#include<bits/stdc++.h>
arr[y][j]=1;
if(nQueen(x,y+1,n,m,arr)){
return 1;
}
arr[y][j]=0;
}
}
return 0;
}
int main(){
int n,m;cin>>n>>m;
int **arr=new int*[n];
for(int i=0;i<n;i++){
arr[i]=new int[m];
for(int j=0;j<m;j++){
arr[i][j]=0;
}
}
if(nQueen(0,0,n,m,arr)){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
else{
cout<<"Not possible";
}
}
WEEK-9 A&B
ANS-1
#include<iostream>
#include<queue>
struct node{
int data;
node*down;
node*next;
};
node *head=NULL;
node *p;
int i;
for(i=0;i<n;++i)
if (head==NULL){
head=p=new node();
else
p = p->next;
p->data=arr[i];
p->next=p->down=NULL;
return head;
node *createList(void)
int arr6[]={2};
int arr7[]={16};
int arr8[]={3};
head1->down = head2;
head1->next->next->next->down= head3;
head3->down = head4;
head4->down = head5;
head2->next->down = head6;
head2->next->next->down= head7;
head7->down = head8;
return head1;
class MultiLinkedList{
public:
node*head;
MultiLinkedList(){
head=NULL;
void createTree(node*head1){
head=head1;
void Display(node*curr){
queue<node*>q;
q.push(curr);
while(!q.empty()){
curr=q.front();
while(curr!=NULL){
if(curr->down!=NULL){
q.push(curr->down);
cout<<curr->data<<"->";
curr=curr->next;
q.pop();
};
int main(){
node *head=NULL;
head=createList();
MultiLinkedList m;
m.createTree(head);
m.Display(m.head);
return 0;
ANS-2
#include <iostream>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
};
class BinaryTree {
private:
TreeNode* root;
if (node == NULL)
} else {
return node;
if (node == NULL)
return 0;
if (node == NULL)
return false;
if (val == node->data)
return true;
else
public:
BinaryTree() : root(NULL) {}
int getHeight() {
return getHeightRecursive(root);
}
};
int main() {
BinaryTree tree;
tree.insertRecursive(10);
tree.insertRecursive(5);
tree.insertRecursive(15);
tree.insertRecursive(3);
tree.insertRecursive(7);
cout << "Height of the tree: " << tree.getHeight() << endl;
cout << "Tree has duplicate values: " << (hasDupes ? "Yes" : "No") << endl;
return 0;
#include <iostream>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
};
class BinaryTree {
private:
TreeNode* root;
if (node == NULL)
} else {
return node;
{
if(root != NULL)
displayinorder(root->left);
cout<<root->data<<" ";
displayinorder(root->right);
if(root != NULL)
deleteinorder(root->left,val);
if(val==root->data)
delete root;
deleteinorder(root->right,val);
public:
BinaryTree() : root(NULL) {}
}
void display()
displayinorder(root);
deleteinorder(root,val);
};
int main() {
BinaryTree tree;
tree.insertRecursive(10);
tree.insertRecursive(5);
tree.insertRecursive(15);
tree.insertRecursive(3);
tree.insertRecursive(7);
tree.insertRecursive(16);
tree.insertRecursive(9);
tree.insertRecursive(1);
tree.insertRecursive(4);
tree.insertRecursive(6);
tree.insertRecursive(2);
tree.insertRecursive(8);
tree.insertRecursive(11);
tree.insertRecursive(17);
tree.insertRecursive(13);
tree.display();
tree.deletenode(1);
tree.display();
return 0;
ANS-3
abcd^e-fgh*+^*+i-
—------------------------------------------------------------------------------------
WEEK-10 A
ANS-1
1.
#include <iostream>
int hash_table[table_size];
void initializeHashTable() {
for (int i = 0; i < table_size; i++) {
hash_table[i] = -1;
return key % 9;
int i = 0;
i++;
hash_table[index] = key;
int main() {
initializeHashTable();
int keys[] = {14, 18, 24, 20, 3, 23, 33, 15};
int len=sizeof(keys)/sizeof(0);
insertKey(keys[i]);
std::cout << "Index " << i << ": " << hash_table[i] << std::endl;
return 0;
ANS-2
#include <iostream>
void initializeHashTable() {
hash_table[i] = -1;
return key % 7;
int i = 0;
i++;
if (i >= table_size) {
cout << "Hash table is full. Cannot insert " << key << endl;
return;
}
}
hash_table[index] = key;
int main() {
initializeHashTable();
int keys[] = {50, 700, 76, 85, 92, 73, 101, 70};
insertKey(keys[i]);
cout << "Index " << i << ": " << hash_table[i] << endl;
return 0;
}
ANS-3
#include <iostream>
#include <vector>
struct KeyValuePair {
int key;
int value;
KeyValuePair* next;
};
int main() {
int numBuckets;
char choice;
do {
if (!hashTable[hashKey]) {
hashTable[hashKey] = kvp;
} else {
kvp->next = hashTable[hashKey];
hashTable[hashKey] = kvp;
}
cout << "Do you want to enter more values? (y/n): ";
while (current) {
cout << "(" << current->key << ", " << current->value << ") ";
current = current->next;
while (current) {
delete current;
current = next;
return 0;
}
ANS-4
#include <iostream>
int hash_table[table_size];
void initializeHashTable() {
hash_table[i] = -1;
return key % 7;
}
return key % 3;
int i = 0;
i++;
hash_table[index] = key;
int main() {
initializeHashTable();
int keys[] = {50, 700, 76, 85, 92, 73, 101, 70};
insertKey(keys[i]);
cout << "Index " << i << ": " << hash_table[i] << endl;
return 0;
ANS-5
#include <iostream>
#include <vector>
int n = sortedArr.size();
int count = 0;
if (sortedArr[i] == max_height) {
count++;
int main() {
int arr[] = {5, 3, 2, 3, 6, 3, 3};
return 0;
WEEK-10 B
ANS-1
#include<iostream>
#include<queue>
class node{
public:
int data;
node*left;
node*right;
node(int value){
data=value;
left=NULL;
right=NULL;
};
class LevelTree{
public:
node*root;
LevelTree(){
root=NULL;
queue<node*>q;
int index=0;
root=new node(arr[index]);
index++;
q.push(root);
while(!q.empty()&&index<n){
node*curr=q.front();
q.pop();
if(arr[index]!=0){
curr->left=new node(arr[index]);
q.push(curr->left);
index++;
if(arr[index]!=0){
curr->right=new node(arr[index]);
q.push(curr->right);
index++;
void Preorder(node*curr){
if(curr==NULL){
return;
cout<<curr->data<<" ";
Preorder(curr->left);
Preorder(curr->right);
void Inorder(node*curr){
if(curr==NULL){
return;
Inorder(curr->left);
cout<<curr->data<<" ";
Inorder(curr->right);
void Postorder(node*curr){
if(curr==NULL){
return;
Postorder(curr->left);
Postorder(curr->right);
cout<<curr->data<<" ";
void levelorder(node*curr){
queue<node*>q;
q.push(curr);
while(!q.empty()){
node*temp=q.front();
q.pop();
cout<<temp->data<<" ";
if(temp->left){
q.push(temp->left);
if(temp->right){
q.push(temp->right);
};
int main(){
int arr[]={1,2,3,4,5,6,7,0,8,9,0,0,0,10,11,12,13,0,14,15,0,16,17};
int n=sizeof(arr)/sizeof(arr[0]);
LevelTree t;
t.createTree(arr,n);
cout<<"Pre-order:";
t.Preorder(t.root);
cout<<endl;
cout<<"In-order:";
t.Inorder(t.root);
cout<<endl;
cout<<"Post-order:";
t.Postorder(t.root);
cout<<endl;
cout<<"Level-order:";
t.levelorder(t.root);
}
ANS-2
#include<iostream>
#include<queue>
class node{
public:
int data;
node*left;
node*right;
node(int value){
data=value;
left=NULL;
right=NULL;
};
class LevelTree{
public:
node*root;
LevelTree(){
root=NULL;
queue<node*>q;
int index=0;
root=new node(arr[index]);
index++;
q.push(root);
while(!q.empty()&&index<n){
node*curr=q.front();
q.pop();
if(arr[index]!=0){
curr->left=new node(arr[index]);
q.push(curr->left);
index++;
if(arr[index]!=0){
curr->right=new node(arr[index]);
q.push(curr->right);
index++;
int findMaxPath(node*curr){
if(curr==NULL) {
return 0;
int leftSum=findMaxPath(curr->left);
int rightSum=findMaxPath(curr->right);
return max(leftSum,rightSum)+curr->data;
int maxPath(){
if (root==NULL){
return 0;
}
int result=findMaxPath(root);
return result;
};
int main(){
int arr[]={1,2,3,4,5,6,7,0,8,9,0,0,0,10,11,12,13,0,14,15,0,16,17};
int n=sizeof(arr)/sizeof(arr[0]);
LevelTree t;
t.createTree(arr,n);
int maxSum=t.maxPath();
ANS-3
#include <iostream>
#include <queue>
#include <stack>
class TreeNode {
public:
char data;
TreeNode* left;
TreeNode* right;
};
void levelOrderTraversal(TreeNode* root) {
if (!root) {
return;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
q.pop();
if (current->left) {
q.push(current->left);
if (current->right) {
q.push(current->right);
if (!root) {
return;
stack<TreeNode*> s1;
stack<TreeNode*> s2;
bool leftToRight = true;
s1.push(root);
while (!s1.empty()) {
s1.pop();
if (leftToRight) {
if (current->left) {
s2.push(current->left);
if (current->right) {
s2.push(current->right);
} else {
if (current->right) {
s2.push(current->right);
if (current->left) {
s2.push(current->left);
swap(s1, s2);
leftToRight = !leftToRight;
}
int height(TreeNode* root) {
if (!root) {
return 0;
if (!root) {
return;
if (level == 1) {
int h = height(root);
printGivenLevel(root, i);
}
}
if (!root) {
return;
if (level == 1) {
int h = height(root);
if (leftToRight) {
printGivenLevel(root, i);
} else {
printGivenLevelReverse(root, i);
leftToRight = !leftToRight;
int main() {
levelOrderTraversal(root);
spiralOrderTraversal(root);
recursiveLevelOrderTraversal(root);
recursiveSpiralOrderTraversal(root);
return 0;
}
ANS-4
#include <iostream>
class TreeNode {
public:
int data;
int count;
TreeNode* left;
TreeNode* right;
};
class ModifiedBST {
public:
TreeNode* root;
ModifiedBST() {
root = nullptr;
if (node == nullptr) {
return new TreeNode(key);
if (key == node->data) {
node->count++;
} else {
return node;
if (node == nullptr) {
return;
inOrderTraversal(node->left);
inOrderTraversal(node->right);
}
void displayInOrder() {
inOrderTraversal(root);
while (node->left) {
node = node->left;
return node;
if (node == nullptr) {
return node;
} else {
if (node->count > 1) {
node->count--;
} else {
if (node->left == nullptr) {
delete node;
return temp;
} else if (node->right == nullptr) {
delete node;
return temp;
node->data = temp->data;
node->count = temp->count;
return node;
};
int main() {
ModifiedBST bst;
int elements[] = {10, 20, 30, 40, 50, 40, 35, 25, 20, 40, 18, 19, 22, 27, 30, 27};
bst.insertElement(elements[i]);
}
bst.displayInOrder();
int elementToDelete;
bst.deleteElement(elementToDelete);
bst.displayInOrder();
return 0;
—----------------------------------------------------------------------------------------------------------------------------
WEEK-11 B
ANS-1
#include <iostream>
class Node {
public:
int data;
Color color;
Node* parent;
Node* left;
Node* right;
};
class RBTree {
private:
Node* root;
Node* TNULL;
if (node != TNULL) {
preOrderHelper(node->left);
preOrderHelper(node->right);
if (node != TNULL) {
inOrderHelper(node->left);
inOrderHelper(node->right);
return node;
}
void fixInsert(Node* k) {
Node* u;
if (k->parent == k->parent->parent->right) {
u = k->parent->parent->left;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->left) {
k = k->parent;
rightRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
leftRotate(k->parent->parent);
} else {
u = k->parent->parent->right;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->right) {
k = k->parent;
leftRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
rightRotate(k->parent->parent);
if (k == root) {
break;
root->color = BLACK;
if (u->parent == nullptr) {
root = v;
} else if (u == u->parent->left) {
u->parent->left = v;
} else {
u->parent->right = v;
v->parent = u->parent;
}
void fixDelete(Node* x) {
Node* s;
if (x == x->parent->left) {
s = x->parent->right;
if (s->color == RED) {
s->color = BLACK;
x->parent->color = RED;
leftRotate(x->parent);
s = x->parent->right;
s->color = RED;
x = x->parent;
} else {
if (s->right->color == BLACK) {
s->left->color = BLACK;
s->color = RED;
rightRotate(s);
s = x->parent->right;
s->color = x->parent->color;
x->parent->color = BLACK;
s->right->color = BLACK;
leftRotate(x->parent);
x = root;
} else {
s = x->parent->left;
if (s->color == RED) {
s->color = BLACK;
x->parent->color = RED;
rightRotate(x->parent);
s = x->parent->left;
s->color = RED;
x = x->parent;
} else {
if (s->left->color == BLACK) {
s->right->color = BLACK;
s->color = RED;
leftRotate(s);
s = x->parent->left;
s->color = x->parent->color;
x->parent->color = BLACK;
s->left->color = BLACK;
rightRotate(x->parent);
x = root;
x->color = BLACK;
Node* z = TNULL;
Node* x, y;
if (node->data == key) {
z = node;
node = node->right;
} else {
node = node->left;
if (z == TNULL) {
return;
y = z;
if (z->left == TNULL) {
x = z->right;
rbTransplant(z, z->right);
x = z->left;
rbTransplant(z, z->left);
} else {
y = minimum(z->right);
yOriginalColor = y->color;
x = y->right;
if (y->parent == z) {
x->parent = y;
} else {
rbTransplant(y, y->right);
y->right = z->right;
y->right->parent = y;
rbTransplant(z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
if (yOriginalColor == BLACK) {
fixDelete(x);
// Preorder
// Inorder
// Post order
// Search
// Insert
// Delete
// Find min
// Find max
public:
RBTree() {
TNULL->color = BLACK;
TNULL->left = nullptr;
TNULL->right = nullptr;
root = TNULL;
// Preorder
void preOrder() {
preOrderHelper
ANS-2
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Node {
public:
string data;
Color color;
Node* parent;
Node* left;
Node* right;
};
class SpellChecker {
private:
Node* root;
Node* TNULL;
void leftRotate(Node* x) {
Node* y = x->right;
x->right = y->left;
if (y->left != TNULL) {
y->left->parent = x;
y->parent = x->parent;
if (x->parent == nullptr) {
root = y;
} else if (x == x->parent->left) {
x->parent->left = y;
} else {
x->parent->right = y;
y->left = x;
x->parent = y;
void rightRotate(Node* x) {
Node* y = x->left;
x->left = y->right;
if (y->right != TNULL) {
y->right->parent = x;
y->parent = x->parent;
if (x->parent == nullptr) {
root = y;
} else if (x == x->parent->right) {
x->parent->right = y;
} else {
x->parent->left = y;
y->right = x;
x->parent = y;
void fixInsert(Node* k) {
Node* u;
if (k->parent == k->parent->parent->right) {
u = k->parent->parent->left;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->left) {
k = k->parent;
rightRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
leftRotate(k->parent->parent);
} else {
u = k->parent->parent->right;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->right) {
k = k->parent;
leftRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
rightRotate(k->parent->parent);
if (k == root) {
break;
root->color = BLACK;
return node;
public:
SpellChecker() {
TNULL->color = BLACK;
TNULL->left = nullptr;
TNULL->right = nullptr;
root = TNULL;
Node* y = nullptr;
Node* x = root;
while (x != TNULL) {
y = x;
x = x->left;
} else {
x = x->right;
node->parent = y;
if (y == nullptr) {
root = node;
y->left = node;
} else {
y->right = node;
}
if (node->parent == nullptr) {
node->color = BLACK;
return;
if (node->parent->parent == nullptr) {
return;
fixInsert(node);
vector<string> words;
if (isalpha(c)) {
word += tolower(c);
} else if (!word.empty()) {
if (!search(word)) {
word = "";
}
if (!word.empty() && !search(word)) {
void inOrder() {
inOrderHelper(root);
if (node != TNULL) {
inOrderHelper(node->left);
inOrderHelper(node->right);
};
int main() {
SpellChecker checker;
checker.insert("apple");
checker.insert("banana");
checker.insert("cherry");
checker.insert("date");
checker.insert("elderberry");
checker.tokenizeAndCheck(text);
checker.inOrder();
return 0;
ANS-3
#include <iostream>
class Node {
public:
int data;
Color color;
Node* parent;
Node* left;
Node* right;
};
class DynamicOrderStatistics {
private:
Node* root;
Node* TNULL;
void leftRotate(Node* x) {
Node* y = x->right;
x->right = y->left;
if (y->left != TNULL) {
y->left->parent = x;
y->parent = x->parent;
if (x->parent == nullptr) {
root = y;
} else if (x == x->parent->left) {
x->parent->left = y;
} else {
x->parent->right = y;
y->left = x;
x->parent = y;
y->size = x->size;
void rightRotate(Node* x) {
Node* y = x->left;
x->left = y->right;
if (y->right != TNULL) {
y->right->parent = x;
y->parent = x->parent;
if (x->parent == nullptr) {
root = y;
} else if (x == x->parent->right) {
x->parent->right = y;
} else {
x->parent->left = y;
y->right = x;
x->parent = y;
y->size = x->size;
void fixInsert(Node* k) {
Node* u;
if (k->parent == k->parent->parent->right) {
u = k->parent->parent->left;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->left) {
k = k->parent;
rightRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
leftRotate(k->parent->parent);
} else {
u = k->parent->parent->right;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->right) {
k = k->parent;
leftRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
rightRotate(k->parent->parent);
if (k == root) {
break;
root->color = BLACK;
if (u->parent == nullptr) {
root = v;
} else if (u == u->parent->left) {
u->parent->left = v;
} else {
u->parent->right = v;
}
v->parent = u->parent;
void fixDelete(Node* x) {
Node* s;
if (x == x->parent->left) {
s = x->parent->right;
if (s->color == RED) {
s->color = BLACK;
x->parent->color = RED;
leftRotate(x->parent);
s = x->parent->right;
s->color = RED;
x = x->parent;
} else {
if (s->right->color == BLACK) {
s->left->color = BLACK;
s->color = RED;
rightRotate(s);
s = x->parent->right;
s->color = x->parent->color;
x->parent->color = BLACK;
s->right->color = BLACK;
leftRotate(x->parent);
x = root;
}
} else {
s = x->parent->left;
if (s->color == RED) {
s->color = BLACK;
x->parent->color = RED;
rightRotate(x->parent);
s = x->parent->left;
s->color = RED;
x = x->parent;
} else {
if (s->left->color == BLACK) {
s->right->color = BLACK;
s->color = RED;
leftRotate(s);
s = x->parent->left;
s->color = x->parent->color;
x->parent->color = BLACK;
s->left->color = BLACK;
rightRotate(x->parent);
x = root;
x->color = BLACK;
Node* x, y;
if (node->data == key) {
z = node;
node = node->right;
} else {
node = node->left;
if (z == TNULL) {
return;
y = z;
if (z->left == TNULL) {
x = z->right;
rbTransplant(z, z->right);
x = z->left;
rbTransplant(z, z->left);
} else {
y = minimum(z->right);
yOriginalColor = y->color;
x = y->right;
if (y->parent == z) {
x->parent = y;
} else {
rbTransplant(y, y->right);
y->right = z->right;
y->right->parent = y;
rbTransplant(z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
if (yOriginalColor == BLACK) {
fixDelete(x);
node = node->left;
return node;
if (node == TNULL) {
return 0;
if (x == node->data) {
return node->left->size + 1;
}
if (x < node->data) {
public:
DynamicOrderStatistics() {
TNULL->color = BLACK;
TNULL->left = nullptr;
TNULL->right = nullptr;
root = TNULL;
void insert(int x) {
Node* y = nullptr;
y = xNode;
xNode->size++;
xNode = xNode->left;
} else {
xNode->size++;
xNode = xNode->right;
}
node->parent = y;
if (y == nullptr) {
root = node;
y->left = node;
} else {
y->right = node;
if (node->parent == nullptr) {
node->color = BLACK;
return;
if (node->parent->parent == nullptr) {
return;
fixInsert(node);
rbDeleteNodeHelper(root, key);
int select(int k) {
if (current->left->size + 1 == k) {
return current->data;
} else {
k -= current->left->size + 1;
current = current->right;
int rank(int x) {
void inOrder() {
inOrderHelper(root);
if (node != TNULL) {
inOrderHelper(node->left);
inOrderHelper(node->right);
};
int main() {
DynamicOrderStatistics tree;
tree.insert(20);
tree.insert(30);
tree.insert(40);
tree.insert(50);
tree.insert(60);
tree.insert(70);
tree.insert(80);
tree.insert(90);
tree.insert(100);
tree.insert(110);
tree.insert(120);
tree.insert(130);
cout << "Rank of 70: " << tree.rank(70) << endl; // Should print 6
tree.remove(130);
tree.remove(80);
tree.remove(60);
tree.remove(20);
tree.remove(100);
tree.remove(60);
tree.remove(110);
tree.remove(70);
tree.remove(30);
tree.remove(120);
cout << "Select 7: " << tree.select(7) << endl; // Should print 110
return 0;
WEEK-11 A
ANS-1
#include <iostream>
#include <stack>
#include <queue>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
};
if (root == NULL) {
} else {
}
return root;
if (root == NULL) {
return 0;
std::stack<TreeNode*> s;
s.push(current);
current = current->left;
current = s.top();
s.pop();
current = current->right;
}
void preorderTraversal(TreeNode* root) {
std::stack<TreeNode*> s;
s.push(root);
while (!s.empty()) {
s.pop();
if (current->right) s.push(current->right);
if (current->left) s.push(current->left);
s1.push(root);
while (!s1.empty()) {
s1.pop();
s2.push(current);
if (current->left) s1.push(current->left);
if (current->right) s1.push(current->right);
}
while (!s2.empty()) {
s2.pop();
std::queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
q.pop();
if (current->left) q.push(current->left);
if (current->right) q.push(current->right);
int main() {
int values[] = {10, 20, 30, 40, 50, 60, 70, 75, 80};
std::cout << "Height of the tree (recursive): " << getHeightRecursive(root) << std::endl;
inorderTraversal(root);
preorderTraversal(root);
postorderTraversal(root);
levelOrderTraversal(root);
return 0;
ANS-2
#include <iostream>
#include <vector>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
};
if (root == NULL) {
} else {
return root;
return 0;
return NULL;
return root;
int n = arr.size();
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main() {
int values[] = {10, 20, 30, 40, 50, 60, 70, 75, 80};
insertionSort(sortedValues);
std::cout << "Height of the balanced tree: " << height << std::endl;
return 0;
ANS-3
#include <iostream>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
int height;
};
return node->height;
TreeNode* rightRotate(TreeNode* y) {
TreeNode* x = y->left;
TreeNode* T2 = x->right;
x->right = y;
y->left = T2;
updateHeight(y);
updateHeight(x);
return x;
TreeNode* leftRotate(TreeNode* x) {
TreeNode* y = x->right;
TreeNode* T2 = y->left;
y->left = x;
x->right = T2;
updateHeight(x);
updateHeight(y);
return y;
if (balance > 1) {
if (getBalanceFactor(node->left) < 0) {
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
if (root == NULL) {
} else {
return root;
updateHeight(root);
return balanceNode(root);
inorderTraversal(root->left);
std::cout << root->data << " ";
inorderTraversal(root->right);
node = node->left;
return node;
} else {
if (temp == NULL) {
temp = root;
root = NULL;
} else {
*root = *temp;
delete temp;
} else {
if (root == NULL) {
return root;
updateHeight(root);
return balanceNode(root);
int main() {
int insertValues[] = {10, 20, 30, 40, 50, 45, 35, 25, 15, 5, 8, 18, 28, 38, 48};
inorderTraversal(root);
inorderTraversal(root);
return 0;
ANS-4
#include <iostream>
#include <cstdlib>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
};
}
bool isAVLTree(TreeNode* root) {
return true;
return false;
int main() {
if (isAVLTree(root)) {
std::cout << "The given BST is also an AVL Tree." << std::endl;
} else {
std::cout << "The given BST is not an AVL Tree." << std::endl;
return 0;
}
ANS-5
#include <iostream>
#include <vector>
#include <algorithm>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
};
class KaryTreeNode {
public:
int data;
std::vector<KaryTreeNode*> children;
};
if (root == NULL) {
return new TreeNode(value);
return root;
if (root == NULL) {
if (root->children.size() < k) {
} else {
root->children.push_back(newNode);
return root;
return NULL;
}
return root;
node = node->left;
return node;
preorderTraversal(root->left);
preorderTraversal(root->right);
deleteNode(root->left, value);
} else if (value > root->data) {
deleteNode(root->right, value);
} else {
if (root->left == NULL) {
root = root->right;
delete temp;
root = root->left;
delete temp;
} else {
root->data = temp->data;
deleteNode(root->right, temp->data);
int main() {
std::vector<int> sortedValues;
int values[] = {10, 20, 30, 40, 50, 45, 35, 25, 15, 5, 8, 18, 28, 38, 48};
int k = 3;
sortedValues.push_back(values[i]);
std::sort(sortedValues.begin(), sortedValues.end());
preorderTraversal(binaryRoot);
deleteNode(balancedRoot, 18);
deleteNode(balancedRoot, 50);
deleteNode(balancedRoot, 25);
deleteNode(balancedRoot, 30);
deleteNode(balancedRoot, 28);
preorderTraversal(balancedRoot);
return 0;
—----------------------------------------------------------------------------------------------------------------------------
WEEK-14 A
ANS-1
#include<iostream>
int *keys;
int t;
BTreeNode **C;
int n;
bool leaf;
public:
void traverse();
};
class BTree
BTreeNode *root;
int t;
public:
BTree(int _t)
void traverse()
{ if (root != NULL) root->traverse(); }
BTreeNode* search(int k)
};
t = t1;
leaf = leaf1;
n = 0;
void BTreeNode::traverse()
int i;
if (leaf == false)
C[i]->traverse();
}
if (leaf == false)
C[i]->traverse();
BTreeNode *BTreeNode::search(int k)
int i = 0;
i++;
if (keys[i] == k)
return this;
if (leaf == true)
return NULL;
return C[i]->search(k);
void BTree::insert(int k)
if (root == NULL)
root->keys[0] = k;
root->n = 1;
else
{
if (root->n == 2*t-1)
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k)
i++;
s->C[i]->insertNonFull(k);
root = s;
else
root->insertNonFull(k);
void BTreeNode::insertNonFull(int k)
int i = n-1;
if (leaf == true)
keys[i+1] = keys[i];
i--;
keys[i+1] = k;
n = n+1;
else
i--;
if (C[i+1]->n == 2*t-1)
splitChild(i+1, C[i+1]);
if (keys[i+1] < k)
i++;
C[i+1]->insertNonFull(k);
{
BTreeNode *z = new BTreeNode(y->t, y->leaf);
z->n = t - 1;
z->keys[j] = y->keys[j+t];
if (y->leaf == false)
z->C[j] = y->C[j+t];
y->n = t - 1;
C[j+1] = C[j];
C[i+1] = z;
keys[j+1] = keys[j];
keys[i] = y->keys[t-1];
n = n + 1;
int main()
BTree t(3);
t.insert(10);
t.insert(20);
t.insert(5);
t.insert(6);
t.insert(12);
t.insert(30);
t.insert(7);
t.insert(17);
t.traverse();
int k = 6;
k = 15;
return 0;
}
ANS-2
#include <iostream>
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
bool isThreaded;
};
class ThreadedBinaryTree {
public:
TreeNode* root;
ThreadedBinaryTree() : root(NULL) {}
void printThreadedTree();
};
if (node == NULL) {
return;
if (node->left == NULL) {
node->left = prev;
node->isThreaded = true;
prev->right = node;
prev->isThreaded = false;
prev = node;
createThreadedTree(node->right, prev);
if (node == NULL) {
return;
current = current->left;
if (current->isThreaded) {
current = current->right;
} else {
current = current->right;
current = current->left;
void ThreadedBinaryTree::printThreadedTree() {
inOrderTraversal(root);
int main() {
ThreadedBinaryTree tree;
tree.createThreadedTree(tree.root, prev);
tree.printThreadedTree();
return 0;
ANS-3
#include <iostream>
#include <vector>
struct BPlusTreeNode {
vector<int> keys;
vector<BPlusTreeNode*> children;
bool isLeaf;
BPlusTreeNode() : isLeaf(true) {}
};
class BPlusTree {
private:
BPlusTreeNode* root;
public:
BPlusTree() : root(nullptr) {}
if (root == nullptr) {
root->keys.push_back(key);
} else {
insertKey(root, key);
if (node->isLeaf) {
insertIntoLeaf(node, key);
} else {
int i = 0;
i++;
insertKey(node->children[i], key);
}
leaf->keys.push_back(key);
sort(leaf->keys.begin(), leaf->keys.end());
splitLeaf(leaf);
if (leaf->isLeaf) {
newLeaf->isLeaf = true;
leaf->isLeaf = true;
if (leaf->parent == nullptr) {
newRoot->keys.push_back(newLeaf->keys[0]);
newRoot->children.push_back(leaf);
newRoot->children.push_back(newLeaf);
leaf->parent = newRoot;
newLeaf->parent = newRoot;
root = newRoot;
} else {
newLeaf->parent = leaf->parent;
int index = 0;
index++;
return index;
void printTree() {
if (root != nullptr) {
printNode(root);
if (!node->isLeaf) {
printNode(node->children[i]);
}
}
};
int main() {
BPlusTree bPlusTree;
bPlusTree.insert(key);
bPlusTree.printTree();
return 0;
ANS-4
#include <bits/stdc++.h>
#include <vector>
struct BPlusTreeNode {
vector<int> keys;
vector<BPlusTreeNode*> children;
bool isLeaf;
BPlusTreeNode() : isLeaf(true) {}
};
class BPlusTree {
private:
BPlusTreeNode* root;
public:
BPlusTree() : root(nullptr) {}
if (root == nullptr) {
root->keys.push_back(key);
} else {
insertKey(root, key);
if (node->isLeaf) {
node->keys.push_back(key);
sort(node->keys.begin(), node->keys.end());
splitLeaf(node);
} else {
// ... (rest of the code for searching and printing as shown in previous examples)
};
int main() {
BPlusTree bPlusTree;
bPlusTree.insert(key);
if (bPlusTree.search(searchKey)) {
cout << "Key " << searchKey << " found in the B+ Tree." << endl;
} else {
cout << "Key " << searchKey << " not found in the B+ Tree." << endl;
return 0;
—----------------------------------------------------------------------------------------------------------------------------
WEEK-15 B & 16 A
ANS-1
#include <iostream>
#include <vector>
#include <algorithm>
if (graph[path[pos - 1]][v] == 0)
return false;
if (path[i] == v)
return false;
return true;
if (pos == V) {
if (graph[path[pos - 1]][path[0]] == 1)
return true;
else
return false;
path[pos] = v;
return true;
path[pos] = -1;
return false;
}
path[0] = 0;
return;
int main() {
vector<vector<int>> graph = {
{15, 7, 5, 1, 3, 0, 5, 14},
{4, 0, 8, 9, 6, 5, 0, 8},
{21, 6, 0, 3, 7, 14, 8, 0}
};
hamiltonianPath(graph);
return 0;
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
int V = graph.size();
dist[src] = 0;
pq.push({0, src});
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
pq.push({dist[v], v});
if (i != src) {
cout << "F to " << char('A' + i) << ": " << dist[i] << endl;
int main() {
vector<vector<int>> graph = {
{15, 7, 5, 1, 3, 0, 5, 14},
{4, 0, 8, 9, 6, 5, 0, 8},
{21, 6, 0, 3, 7, 14, 8, 0}
};
return 0;
}
ANS-2
#include<bits/stdc++.h>
class Edge {
public:
};
class Graph {
public:
int V, E;
Edge* edge;
};
graph->V = V;
graph->E = E;
graph->edge = new Edge[E];
return graph;
class subset {
public:
int parent;
int rank;
};
if (subsets[i].parent != i)
return subsets[i].parent;
subsets[xroot].parent = yroot;
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
Edge* a1 = (Edge*)a;
Edge* b1 = (Edge*)b;
int V = graph->V;
Edge result[V];
int e = 0;
int i = 0;
subsets[v].parent = v;
subsets[v].rank = 0;
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
return;
int main() {
int V = 4;
int E = 5;
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = 10;
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 6;
graph->edge[2].src = 0;
graph->edge[2].dest = 3;
graph->edge[2].weight = 5;
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 15;
graph->edge[4].src = 2;
graph->edge[4].dest = 3;
graph->edge[4].weight = 4;
KruskalMST(graph);
return 0;
#include<bits/stdc++.h>
using namespace std;
bool marked[MAX];
priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > >
Q;
int y;
Q.push(make_pair(0, x));
while(!Q.empty()) {
p = Q.top();
Q.pop();
x = p.second;
if(marked[x] == true)
continue;
minimumCost += p.first;
marked[x] = true;
y = adj[x][i].second;
if(marked[y] == false)
Q.push(adj[x][i]);
return minimumCost;
int main() {
x = graph[i][0];
y = graph[i][1];
weight = graph[i][2];
adj[x].push_back(make_pair(weight, y));
adj[y].push_back(make_pair(weight, x));
minimumCost = prim(1);
return 0;
ANS-3
#include<bits/stdc++.h>
class Graph {
public:
};
adj[v].push_back(w);
void Graph::DFS(int v) {
visited[v] = true;
list<int>::iterator i;
if (!visited[*i])
DFS(*i);
int main() {
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Depth First Traversal (starting from vertex 2): ";
g.DFS(2);
return 0;
}
#include<iostream>
#include <list>
class Graph {
public:
int V;
list<int> *adj;
Graph(int V);
};
Graph::Graph(int V) {
this->V = V;
adj[v].push_back(w);
void Graph::BFS(int s) {
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
list<int>::iterator i;
while(!queue.empty()) {
s = queue.front();
queue.pop_front();
if (!visited[*i]) {
queue.push_back(*i);
visited[*i] = true;
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Breadth First Traversal (starting from vertex 2): ";
g.BFS(2);
return 0;
}
ANS-4
#include <iostream>
#include <vector>
#include <climits>
const int V = 5;
return min_index;
cout << parent[i] << " - " << i << "\t" << graph[i][parent[i]] << "\n";
int parent[V];
int key[V];
bool mstSet[V];
for (int i = 0; i < V; ++i) {
key[0] = 0;
parent[0] = -1;
mstSet[u] = true;
printMST(parent, graph);
int main() {
vector<vector<int>> graph = {
{0, 3, 6, 2, 0},
{3, 0, 0, 4, 2},
{6, 0, 0, 4, 4},
{2, 4, 4, 0, 0},
{0, 2, 4, 0, 0}
};
primMST(graph);
return 0;
ANS-5
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <unordered_map>
class Graph {
public:
adjList[u].push_back({v, distance});
}
void BFS(char source, char destination) {
queue<char> q;
q.push(source);
visited[source] = true;
parent[source] = source;
while (!q.empty()) {
q.pop();
if (current == destination) {
cout << "Path found from " << source << " to " << destination << ": ";
current = parent[current];
return;
if (!visited[nextNode]) {
q.push(nextNode);
visited[nextNode] = true;
parent[nextNode] = current;
}
}
cout << "Path not found from " << source << " to " << destination << endl;
visited[current] = true;
if (!visited[nextNode]) {
DFSUtil(nextNode, visited);
cout << "DFS traversal starting from node " << source << ": ";
DFSUtil(source, visited);
};
int main(){
Graph g;
g.addEdge('S','A',3);
g.addEdge('A','D',9);
g.addEdge('A','E',8);
g.addEdge('S','B',6);
g.addEdge('B','F',12);
g.addEdge('B','G',14);
g.addEdge('S','C',5);
g.addEdge('C','H',7);
g.addEdge('H','I',5);
g.addEdge('H','J',6);
g.addEdge('I','K',1);
g.addEdge('I','L',10);
g.addEdge('I','M',2);
char source='S';
char destination='I';
g.BFS(source, destination);
g.DFS(source);
return 0;
ANS-6
#include<iostream>
#include <list>
public:
int V;
list<int> *adj;
Graph(int V);
};
Graph::Graph(int V) {
this->V = V;
void Graph::BFS(int s) {
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
list<int>::iterator i;
while(!queue.empty()) {
s = queue.front();
queue.pop_front();
queue.push_back(*i);
visited[*i] = true;
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Breadth First Traversal (starting from vertex 2): ";
g.BFS(2);
return 0;