East West University
Topic: Analyzing and Optimizing Algorithm Performance
Course Name : Algorithms
Course Code : CSE 246
Semester : Summer 2025
Submitted To:
Dr. Md. Tauhid Bin Iqbal
Assistant Professor
Department of Computer Science & Engineering
Submitted By:
Riya Bormon Momo
2022-2-60-008
Sec: 01
Department of Computer Science & Engineering
Problem 2: search a element in a array
Explanation:
# include <iostream> #include <iostream>
Using namespace std; using namespace std;
int main() {
int main() { int arr[] = {4, 8, 1, 7, 3};
int arr[] = {4, 8, 1, 7, 3}; int n = 5;
int n = 5; int x = 7;
int x = 7; bool found = false;
bool found = false; for (int i = 0; i < n; i++) {
for(int i = 0; i < n; i++) { if (arr[i] == x) {
int current = arr[i]; found = true;
if(current == x) { break;
for(int j = i; j < n; j++) { }
if(arr[j] == x) { }
found = true; if (found)
break; cout << "Element " << x << " is present
} in the array." << endl;
} else
break; cout << "Element " << x << " is NOT
} present in the array." << endl;
}
if(found) return 0;
cout << "Element " << x << " is present in the }
array." << endl;
else cout
<< "Element " << x << " is NOT present in
the array." << endl;
return 0;
}
Total Time Complexity: O(n²) and space O(1) Total time Complexity: O(n) and space O(1)
Problem 3 : Reverse an Array
Explanation:
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main() {
int arr[] = {1, 2, 3, 4, 5}; int arr[] = {1, 2, 3, 4, 5};
int n = 5; int n = 5;
int copy[100];
for(int i = 0; i < n / 2; i++) {
for(int i = 0; i < n; i++) { swap(arr[i], arr[n - 1 - i]);
copy[i] = arr[i]; }
}
cout << "Reversed Array: ";
int reversed[100]; for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
for(int i = 0; i < n; i++) { }
reversed[i] = copy[n- 1 - i]; cout << endl;
}
cout << "Reversed Array: "; return 0;
for(int i = 0; i < n; i++) { }
cout << reversed[i] << " ";
}
cout << endl;
return 0;
}
Total Time Complexity: O(n) and space O(n) Total time Complexity: O(n) and space O(1)
Problem 4: Finding Maximum in an Array
Explanation:
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main() {
int arr[] = {2, 4, 6, 8, 10}; int arr[] = {2, 4, 6, 8, 10};
int n = 5; int n = 5;
int maxArr[100];
int maxVal = arr[0];
for(int i = 0; i < n; i++) { for(int i = 1; i < n; i++) {
maxArr[i] = arr[i]; if(arr[i] > maxVal) {
} maxVal = arr[i];
}
int maxVal = maxArr[0]; }
for(int i = 1; i < n; i++) {
if(maxArr[i] > maxVal) { cout << "Maximum Element: " <<
maxVal = maxArr[i]; maxVal << endl;
} return 0;
} }
cout << "Maximum Element: " << maxVal
<< endl;
return 0;
}
Total Time Complexity: O(n) and space O(1) Total time Complexity: O(n) and space O(1)
Problem 5: Remove duplicate elements of an arry
Explanation:
#include <iostream> #include <iostream>
using namespace std; #include <unordered_set>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 2, 4, 5, 5}; int main() {
int n = 7; int arr[] = {1, 2, 3, 2, 4, 5, 5};
int copy[100]; int n = 7;
for(int i = 0; i < n; i++) { unordered_set<int> uniqueSet;
copy[i] = arr[i];
} for(int i = 0; i < n; i++) {
uniqueSet.insert(arr[i]);
int unique[100]; }
int uniqueCount = 0;
cout << "Array with unique elements: ";
for(int i = 0; i < n; i++) { for(auto element : uniqueSet) {
bool found = false; cout << element << " ";
for(int j = 0; j < uniqueCount; j++) { }
if(copy[i] == unique[j]) { cout << endl;
found = true;
break; return 0;
} }
}
if(!found) {
unique[uniqueCount++] = copy[i];
}
}
cout << "Array with unique elements: ";
for(int i = 0; i < uniqueCount; i++) {
cout << unique[i] << " ";
}
cout << endl;
return 0;
}
Total Time Complexity: O(n2) and space O(1) Total time Complexity: O(n) and space O(n)
Problem 6 : Check if a Sub array with Given Sum Exists
Explanation:
#include <iostream> #include <iostream>
#include <vector> #include <vector>
using namespace std; using namespace std;
int main() { int main() {
vector<int> arr = {1, 2, 3, 7, 5}; vector<int> arr = {1, 2, 3, 7, 5};
int n = arr.size(); int targetSum = 12;
int targetSum = 12; int currentSum = 0;
bool found = false; bool found = false;
for (int i = 0; i < n; i++) { for (int i = 0; i < arr.size(); i++) {
int current = arr[i]; currentSum = 0
if(current == targetSum) { // Directly check for (int j = i; j < arr.size(); j++) {
for a single element match currentSum += arr[j];
found = true; if (currentSum == targetSum) {
break; found = true;
} break;
for (int j = i; j < n; j++) { }
int sum = 0; }
for (int k = i; k <= j; k++) { if (found) break;
sum += arr[k]; }
}
if (sum == targetSum) { if (found) {
found = true; cout << "Subarray with given sum exists."
break; << endl;
} } else {
} cout << "No subarray with given sum
if (found) break; exists." << endl;
} }
if (found) return 0;
cout << "Subarray with given sum exists." }
<< endl;
else
cout << "No subarray with given sum
exists." << endl;
return 0;
}
Total Time Complexity: O(n3) and space Total time Complexity: O(n²) and space
O(1) O(1)
Problem 7 : Find the First Repeated Element in an Array
Explanation:
#include <iostream> #include <iostream>
#include <vector> #include <unordered_set>
using namespace std; #include <vector>
using namespace std;
int main() {
vector<int> arr = {1, 2, 3, 4, 5, 3, 6, 7}; int main() {
int n = arr.size(); vector<int> arr = {1, 2, 3, 4, 5, 3, 6, 7};
int repeatedElement = -1; unordered_set<int> seen;
bool found = false;
for (int num : arr) {
for (int i = 0; i < n; i++) { if (!seen.insert(num).second) {
for (int j = i + 1; j < n; j++) { cout << "First repeated element: " << num <<
if (arr[i] == arr[j]) { endl;
repeatedElement = arr[i]; return 0;
found = true; }
break; }
}
} cout << "No repeated elements found." <<
if (found) break; endl;
} return 0;
}
if (repeatedElement != -1) {
cout << "First repeated element: " <<
repeatedElement << endl;
} else {
cout << "No repeated elements found." <<
endl;
}
return 0;
}
Total Time Complexity: O(n²) and space O(1) Total time Complexity: O(n) and space O(n)