0% found this document useful (0 votes)
3 views6 pages

Tushar AP4.

The document outlines two programming experiments for a Computer Science course, focusing on finding the closest numbers in an array and identifying missing numbers between two arrays. Each experiment includes objectives, algorithms, implementation code in C++, and learning outcomes related to sorting, counting, and comparing elements. The time and space complexities for both experiments are also provided.

Uploaded by

tusharsingh06.ts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Tushar AP4.

The document outlines two programming experiments for a Computer Science course, focusing on finding the closest numbers in an array and identifying missing numbers between two arrays. Each experiment includes objectives, algorithms, implementation code in C++, and learning outcomes related to sorting, counting, and comparing elements. The time and space complexities for both experiments are also provided.

Uploaded by

tusharsingh06.ts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 4
Student Name: Tushar Singh UID: 22BCS15923
Branch:CSE Section/Group:622/B
Semester: 5 Date of Performance:20/8/24
Subject Name: Advanced Programming Lab-1 Subject Code: 22CSP-314

1. Title: Closest Num

2. Aim: Given a list of unsorted integers, arr, find the pair of elements that have
the smallest absolute difference between them. If there are multiple pairs, find
the mall.

3. Objective:

Complete the closestNumbers function in the editor below.


closestNumbers has the following parameter(s):
int arr[n]: an array of integers

4. Algorithm:

1. Sort the array to arrange numbers in increasing order.

2. Initialize the minimum difference to the maximum possible integer value.

3. Find the smallest difference between consecutive elements.

4. Collect all pairs with this minimum difference into the result vector.

5. Output the result


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Implementation/Code

#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;

const int MAX_SIZE =


100000;
void closestNumbers(int* arr,
int n, int* result, int&
resultSize) {
if (n < 2) {
resultSize = 0;
return;
}
sort(arr, arr + n);
int min_d = INT_MAX;
resultSize = 0;
for (int i = 0; i < n - 1; ++i) {
int diff = arr[i + 1] - arr[i];
if (diff < min_d) {
min_d = diff;
resultSize = 0; // Reset
result size
}
if (diff == min_d) {
result[resultSize++] =
arr[i];
result[resultSize++] =
arr[i + 1];
}
}
}
int main() {
int n;
cin >> n;
int* arr = new int[n];
for (int i = 0; i < n; ++i) cin
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
>> arr[i];
int result[MAX_SIZE];
int resultSize;
closestNumbers(arr, n,
result, resultSize);

for (int i = 0; i < resultSize;


++i) cout << result[i] << "
";
cout << endl;

delete[] arr;
return 0;
}
6. Output:

7. Learning Outcomes:
▪ Learn how to use std::sort to arrange elements in ascending order.

▪ Understand how to find and compare differences between


consecutive elements.

8. Time Complexity: O(nlogn)

9. Space Complexity: O(n)


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem - 2

1. Title: Missing Numbers

2. Aim: Given two arrays of integers, findwhichelements in thesecondarray are


missing from the first array.

3. Objective:

Complete the missingNumbers function in the editor below. It should return a


sorted array of missing numbers. missingNumbers has the following
parameter(s):
→int arr[n]: the array with missing numbers
→int brr[m]: the original array of numbers

4. Algorithm:

o Count occurrences of elements in arr and brr.


o Compare counts of elements in brr against arr.
o Identify elements where brr count exceeds arr count.
o Store these elements in a set to ensure uniqueness.
o Copy elements from the set to the result array.
o Print the elements
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Implementation/Code

#include <iostream>
#include <unordered_map>
#include <set>
#include <memory>

using namespace std;

void findMissing(const int arr[], int arrSize, const int brr[], int brrSize, int result[], int& resultSize) {
unordered_map<int, int> countArr, countBrr;
set<int> missingNumbers;

for (int i = 0; i < arrSize; ++i) countArr[arr[i]]++;


for (int i = 0; i < brrSize; ++i) countBrr[brr[i]]++;

for (const auto& [number, countInBrr] : countBrr) {


if (countInBrr > countArr[number]) missingNumbers.insert(number);
}

resultSize = 0;
for (int num : missingNumbers) result[resultSize++] = num;
}

int main() {
int n, m;
cin >> n;
auto arr = make_unique<int[]>(n);
for (int i = 0; i < n; ++i) cin >> arr[i];

cin >> m;
auto brr = make_unique<int[]>(m);
for (int i = 0; i < m; ++i) cin >> brr[i];

int result[MAX_SIZE], resultSize = 0;


findMissing(arr.get(), n, brr.get(), m, result, resultSize);

for (int i = 0; i < resultSize; ++i) cout << result[i] << " ";
cout << endl;

return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Output:

7. Learning Outcomes:
o Learn to use hash maps to efficiently count and compare element
frequencies in arrays.

o Understand how to identify missing elements by comparing counts from


two arrays.

8. Time Complexity: O(n+m)

9. Space Complexity: O(n+m)

You might also like