1029. Search an Element in an Array

Given an integer array arr[] and an integer k, you need to find the index of the element k in the array. If the element is not found, return -1.

Example 1:

Input: arr = [1, 2, 3, 4, 5], k = 3

Output: 2

Explanation: The element `3` is found at index `2` (0-based index).

Example 2:

Input: arr = [6, 7, 9, 5, 3, 10], k = 10

Output: 5

Explanation: The element `10` is found at index `5`.

Now Your Turn!

Pick the correct output for the given input

Input: arr = [3, 8, 15, 21, 7], k = -25

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ n ≤ 106
  • -105 ≤ arr[i] ≤ 105
0
class Solution {
public:
int searchElement(vector<int>& arr, int k) {
// Your code goes here
}
};
Test Case

Input:

Arr
K