Average of all elements

 Question:--

 

Find the counts of elements of an unsorted integer array which are equal to the average of all elements of that array.

Ex:
Input Case 1:  
input: [2,2,2,2,2] 
output:  5
solution : 2+ 2+ 2+ 2+ 2 = 10/5 ==> 2
it contain five 2 element

Input Case 2:  
input: [ 1,3,2,4,5]  
output:  1
solution : 1+ 3+ 2+ 4+ 5 = 15/5 ==> 3
it contain one 3 element
 
 
 
JavaScript Solution:---

function avg(a,n){
    var num = []
    var sum = 0
    for (i=0; i < n; i++){
       sum+=a[i]
    }
    // console.log(sum)
    // console.log(sum/n)
    // console.log(sum/n)
    //  return sum
        
    for (var i of a){
        if (i == sum/n){
            num.push(i)
            
        }
    }
    console.log(num.length)
}


var a = [2,2,2,2,2]
var n = a.length

avg(a,n)

Comments

Popular posts from this blog

XAMPP, SQL BASIC COMMANDS

The Minion Game Hackerrank Solution

Arrays - DS | HackerRank Solutions