adsa file
adsa file
Ashutosh
2
Index
S.No Topic Page no.
int main(){
int n;
cout<<"Enter the size of the array : ";
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++) cin>>arr[i];
Outpu
t:
Code: #include<iostream>
#include<vector>
#include<climits>
int main(){
int n;
cout<<"Enter the size of the array : ";
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++) cin>>arr[i];
reverseArray(arr);
displayArray(arr);
}
5
Outpu
t:
int main() {
int n, k;
cout << "Enter the size of the array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the array elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
cout << "Enter the value of k: ";
cin >> k;
pair<int, int> p = KthSmallestAndLargest(arr, k);
cout << k << " smallest element is: " << p.first << endl;
cout << k << " largest element is: " << p.second << endl;
return 0;
}
Outpu
t:
Code: #include<iostream>
#include<vector>
using namespace std;
int main(){
int n, k;
cout << "Enter the size of the array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the array elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
7
sorting(arr);
display(arr);
return 0;
}
Outpu
t:
Q5 Write a function to move all zeroes in an array to the end while maintaining
the relative order of other elements
Code: #include<iostream>
#include<vector>
using namespace std;
int main(){
int n, k;
cout << "Enter the size of the array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the array elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
moveZerosToEnd(arr);
display(arr);
return 0;
}
8
Outpu
t:
Code: #include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int value){
data = value;
next = NULL;
}
};
class Linkedlist{
Node* head;
Node* tail;
public:
Linkedlist(){
head=NULL;
}
void display(){
Node* temp = head;
if(head==NULL) cout<<"List is empty";
while(temp){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
9
void reverseLL(){
Node* prev = NULL;
Node*temp = head;
while(temp){
Node* front = temp->next;
temp->next = prev;
prev = temp;
temp = front;
}
head = prev;
}
};
int main(){
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int n = sizeof(arr)/sizeof(arr[0]);
Linkedlist list1;
list1.insertArrValues(arr,n);
cout<<"Linkedlist : ";
list1.display();
list1.reverseLL();
cout<<"Linkedlist after reversing : ";
list1.display();
return 0;
}
Outpu
t:
Code: #include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int value){
data = value;
next = NULL;
}
};
class Linkedlist{
10
Node* head;
Node* tail;
public:
Linkedlist(){
head=NULL;
}
void display(){
Node* temp = head;
if(head==NULL) cout<<"List is empty";
while(temp){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int middleofLL(){
Node* slow = head;
Node* fast = head;
while(fast!=NULL && fast->next!=NULL){
slow=slow->next;
fast=fast->next->next;
}
return slow->data;
}
};
int main(){
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int n = sizeof(arr)/sizeof(arr[0]);
Linkedlist list1;
list1.insertArrValues(arr,n);
cout<<"Linkedlist : ";
list1.display();
11
return 0;
}
Outpu
t:
Code: #include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int value){
data = value;
next = NULL;
}
};
class Linkedlist{
Node* head;
Node* tail;
public:
Linkedlist(){
head=NULL;
}
bool detectCycle(){
Node* slow = head;
Node* fast = head;
while(slow && fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast) return true;
12
}
return false;
}
};
int main(){
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int n = sizeof(arr)/sizeof(arr[0]);
Linkedlist list1;
list1.insertArrValues(arr,n);
cout<<"Linked list created"<<endl;
return 0;
}
Outpu
t:
Code: #include<iostream>
#include<vector>
#include<stack>
int main(){
int n;
cout<<"Enter the size of the string : ";
cin>>n;
vector<char> s(n);
for(int i=0; i<n; i++) cin>>s[i];
13
return 0;
}
Outpu
t:
Code: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
int main() {
return 0;
}
Outpu
t:
#include <vector>
using namespace std;
vector<string> generateBinaryNumbers(int N) {
vector<string> result;
queue<string> q;
q.push("1");
result.push_back(current);
s
q.push(current + "0");
q.push(current + "1");
}
return result;
}
int main() {
int n;
cout << "Enter the value of N: ";
cin >> n;
return 0;
}
Outpu
t:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value){
val = value;
15
left = NULL;
right = NULL;
}
};
int main() {
return 0;
}
Outpu
t:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
val = value;
left = nullptr;
right = nullptr;
}
};
preorderTraversal(root->left);
preorderTraversal(root->right);
}
int main() {
return 0;
}
Outpu
t:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
val = value;
left = nullptr;
right = nullptr;
}
};
int main() {
17
return 0;
}
Outpu
t:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
val = value;
left = nullptr;
right = nullptr;
}
};
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* current = q.front();
q.pop();
cout << current->val << " ";
if (current->left != nullptr) q.push(current->left);
if (current->right != nullptr) q.push(current->right);
}
}
18
int main() {
return 0;
}
Outpu
t:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
val = value;
left = nullptr;
right = nullptr;
}
};
int main() {
return 0;
}
Outpu
t:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
val = value;
left = nullptr;
right = nullptr;
}
};
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(7);
20
return 0;
}
Outpu
t:
Q18 Write a function to find the lowest common ancestor of two nodes in a
binary tree.
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
val = value;
left = nullptr;
right = nullptr;
}
};
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(7);
root->left->right->right = new TreeNode(8);
TreeNode* p = root->left->right;
TreeNode* q = root->left->right->right;
21
if (lca != nullptr) {
cout << "Lowest Common Ancestor of "<<p->val<<" and "<<q-
>val<<" is: "<<lca->val<<endl;
} else {
cout<<"Lowest Common Ancestor not found."<<endl;
}
return 0;
}
Outpu
t:
Q19 Write a function to perform BFS on a graph from a given start vertex.
int main(){
int V = 5;
vector<vector<int>> adj(V);
22
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 4);
return 0;
}
Outpu
t:
Q20 Write a function to perform DFS on a graph from a given start vertex.
Code: #include<iostream>
#include<vector>
int main(){
int V = 5;
vector<vector<int>> adj(V);
int start = 1;
cout << "DFS from source: " << start << endl;
DFS(adj, start);
return 0;
}
Outpu
t:
24
25