Skip to content

Commit ea20210

Browse files
authored
Add dominantIndices function to count dominant indices
Implement function to count dominant indices based on average of subsequent elements.
1 parent 87944f6 commit ea20210

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
6+
var dominantIndices = function(nums) {
7+
let count = 0;
8+
9+
for (let i = 0; i<nums.length; i++){
10+
let sum = 0;
11+
let avg = 0;
12+
for (let j = i+1; j<nums.length; j++){
13+
sum += nums[j];
14+
}
15+
avg = sum / (nums.length - i - 1);
16+
if (avg != 0 && nums[i] > avg){
17+
count++;
18+
}
19+
}
20+
return count;
21+
};

0 commit comments

Comments
 (0)