You are on page 1of 4

DAA Assignment 2

Name: Sarang Dev Saha Roll no.: 20UCS173

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

void question2(int mat[N][M])


{
unordered_map<int, int> map;
for (int j = 0; j < M; j++)
map[mat[0][j]] = 1;
for (int i = 1; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (map[mat[i][j]] == i)
{
map[mat[i][j]] = i + 1;
if (i==N-1 && map[mat[i][j]]==N)
cout << mat[i][j] << " ";
}
}
}
}
int main()
{
int mat[N][M] =
{
{1, 2, 3, 4, 5},
{3, 5, 7, 9, 11},
{3, 5, 11, 13, 15},
{1, 3, 5, 7, 9},
};
question2(mat);
return 0;
}
DAA Assignment 2
Name: Sarang Dev Saha Roll no.: 20UCS173

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

5) Consider an n x n array A containing integer elements (positive, negative, and zero).


Assume that the elements in each row of A are in strictly increasing order, and the
elements of each column of A are in strictly decreasing order. (Hence there cannot be two
zeroes in the same row or the same column.) Describe an efficient algorithm that counts
the occurrences of the element 0 in A. Analyze the running time.

➔ 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;
}

You might also like