You are on page 1of 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 4

Student Name: Rahul Raj UID: 20BCS7051


Branch: BE-CSE Section/Group: 902-B
Semester: 5th Subject Code: 20CSP-314
Subject Name: Competitive Coding Lab

Question Statement 4.2 –

1. Aim/Overview of the practical:


Given two arrays of integers, find which elements in the second array are missing from the
first array.
Example
arr = [7,2,5,3,5,3]
brr = [7,2,5,4,6,3,5,3]
The brr array is the original list. The numbers missing are [4,6].

2. Task to be done/ Which logistics used:


Question – Find missing number.
Approach – Using hashing.

3. Algorithm/Flowchart (For programming based labs):


1. Start.
2. Declare new array for storing frequencies.
3. Iterate both arrays and store frequencies in map.
4. Then check if count of numbers of first array is equal to second or not.
5. Stop.
4. Steps for experiment/practical/Code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int frequency[100001];
for(int i = 1; i <= 100000; i++) {
frequency[i] = 0;
}
int n, m;
cin>>n;

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


int tmp;
cin>>tmp;
frequency[tmp]++;
}
cin>>m;
for(int i = 0; i < m; i++) {
int tmp;
cin>>tmp;
frequency[tmp]--;
}

for(int i = 1; i <= 10000; i++) {


if(frequency[i] != 0) {
cout<<i<<" ";
}
}
return 0;
}

5. Observations/Discussions/Complexity Analysis:

Time Complexity – O(m) where n is the length of the larger array.


Space Complexity– O(m - n).

6. Result/Output/Writing Summary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Question Statement 4.4 –

1. Aim/Overview of the practical:


Given an array of integers and a target value, determine the number of pairs of
array elements that have a difference equal to the target value. Example

k=1
arr = [1,2,3,4]
There are three values that differ by k = 1: 2 – 1 = 1, 3 – 2 = 1, and 4 – 3 = 1. Return 3.

2. Task to be done/ Which logistics used:


Question – Number of pairs that exist to generate target difference.
Approach – Using set.

3. Algorithm/Flowchart (For programming based labs):


1. Start.
2. Declare a set S.
3. First add k to all elements in array and store in set.
4. Then search for array elements in S as they will be automatically present if we
added target difference.
5. Stop.

4. Steps for experiment/practical/Code:

#include <bits/stdc++.h>
using namespace std;
int pairs(int k, vector<int> arr) {
int count = 0;
set<int> s;
for (int i = 0; i < arr.size(); i++)
{ s.insert(arr[i] + k);
}
for (int i = 0; i < arr.size(); i++) {
if (s.find(arr[i]) != s.end()) count++;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
return count;
}
int main() {
int n, k, temp;
cin >> n >> k;
vector<int> arr;
for (int i = 0; i < n; i++)
{
cin >> temp;
arr.push_back(temp);
}
cout << pairs(k, arr);
}
5. Observations/Discussions/Complexity Analysis:

Time Complexity – O(n) where n is the length of the list.


Space Complexity – O(n).

5. Result/Output/WritingSumm
Result/Output/WritingSummary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning outcomes (What I have learnt):

1. Learn the concept of Searching.

2. Types of sorting and operations on that.

3. Implementation of conditional statements.

4. How to use STL.

You might also like