countNegative.js

 /*Count negative numbers

Description
Given an array of integers of size N, find the number of negative elements in the array.

Input format
First line contains a positive integer n denoting the number of elements in the array and second
line contains n space separated elements of array.

Output format
For the given array, print the count of negative numbers in the array.

Sample input-1
5
1 -5 -4 3 0
Sample output-1
2
Explanation-1
There are two negative numbers in the array, that are -5 and -4.
Sample input-2
5
1 5 3 7 11
Sample output-2
0
Explanation-2
There are no negative elements in the array.
*/


let fs = require("fs");

let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');

function readLine() {
idx++;
return data[idx - 1].trim();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////



let noOfInputs = parseInt(readLine());// 5

let sequence = readLine().split(" "); // [ "1","-5","-4","3","0"]

let count = 0;

for (let i = 0; i < noOfInputs; i++) // 0<5 true, 1<5 true, 2<5 true, 3<5 true, 4<5 true, 5<5 false
{
if (sequence[i] < 0) //. 1<0 false, -5<0 true, -4<0 true,3<0 false, 0<0 false

{
count++; //2

}
}

console.log(count);//2



Comments