0% found this document useful (0 votes)
17 views11 pages

aDSA_pa1

The document outlines programming assignments for Advanced Data Structures and Algorithms, detailing algorithms and code for various problems such as finding the largest element in an array, reversing an array, and merging two sorted arrays. Each problem includes the algorithm steps, code implementation in C++, and an analysis of time and space complexity. The assignments emphasize understanding and applying fundamental data structure concepts.

Uploaded by

triptiofficial29
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)
17 views11 pages

aDSA_pa1

The document outlines programming assignments for Advanced Data Structures and Algorithms, detailing algorithms and code for various problems such as finding the largest element in an array, reversing an array, and merging two sorted arrays. Each problem includes the algorithm steps, code implementation in C++, and an analysis of time and space complexity. The assignments emphasize understanding and applying fundamental data structure concepts.

Uploaded by

triptiofficial29
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/ 11

School of Computing Science & Engineering

Subject: Advanced Data Structures and Algorithms


(E2UC503B) Programming Assignment 1
Name: Aaryan Kumar Pankaj
Admi. no. : 22SCSE1012011

Write algorithms, program of following problems. Also


find the time and space complexity of each algorithm.

1. Find the largest element in a given array.

Algorithm:
1. Start
2. Input integer n (the number of elements in the array).
3. Declare an array arr of size n.
4. For each i from 0 to n-1, do:
Input arr[i].
5.Initialize maxNo = INT_MIN.
6.For each i from 0 to n-1, do:
If arr[i] > maxNo, set maxNo = arr[i].
7.Output maxNo.
8.End

Time Complexity: O(n)


Space Complexity: O(n)
#include <iostream>
#include <climits>
#include <vectors>
using namespace std;

int main() {
int n;
cin >> n;

int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

int maxNo = INT_MIN;

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


maxNo = max(maxNo, arr[i]);
}

cout << maxNo << endl;


return 0;
}

2. Reverse a given array.

Algorithm
1. Start
2. Input the size of the array n.
3. Declare an array arr of size n.
4. For each i from 0 to n-1, do:
Input arr[i].
5.Use two pointers:
Set left = 0 (start of the array)
and right = n-1 (end of the array).
6.While left < right, do the following:
Swap arr[left] and arr[right].
Increment left by 1 and decrement right by 1.
7.Output the reversed array.
8.End
#include <iostream>
#include <climits>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0; i<=n-1; i++){
cin>>arr[i];
}

int left =0;


int right =n-1;

while (left < right){


int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}

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


cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}

Time complexity is O(n)


Space complexity is O(1)
3. Find the second largest element in a given array

Algorithm :
1. Start
2. Input the size of the array n.
3. Declare an array arr of size n.
4. For each i from 0 to n-1, do:
Input arr[i].
5.Initialize two variables:
largest = INT_MIN (to keep track of the largest element)
second_largest = INT_MIN (track of the 2nd largest element)
6.For each i from 0 to n-1, do:
If arr[i] > largest, set second_largest = largest and largest = arr[i].
Else if arr[i] > second_largest and arr[i] != largest,
set second_largest = arr[i].
7.Output second_largest.
8.End

#include <iostream>
#include <climits>
using namespace std;
int main() {
int n;
cin >> n;
if (n < 2) {
cout << "Array must have at least two elements!" << endl;
return -1;
}
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int largest = INT_MIN;
int second_largest = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > largest) {
second_largest = largest;
largest = arr[i];
} else if (arr[i] > second_largest && arr[i] != largest) {
second_largest = arr[i];
}
}
if (second_largest == INT_MIN) {
cout << "No second largest element exists." << endl;
} else {
cout << "Second largest : " << second_largest << endl;
}

return 0;
}
Time Complexity: O(n) since the array is traversed
only once.
Space Complexity: O(1) as no extra space is used apart
from a few variables

4. Check if a given array is sorted

Algorithm :
1. Start
2. Input the size of the array n.
3. Declare an array arr of size n.
4. For each i from 0 to n-1, do:
Input arr[i].
5.Initialize a boolean variable Sorted = true.
6.Loop through the array from index i = 1 to n-1:
If arr[i] < arr[i-1], set isSorted = false and break the loop.
7.If isSorted is still true, output "Array is sorted".
Otherwise, output "Array is not sorted".
8.End
#include <iostream>
#include <climits>
using namespace std;

int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
bool sorted = true;

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


if (arr[i] < arr[i-1]) {
sorted = false;
break;
}
}
if (sorted) {
cout << "The array is sorted." << endl;
} else {
cout << "Not sorted " << endl;
}
return 0;
}

Time Complexity: O(n) because we are iterating through the array once.
Space Complexity: O(1) since only one extra boolean variable (Sorted) is used.

5. Remove duplicates from a given array

Algorithm :
1. Start
2. Input the size of the array n.
3. Declare an array arr of size n.
4. For each i from 0 to n-1, do:
Input arr[i].
5.Initialize an empty set to track unique elements.
6.Create a new array or use a separate index to store unique elements only.
7.Traverse the original array and for each element:
If the element is not in the set, add it to the set and the new array.
8.Output the new array without duplicates.
9.End

#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
int n;
cin >> n;

int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i]; // Input elements
}

unordered_set<int> seen;
vector<int> result;

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


if (seen.find(arr[i]) == seen.end()) {
result.push_back(arr[i]);
seen.insert(arr[i]);
}
}
for (int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
}
cout << endl;

return 0;
}
Time Complexity: O(n)
Space Complexity: O(n)
6. Rotate a given array

Algorithm:
1. Start
2. Input the array arr[] of size n and number of positions d to rotate.
3. Compute d = d % n to handle cases where d >= n.
4. Reverse the subarray arr[0...d-1] (the first d elements).
5. Reverse the subarray arr[d...n-1] (the remaining elements).
6. Reverse the entire array arr[0...n-1] to complete the left rotation.
7. Print the array.
8. End

Example :
Let arr = {1, 2, 3, 4, 5} and d = 2. The size n of the array is 5.
First, reverse the first d elements:
Reverse arr[0...1] → {2, 1, 3, 4, 5}
Then, reverse the remaining elements:
Reverse arr[2...4] → {2, 1, 5, 4, 3}
Finally, reverse the whole array:
Reverse arr[0...4] → {3, 4, 5, 1, 2}
So, the array {1, 2, 3, 4, 5} after left rotation by 2 positions becomes {3, 4, 5,
1, 2}.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void leftRotate(vector<int>& arr, int d) {
int n = arr.size();
d = d % n;

// Reverse the first d elements


reverse(arr.begin(), arr.begin() + d);

// Reverse the remaining elements


reverse(arr.begin() + d, arr.end());

// Reverse the whole array


reverse(arr.begin(), arr.end());
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
int d = 2;
leftRotate(arr, d);
cout << "Array after left rotation:" << endl;
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Time Complexity: O(n)
Space Complexity: O(1)

7. Find the frequency of elements in a given array

Algorithm:
1. Start
2. Input the array arr[] of size n.
3. Declare an empty unordered map mp to store the frequency of elements.
4. Traverse the array:
For each element arr[i], increment its frequency in the map mp.
If arr[i] is not in the map, it is automatically inserted with a count of 1.
5.After processing the array, traverse the map:
For each element in the map, print the key (element) and its
corresponding value (frequency).
6.End

#include <bits/stdc++.h>
using namespace std;
void countFreq(int arr[], int n)
{
unordered_map<int, int> mp;
// Traverse through array elements and count frequencies
for (int i = 0; i < n; i++)
mp[arr[i]]++;
// Traverse through map and print frequencies
for (auto x : mp)
cout << x.first << " " << x.second << endl;
}
int main()
{
int arr[] = { 10, 20, 20, 10, 10, 20, 5, 20 };
int n = sizeof(arr) / sizeof(arr[0]);
countFreq(arr, n);
return 0;
}

Time Complexity: O(n)


Space Complexity: O(n)

8. Merge two sorted arrays

Steps:
1. Start
2. Input two sorted arrays arr1[] and arr2[] of sizes n1 and n2, respectively.
3. Initialize three pointers:
i = 0 for array arr1[]
j = 0 for array arr2[]
k = 0 for the merged array merged[] of size n1 + n2
4.Compare elements of both arrays:
If arr1[i] is less than or equal to arr2[j], place arr1[i] in the merged[]
array and increment i and k.
Otherwise, place arr2[j] in the merged[] array and increment j and k.
5.If one array is exhausted, copy the remaining elements of the other array
into the merged[] array.
6.Output the merged array.
7.End

#include <iostream>
using namespace std;
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int merged[]) {
int i = 0, j = 0, k = 0;
// Traverse both arrays and pick smaller element
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
merged[k] = arr1[i];
i++;
} else {
merged[k] = arr2[j];
j++;
}
k++;
}
// If there are remaining elements in arr1[], add them to merged[]
while (i < n1) {
merged[k] = arr1[i];
i++;
k++;
}

// If there are remaining elements in arr2[], add them to merged[]


while (j < n2) {
merged[k] = arr2[j];
j++;
k++;
}
}

int main() {
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 4, 6, 8};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);

int merged[n1 + n2]; // Create an array to hold the merged elements


mergeArrays(arr1, n1, arr2, n2, merged);

// Output the merged array


cout << "Merged array: ";
for (int i = 0; i < n1 + n2; i++) {
cout << merged[i] << " ";
}
cout << endl;

return 0;
}

Time Complexity: O(n1 + n2) since we traverse both arrays


once.
Space Complexity: O(n1 + n2) because we need additional
space to store the merged array of size n1 + n2.

You might also like