Algorithm 18 - Count of positives / sum of negatives

Beast from the east·2021년 10월 6일
0

Algorithm

목록 보기
18/27

Q.

Description:
Given an array of integers.

Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.

If the input array is empty or null, return an empty array.

Example
For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].

A)

#include <stddef.h>

void count_positives_sum_negatives(
  int *values, size_t count, int *positivesCount, int *negativesSum) 
{
  for (size_t i = 0; i < count; i++)
  {
    if (values[i] > 0)
      *positivesCount = *positivesCount + 1;
    else if (values[i] < 0)
      *negativesSum += values[i];
  }
}  -> 0 is not positive
profile
Hello, Rabbit

0개의 댓글