A cold storage company has N storage units for various products. The company has received N orders that must be preserved at respective N temperatures inside the storage units. The company manager wishes to identify which products must be preserved at negative temperatures.
Write an algorithm to help the manager find the number of products that have negative temperature storage requirements.
Input
The first line of the input consists of an integer numOfProducts, representing the number of products (N).
The second line consists of N space-separated integers – temperature0, temperature1,….. , temperatureN-1 representing the temperatures at which the products must be preserved in the storage units.
Output
Print an integer representing the number of products that must be preserved at negative temperatures.
Constraints
0 ≤ numOfProducts ≤ 106
-106 ≤ temperaturei ≤ 106
0 ≤ i < numOfProducts
Example
Input:
7
9 -3 8 -6 -7 8 10
Output:
3
Explanation:
The products that must be preserved at negative temperatures are at indices [1,3,4] i.e. [-3, -6, -7].
So, the output is 3.
Solution
import java.util.Scanner;
public class a {
public static int countNegativeTemperatures(int[] temperatures) {
int count = 0;
for (int i = 0; i < temperatures.length; i++) {
if (temperatures[i] < 0) {
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numOfProducts = scanner.nextInt();
int[] temperatures = new int[numOfProducts];
for (int i = 0; i < numOfProducts; i++) {
temperatures[i] = scanner.nextInt();
}
System.out.println(countNegativeTemperatures(temperatures));
}
}
Happy Learning – If you require any further information, feel free to contact me.