Array ( [0] => [1] => questions [2] => Arrays [3] => Linear-Search ) Popular Algorithms | Linear Search | THE INQUISITIVE





Linear Search

LEVEL:Beginner

Description

Given a list of integers and a search value. Find the search value from the array using Linear Search Algorithm.

Input Format

First line contains the size of array (n), Second line contains the search value followed by n integers separated by spaces.

Output Format

Print the index of the search value if present else print -1.


Example 1:

Input
5
3
32 45 3 56 790
Output
2
Example 2:

Input
6
34
37 46 33 205 67 105
Output
-1
Example 3:

Input
5
980
37 463 756 980 67 105
Output
3

oops

Login to see Discussion




Approach

Step-i) Take the input search value and array from the user.
Step-ii) Traverse through the whole array, by comparing each array element with the search value.
Step-iii) If the elements match return the index of element in array, else return -1.

Time Complexity: O(n)
Space Complexity: O(n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution