Don't miss your Year-End Offer: Up to 20% OFF

Count Negative Numbers in Array

13 February 2024
public class HackerAlgo {

    public static int countNegativeNumbers(int[] arr) {
        int count = 0;
        for (int num : arr) {
            if (num< 0) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
int[] exampleArr = {1, 5, -3, 2, 0, -8, 4};
        int result = countNegativeNumbers(exampleArr);
System.out.println(result);
    }
}
def count_negative_numbers(arr):
    return sum(1 for num in arr if num< 0)

# Example usage
example_arr = [1, 5, -3, 2, 0, -8, 4];
result = count_negative_numbers(example_arr)
print(result)
#include <iostream>
#include <vector>

class HackerAlgo {
public:
    static int countNegativeNumbers(const std::vector<int>&arr) {
        int count = 0;
        for (int num : arr) {
            if (num< 0) {
                count++;
            }
        }
  return count

Leave a Comment