Task 1: Maximum Count
"Finding the Majority: Positives vs. Negatives!"
Given an array of integers, return the maximum between the count of positive integers and the count of negative integers. Zeros are ignored.
The Strategy: Iterate through the list once. Maintain two counters: one for positive numbers (val > 0) and one for negative numbers (val < 0). Return the larger of the two counts.
Perl Implementation
sub maximum_count ($ints) {
my ($pos, $neg) = (0, 0);
for my $n (@$ints) {
++$pos if $n > 0;
++$neg if $n < 0;
}
return $pos > $neg ? $pos : $neg;
}
Python Implementation
def maximum_count(ints: Sequence[int]) -> int:
positives = sum(1 for value in ints if value > 0)
negatives = sum(1 for value in ints if value < 0)
return max(positives, negatives)