समस्या
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.
सीमाएँ
0 ≤ length ≤ 100,000
उदाहरण
इनपुट:
numbers = [4, 0, -2, -7]
अपेक्षित आउटपुट:
expected = 2
इनपुट:
std::vector<long long> numbers = {4, 0, -2, -7};
अपेक्षित आउटपुट:
long long expected = 2;
इनपुट:
long[] numbers = new long[] {4, 0, -2, -7};
अपेक्षित आउटपुट:
long expected = 2;
व्याख्या
-2 at index 2 is the first negative value.
इनपुट:
numbers = [1, 2, 3]
अपेक्षित आउटपुट:
expected = -1
इनपुट:
std::vector<long long> numbers = {1, 2, 3};
अपेक्षित आउटपुट:
long long expected = -1;
इनपुट:
long[] numbers = new long[] {1, 2, 3};
अपेक्षित आउटपुट:
long expected = -1;
व्याख्या
There are no negative numbers, so the answer is -1.
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
You only need the first position where a value is below zero.
संकेत 2
Scan from left to right and stop as soon as you find one.
संकेत 3
If no negative value appears, be sure to return the missing-case value.
अपेक्षित जटिलता
O(n) time, O(1) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।