Linear Searching in C++ (O(n) Time Complexity)


Given an array of integers nums, return the index of a given value.

If the value doesn't exist, return -1.

Example 1:

Input: nums = [1, 2, 4, 5], value = 4
Output: 2
Explanation: nums[2] is 4

Note:

Your algorithm should run in O(n) time and use O(1) space.


Problem Definition

Given an array of integers nums, return the index of a given value. If the value doesn't exist, return -1.

Example:

Input: nums = [1, 2, 4, 5], value = 4
Output: 2
Explanation: nums[2] is 4

Constraints:

Understanding the Problem

The core challenge is to find the index of a given value in an array. This is a fundamental problem in computer science with applications in searching algorithms, data retrieval, and more. A common pitfall is not handling the case where the value does not exist in the array, which should return -1.

Approach

To solve this problem, we can use a simple linear search algorithm. The idea is to iterate through the array and check each element to see if it matches the given value.

Naive Solution

The naive solution involves iterating through the array and checking each element. This approach is straightforward but not optimal for large datasets.

Optimized Solution

The optimized solution is essentially the same as the naive solution because linear search is the best we can do for an unsorted array. The key is to ensure the implementation is efficient and handles edge cases properly.

Algorithm

Here is a step-by-step breakdown of the linear search algorithm:

  1. Initialize a loop to iterate through the array.
  2. For each element, check if it matches the given value.
  3. If a match is found, return the current index.
  4. If the loop completes without finding a match, return -1.

Code Implementation

#include <iostream>
#include <vector>

int linearSearch(const std::vector<int>& nums, int value) {
    // Iterate through the array
    for (int i = 0; i < nums.size(); ++i) {
        // Check if the current element matches the value
        if (nums[i] == value) {
            return i; // Return the index if a match is found
        }
    }
    return -1; // Return -1 if no match is found
}

int main() {
    std::vector<int> nums = {1, 2, 4, 5};
    int value = 4;
    int index = linearSearch(nums, value);
    std::cout << "Index of " << value << " is: " << index << std::endl;
    return 0;
}

Complexity Analysis

Time Complexity: O(n), where n is the number of elements in the array. This is because we may need to check each element in the worst case.

Space Complexity: O(1), as we are not using any extra space that scales with the input size.

Edge Cases

Consider the following edge cases:

Example:

Input: nums = [], value = 4
Output: -1

Input: nums = [1], value = 1
Output: 0

Input: nums = [1], value = 2
Output: -1

Testing

To test the solution comprehensively, consider the following test cases:

Example test cases:

std::vector<int> test1 = {1, 2, 4, 5};
std::vector<int> test2 = {};
std::vector<int> test3 = {1};
std::vector<int> test4 = {1, 2, 3, 4, 5};

assert(linearSearch(test1, 4) == 2);
assert(linearSearch(test2, 4) == -1);
assert(linearSearch(test3, 1) == 0);
assert(linearSearch(test3, 2) == -1);
assert(linearSearch(test4, 5) == 4);

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

Linear search is a fundamental algorithm that is easy to implement and understand. While it may not be the most efficient for large datasets, it is optimal for unsorted arrays. Understanding and implementing linear search is a valuable skill for any programmer.

Additional Resources

For further reading and practice, consider the following resources: