JavaScript program to find the average of all negative numbers in an array

In this problem, we are given an array of integers, which may contain both negative and positive numbers. We have to find the average of all the negative numbers in the array. In this article, we are going to learn how we can find the average of all negative numbers in an array using JavaScript.

Problem Examples

Example 1

Input:

arr = [1, -3, 4, -2, -5];

Output:

-3.33

Explanation: The negative numbers in the array are -3, -2, and -5. Their sum is -10, and the number of negative elements is 3. The average is: -10 / 3 = -3.33.

Example 2

Input:

arr = [7, -4, -8, 5, -6, 10];

Output:

-6

Explanation: The negative numbers in the array are -4, -8, and -6. Their sum is -18, and the number of negative elements is 3. The average is: -18 / 3 = -6.

Approaches to Find the Average of All Negative Numbers

Below are different approaches to finding the average of negative numbers in an array:

Using For Loop Approach

This is the simple and direct approach to finding the average of all negative elements in the array. In this approach, we manually iterate through each element of the array and check if it's negative. If it is, we add it to a running sum and increment the count. At the end of the iteration, we divide the sum of negative numbers by the count of negative elements to find the average.

Steps for Implementation

  • Initialize variables to track sum and count of negative numbers
  • Loop through each element in the array
  • Check if the element is negative and update sum and count
  • Calculate and return the average (sum/count)
  • Handle edge case when no negative numbers exist

Example

function findAverageOfNegatives(arr) {
    let sum = 0;
    let count = 0;

    for (let i = 0; i < arr.length; i++) {
        if (arr[i] < 0) {
            sum += arr[i];
            count++;
        }
    }

    if (count === 0) {
        return 0; // No negative numbers
    }

    return sum / count;
}

let arr1 = [1, -3, 4, -2, -5];
let arr2 = [7, -4, -8, 5, -6, 10];
let arr3 = [1, 2, 3, 4]; // No negatives

console.log("Array 1 average:", findAverageOfNegatives(arr1));
console.log("Array 2 average:", findAverageOfNegatives(arr2));
console.log("Array 3 average:", findAverageOfNegatives(arr3));
Array 1 average: -3.3333333333333335
Array 2 average: -6
Array 3 average: 0

Time Complexity: O(N) - Single pass through the array
Space Complexity: O(1) - Only using constant extra space

Using Filter and Reduce Methods

In this approach, we use JavaScript's built-in array methods. The filter() method selects only the negative numbers from the array, and the reduce() method calculates their sum. Finally, we divide the sum by the count of filtered elements to get the average.

Steps for Implementation

  • Use filter() to create an array of negative numbers only
  • Use reduce() to calculate the sum of negative numbers
  • Calculate average by dividing sum by the count of negative numbers
  • Handle edge case when no negative numbers exist

Example

function findAverageOfNegatives(arr) {
    // Filter negative numbers
    let negatives = arr.filter(num => num < 0);
    
    if (negatives.length === 0) {
        return 0; // No negative numbers
    }
    
    // Calculate sum using reduce
    let sum = negatives.reduce((acc, num) => acc + num, 0);
    
    return sum / negatives.length;
}

let arr1 = [1, -3, 4, -2, -5];
let arr2 = [7, -4, -8, 5, -6, 10];
let arr3 = [1, 2, 3, 4]; // No negatives

console.log("Array 1 average:", findAverageOfNegatives(arr1));
console.log("Array 2 average:", findAverageOfNegatives(arr2));
console.log("Array 3 average:", findAverageOfNegatives(arr3));
Array 1 average: -3.3333333333333335
Array 2 average: -6
Array 3 average: 0

Time Complexity: O(N) - Two passes through the array (filter + reduce)
Space Complexity: O(K) - Where K is the number of negative elements

Comparison

Method Time Complexity Space Complexity Code Readability
For Loop O(N) O(1) Good
Filter & Reduce O(N) O(K) Excellent

Conclusion

Both approaches effectively calculate the average of negative numbers in an array. The for loop method is more memory efficient, while the filter and reduce approach offers better code readability and follows functional programming principles. Choose based on your specific requirements and coding style preferences.

Updated on: 2026-03-15T23:19:01+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements