Professional Documents
Culture Documents
1) Given that there is a number present in all the three sorted arrays- x[1], x[2]….,x[p] y[1],
y[2]….., y[q] z[1], z[2]…..z[r]. Find atleast one common element in o(p+q+r) time.
➔ To have the task done in O(p+q+r) we need to first find the intersection of first two arrays and
store the intersection in a temporary array and then find the intersection of temporary array
and the third array.
#include <bits/stdc++.h>
using namespace std;
void question1(int arr1[], int arr2[], int arr3[], int n1, int n2, int n3)
{
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2 && k < n3)
{
if (arr1[i] == arr2[j] && arr2[j] == arr3[k]){
cout << arr1[i] << " "; i++; j++; k++;
}
else if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr3[k])
j++;
else
k++;
}
}
int main()
{
int n1, n2, n3;
cin >> n1 >> n2 >> n3;
int arr1[n1], arr2[n2], arr3[n3];
for (int i = 0; i < n1; i++) {
cin >> arr1[i];
}
for (int i = 0; i < n2; i++) {
cin >> arr2[i];
}
for (int i = 0; i < n3; i++) {
cin >> arr3[i];
}
cout << "Common Elements are: ";
question1(arr1, arr2, arr3, n1, n2, n3);
return 0;
}
DAA Assignment 2
Name: Sarang Dev Saha Roll no.: 20UCS173
2) Given a matrix A[1,2…n][1,2…m], where each row is sorted and there is a common element in
all rows. Find the element. Time complexity should be O(n*m) [naïve algo yields a complexity
of O(n*m2)].
➔ To have the task done in 0(n*m) where n is the number of rows and m is the number of
columns, we take every element of the first row and search it in the subsequent rows till we find
a common element.
The naïve approach would be using Linear search, which would result in a complexity of
O(n*m2). Hence, for our desired complexity we will use Merge sort.
#include <bits/stdc++.h>
using namespace std;
#define N 4
#define M 5
3) If A(x) = anxn + ... +a1x+a0, then the derivative A(x), A`(x) = nanxn-1 + ... + a1. Devise an
algorithm which produces the value of a polynomial and its derivative at a point x = v.
Determine the number of required arithmetic operations.
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int> v1;
int n;
cin>>n;
int v;
cin>>v;
for(int i=0;i<n;i++){
int n1;
cin>>n1;
v1.push_back(n1);
}
int res=v1[0];
for(int i=1;i<n;i++){
res=res*v+v1[i];
}
cout<<res<<endl;
vector<int> v2(n-1);
for(int i=0;i<v2.size();i++){
int a=v1[i]*(n-1-i);
v2[i]=a;
}
int res1=v2[0];
for(int i=1;i<n-1;i++){
res1=res1*v+v2[i];
}
cout<<res1<<endl;
return 0;
}
DAA Assignment 2
Name: Sarang Dev Saha Roll no.: 20UCS173
➔ In this implementation we first move through the first row and look for 0, and then move
diagonally downward looking for 0s and incrementing ‘count’. Thus the time complexity of this
implementation will be O(n3/2).
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<vector<int>> v;
int n,m;
cin>>n>>m;
int count=0;
for(int i=0;i<n;i++){
vector<int> v1;
for(int j=0;j<m;j++){
int n1;
cin>>n1;
v1.push_back(n1);
}
v.push_back(v1);
}
int i=0;
int j=0;
while(i<n&&j<n){
if(v[i][j]>0){
i++;
}
else if(v[i][j]<0){
j++;
}
else{
count++;
i++;
j++;
}
}
cout<<count<<endl;
return 0;
}