All challenges
Easy 0 solved 0% acceptance

First Negative Index

Find where the first negative number appears.

Problem

Find the position (counting from 0) of the first negative number in the list.

Implement first_negative_index(numbers).

Input

numbers: The list of integers.

Output

Return the index, or -1 if there are no negative numbers.

Constraints

0 ≤ length ≤ 100,000

Examples

Example 1
Input:
numbers = [4, 0, -2, -7]

Expected output:
expected = 2
Input:
std::vector<long long> numbers = {4, 0, -2, -7};

Expected output:
long long expected = 2;
Input:
long[] numbers = new long[] {4, 0, -2, -7};

Expected output:
long expected = 2;

Explanation

-2 at index 2 is the first negative value.

Example 2
Input:
numbers = [1, 2, 3]

Expected output:
expected = -1
Input:
std::vector<long long> numbers = {1, 2, 3};

Expected output:
long long expected = -1;
Input:
long[] numbers = new long[] {1, 2, 3};

Expected output:
long expected = -1;

Explanation

There are no negative numbers, so the answer is -1.

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.