Read the input sequence into an array. Count the number of negative elements. If there are no negative numbers (the count is ), print "NO". Otherwise, first print the count of negative numbers, followed by the numbers themselves in reverse order.
Algorithm implementation
To store the sequence, declare an array .
int m[101];Read the input data.
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &m[i]);Count the number of negative elements in the variable .
cnt = 0;
for (i = 0; i < n; i++)
if (m[i] < 0) cnt++;If there are no negative numbers in the array, print "NO".
if (cnt == 0)
printf("NO\n");
else
{Print the count of negative numbers.
printf("%d\n", cnt);Print the negative numbers in reverse order.
for (i = n - 1; i >= 0; i--)
if (m[i] < 0) printf("%d ", m[i]);
printf("\n");
}Python implementation
Read the input data.
n = int(input())
lst = list(map(int, input().split()))Create a list negatives that contains only the negative numbers.
negatives = [x for x in lst if x < 0]If there are no negative numbers in the array, print "NO".
if not negatives:
print("NO")
else:Otherwise, first print the count of negative numbers, followed by the negative numbers in reverse order.
print(len(negatives))
print(*negatives[::-1])