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

Count Total Positives, Negatives, and Zeros from an Array

13 February 2024
public class HackerAlgo {

    // Count Total Positives, Negatives, and Zeros
    public static void countPositivesNegativesZeros(int[] arr) {
        int positiveCount = 0;
        int negativeCount = 0;
        int zeroCount = 0;

        for (int num : arr) {
            if (num> 0) {
positiveCount++;
            } else if (num< 0) {
negativeCount++;
            } else {
zeroCount++;
            }
        }

System.out.println("Positive: " + positiveCount + ", Negative: " + negativeCount + ", Zero: " + zeroCount);
    }

    public static void main(String[] args) {
        // Example for Count Total Positives, Negatives, and Zeros
int[] arr1 = {1, -2, 3, 0, -5};
countPositivesNegativesZeros(arr1);

int[] arr2 = {0, 0, 0, 0, 0};
countPositivesNegativesZeros(arr2
# Count Total Positives, Negatives, and Zeros
def count_positives_negatives_zeros(arr):
positive_count = sum(1 for num in arr if num> 0)
negative_count = sum(1 for num in arr if num< 0)
zero_count = len(arr) - positive_count - negative_count

print(f"Positive: {positive_count}, Negative: {negative_count}, Zero: {zero_count}")

# Example usage
arr1 = [1, -2, 3, 0, -5]
count_positives_negatives_zeros(arr1)

arr2 = [0, 0, 0, 0, 0]
count_positives_negatives_zeros(arr2
#include <iostream>
#include <vector>

class HackerAlgo {
public:
    // Count Total Positives, Negatives, and Zeros
    static void countPositivesNegativesZeros(const std::vector<int>&arr) {
        int positiveCount = 0;
        int negativeCount = 0;
        int zeroCount = 0;

        for (int num : arr) {
            if (num> 0) {
positiveCount++;
            } else if (num< 0) {
negativeCount++;
            } else {
zeroCount++;
            }
        }

std::cout<< "Positive: " <<positiveCount<< ", Negative: " <<negativeCount<< ", Zero: " <<zeroCount<< std::endl;
    }
};

int main() {
    // Example for Count Total Positives, Negatives, and Zeros
std::vector<int> arr1 = {1, -2, 3, 0, -5};
HackerAlgo::countPositivesNegativesZeros(arr1);

std::vector<int> arr2 = {0, 0, 0, 0, 0};
HackerAlgo::countPositivesNegativesZeros(arr2);

    return 0
class HackerAlgo {
    // Count Total Positives, Negatives, and Zeros
    static countPositivesNegativesZeros(arr) {
constpositiveCount = arr.filter(num =>num> 0).length;
constnegativeCount = arr.filter(num =>num< 0).length;
constzeroCount = arr.length - positiveCount - negativeCount;

console.log(`Positive: ${positiveCount}, Negative: ${negativeCount}, Zero: ${zeroCount}`

Leave a Comment