Practice

Linear Search

HARDAlgorithms

Read N (1 ≤ N ≤ 10), then N integers into an array, then a target value. Output the 1-based position of the first occurrence of the target in the array, or -1 if not found.

Input: N, then N integers, then the target. Output: The position (1-based) or -1.

Example:

Input:  5
        10  30  20  40  50
        30
Output: 2

Sample Test Cases

Test 1: Found at index 2
Inputs: 5, 10, 30, 20, 40, 50, 30
Expected: 2
Test 2: Not found
Inputs: 5, 10, 30, 20, 40, 50, 99
Expected: -1
Test 3: Found at index 1
Inputs: 3, 5, 5, 5, 5
Expected: 1

Premium Question

This question needs a paid plan — upgrade, or join a class from a teacher who has one. You can read the description and sample tests, but grading is locked.

Sign in to get started