
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Average of All Negative Numbers in an Array using JavaScript
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.
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 in an array
Below are different approaches to Finding the Average of Negative Numbers in an Array:
Using Brute Force 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. 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
- Define a function that takes an array as the input parameter.
- Initialize two variables, sum and count, to store the sum of negative elements and the count of negative numbers, respectively.
- Use a loop to traverse and add all negative numbers and store their count.
- Return zero if there are no negative elements in the array.
- Return sum divided by count.
Implementation Code
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; } return sum / count; } let arr = [1, -3, 4, -2, -5]; console.log("Average of negative numbers:", findAverageOfNegatives(arr));
Output
Average of negative numbers: -3.33
Time Complexity: O(N)
Space Complexity: O(1)
Using Filter and Reduce Approach
In this approach, we use two of JavaScript's built-in methods, filter() and reduce(). The filter() method is used to select only the negative numbers from the array, and then the reduce() method is used to calculate their sum. Finally, we return by dividing the sum by the length of the filtered array to get the average.
Steps for Implementation
- Define a function and pass the array as a parameter.
- Filter out negative elements and calculate the sum of negative numbers.
- Return zero if there are no negative elements in the array.
- Return the average.
Implementation Code
function findAverageOfNegatives(arr) { let negatives = arr.filter(num => num < 0); let sum = negatives.reduce((acc, num) => acc + num, 0); if (negatives.length === 0) { return 0; // Return 0 if there are no negative numbers } return sum / negatives.length; } let arr = [1, -3, 4, -2, -5]; console.log("Average of negative numbers:", findAverageOfNegatives(arr));
Output
Average of negative numbers: -3.33
Time Complexity: O(N)
Space Complexity: O(1)