aDSA_pa1
aDSA_pa1
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
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
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];
}
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
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;
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.
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;
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;
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;
}
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++;
}
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]);
return 0;
}