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
Your algorithm should run in O(n) time and use O(1) space.
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 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.
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.
The naive solution involves iterating through the array and checking each element. This approach is straightforward but not optimal for large datasets.
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.
Here is a step-by-step breakdown of the linear search algorithm:
#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;
}
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.
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
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);
When approaching such problems, consider the following tips:
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.
For further reading and practice, consider the following resources: