How can I Find the first negative number in an array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I Find the first negative number in an array?

Let's say I have an array of X=[22, 41, 2, -10, 7, -55, 88, 4] and I want to find the first negative number. How can I do that in python?

21st Apr 2021, 7:06 PM
Karzan
4 Answers
+ 1
Karzan Sort array and get 1st value. You can do like this also arr = [22, 41, 2, -10, 7, -55, 84, 4] min = 0 for i in arr: if i < 0: min = i break print (min)
21st Apr 2021, 7:13 PM
A͢J
A͢J - avatar
+ 1
This is what I did so others can see too. for val in X: if val <0: print(val) break
21st Apr 2021, 7:18 PM
Karzan
+ 1
🅰🅹 (Challenge Accepted) your second solution is better. Sorting might rearrange the original order. Then you would find the lowest negative number, though not necessarily the first.
21st Apr 2021, 10:14 PM
Brian
Brian - avatar
0
Nevermind, I solved it
21st Apr 2021, 7:13 PM
Karzan