Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions search/linear_search.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* \file
* \brief [Linear search
* algorithm](https://en.wikipedia.org/wiki/Linear_search)
* \brief Linear search algorithm implementation
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* @author Unknown author
* @author [Ritika Mukherjee](https://github.com/ritikaa17)
Expand All @@ -11,32 +12,34 @@
#include <iostream> /// for IO operations

/**
* \brief [Algorithm implementation for linear search]
* \brief Algorithm implementation for linear search
* \param [in] array array to search in
* \param [in] size length of array
* \param [in] key key value to search for
* \returns index where the key-value occurs in the array
* \returns -1 if key-value not found
* \returns -1 if key-value not found or invalid input
*/
int LinearSearch(int *array, int size, int key) {
int LinearSearch(int* array, int size, int key) {
// Handle invalid input
if (array == nullptr || size <= 0) {
return -1;
}

for (int i = 0; i < size; ++i) {
if (array[i] == key) {
return i;
}
}

/* We reach here only in case element is not present in array, return an
* invalid entry in that case*/
return -1;
}

/**
* @brief Self-test implementations
* @returns void
*/
static void tests() {
int size = 4;
int *array = new int[size];
int* array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = i;
}
Expand All @@ -57,13 +60,15 @@ static void tests() {
assert(LinearSearch(array, size, 1) == 1);
assert(LinearSearch(array, size, 5) == 5);

// Edge case test
assert(LinearSearch(nullptr, 0, 5) == -1);

std::cout << "All tests have successfully passed!\n";
delete[] array; // free memory up
delete[] array;
}

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
int mode = 0;
Expand All @@ -83,10 +88,9 @@ int main() {
std::cin >> size;
}

int *array = new int[size];
int* array = new int[size];
int key = 0;

// Input for the array elements
std::cout << "Enter the array of " << size << " numbers: ";
for (int i = 0; i < size; i++) {
std::cin >> array[i];
Expand All @@ -103,7 +107,7 @@ int main() {
}
delete[] array;
} else {
tests(); // run self-test implementations
tests();
}
return 0;
}
}
43 changes: 43 additions & 0 deletions trees/LCA.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdexcept>

struct Node {
int data;
Node* left;
Node* right;

Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

bool find(Node* root, int val) {
if (!root)
return false;
if (root->data == val)
return true;
return find(root->left, val) || find(root->right, val);
}

Node* lcaUtil(Node* root, int p, int q) {
if (!root)
return nullptr;

if (root->data == p || root->data == q)
return root;

Node* left = lcaUtil(root->left, p, q);
Node* right = lcaUtil(root->right, p, q);

if (left && right)
return root;

return left ? left : right;
}

Node* LCA(Node* root, int p, int q) {
if (!root)
return nullptr;

if (!find(root, p) || !find(root, q))
return nullptr;

return lcaUtil(root, p, q);
}