You are on page 1of 6

Data Structures And Algorithm Analysis

Assignment
Due Date: 11/11/2022

Name: Safa
Registration #: SP21-BSE-034

1. Create double linklist


2. Write insertion function for (insert in beginning, insert in middle, insert in
between)
3. Deletion function for (delete in beginning. Delete in between the two nodes,
delete at the end)
4. Delete at any node with respect to data
5. Display function
6. Palindrome check.
7. Delete all duplicate values
8. Sum of all even data in a node
9. Input a number from a user, split it and then save each separate digit in
sperate nodes respectively
10.Create duplicate nodes with the sequence of M nodes and N nodes.
1->2->3->4; M=2, N=3; 1->2->3->3->3->4
11. Implement stack
12. Implement two stack in an array
13. Implement stack using concept of queue
14. Implement mergabale stack
15. Reverse a stack
16. Implement a queue using stack
17. Implementation of a queue with an additional data member of age, creation
must be in ascending order with respect to age.
18. Maximum number of pairs of distinct linklist elements possible by
including each element in only one pair.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 4, 2, 4, 1, 4, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
int maxi = 0, remain, ans;

map<int, int> freq;


for (int i = 0; i < n; i++) {
freq[arr[i]]++;
}
for (int it : freq) {

maxi = max(maxi, it.second);


}

remain = n - maxi;
if (maxi >= remain) {

ans = remain;
}
else {

ans = n / 2;
}
cout << ans;
freq.clear();
return 0;
}

19.Rank the array according to rightmost set bit and least set bits
#include <bits/stdc++.h>
using namespace std;

class Pair {
public:
int index;
int rsb;
int setbit;

Pair(int index, int rsb, int setbit)


:index(index),rsb(rsb),setbit(setbit)
{
}
};

bool operator<(const Pair& a, const Pair& b)


{
if (a.rsb > b.rsb)
return false;
else if (a.rsb < b.rsb)
return true;
else if (a.setbit < b.setbit)
return false;
else if (b.setbit < a.setbit)
return true;
else if (a.index < b.index)
return false;
else
return true;
}

void rearrange(int ar[], int n)


{

priority_queue<Pair> pq;

for (int i = 0; i < n; i++) {

int k = (ar[i] & -ar[i]);

int setbit
= __builtin_popcount(ar[i]);

pq.push(Pair(i, k, setbit));
}

int rank = 1;

while (!pq.empty()) {
Pair p = pq.top();
pq.pop();
ar[p.index] = rank++;
}
}

int main()
{
int arr[] = { 1, 2, 3, 4, 5 };

int N = sizeof(arr) / sizeof(arr[0]);;

rearrange(arr, N);
for (int i = 0; i < N; i++)
cout<<arr[i]<<" ";

return 0;
}

20.Convert given Array to 0 by reducing elements pairwise with any


positive value
#include <bits/stdc++.h>
using namespace std;

void gfg(vector<int>& v)
{

priority_queue<int> q;

for (auto x : v) {
q.push(x);
}

int cnt = 0;
while (q.size() >= 2) {

int ele1 = q.top();


q.pop();

int ele2 = q.top();


q.pop();

ele1--;
ele2--;
cnt += 2;

if (ele1) {
q.push(ele1);
}
if (ele2) {
q.push(ele2);
}
}

if (q.size() == 0)
cout << cnt << endl;
else
cout << -1;
}

int main()
{

vector<int> v = { 6, 3, 4 };
gfg(v);
}

21.Maximum count of pairs such that element at each index i is included in


i pairs
#include <bits/stdc++.h>
using namespace std;

void maxPairs(int arr[], int n)


{

vector<int> matchList;

priority_queue<int> pq;

for (int i = 0; i < n; i++)


{
if (arr[i] > 0)
pq.push(i);
}
while (pq.size() >= 2)
{

int top = pq.top();


pq.pop();

int cur = pq.top();


pq.pop();

matchList.push_back(top + 1);
matchList.push_back(cur + 1);
arr[top]--;
arr[cur]--;

if (arr[top] > 0)
pq.push(top);

if (arr[cur] > 0)
pq.push(cur);
}

cout << (matchList.size() / 2) << "\n";


for (int i = 0; i < matchList.size(); i += 2)
{
cout << matchList[i] << " " << matchList[i+1] << "\n";
}
}

int main()
{
int arr[] = { 5, 6, 7, 8 };
int n = sizeof(arr)/sizeof(arr[0]);
maxPairs(arr,n);

return 0;
}

You might also like