Start learning free

LESSON 147 OF 409 · LOOPING OVER ARRAY INDICES

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

Practice Read the full explanation

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.


Hints:

We should try out each element in nums until we find one equal to value and return its index. How can we achieve this?

With a for loop, of course! We can use a variable num to iterate through the elements and an if statement to check if num is equal to value. If it is, we have found the solution! But...
This is how we look for value in nums, but there's a problem. The problem asks us to return the index of that element, and by iterating like this we only know the value of the current element, not its index. How can we fix this?

We should use another method of iterating through an array: by indices! Let's use an index variable i to go through every index between 0 and nums.size() - 1.
For each index i, the index is i and the value is the element of nums at that index. If that element is equal to value, we return i.
What if we don't have any element equal to value? Where should we return -1?

After the loop, outside of it, we return -1.
If we reach that line, this means the for loop terminated naturally without returning any index. If we had returned an index, we wouldn't even be executing this line of code as the program would have finished.

We have a for loop that performs exactly n iterations (where n is the length of nums), thus the Time Complexity is O(n).
The only extra space we use is the iterator variable i inside the loop, thus the Extra Space used is O(1).

Video Solution:

STEP 1 OF 3
    Ctrl + Enter to check

    C++ · LOOPING OVER ARRAY INDICES

    Linear Search in C++, explained

    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:

    • The algorithm should run in O(n) time.
    • The algorithm should use O(1) space.

    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:

    • Empty array: The function should return -1.
    • Value not in array: The function should return -1.
    • Array with one element: The function should correctly identify if the single element matches the value.

    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:

    • Basic cases with small arrays.
    • Edge cases with empty arrays and single-element arrays.
    • Cases where the value is at the beginning, middle, and end of the array.

    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:

    • Understand the problem requirements and constraints.
    • Start with a simple solution and then optimize.
    • Think about edge cases and how to handle them.
    • Practice similar problems to improve problem-solving skills.

    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:

    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free