Skip to content

Commit 8f71372

Browse files
author
hasibulislam999
committed
Statistics from a Large Sample problem solved
1 parent 0700441 commit 8f71372

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Title: Statistics from a Large Sample
3+
* Description: You are given a large sample of integers in the range [0, 255]. Since the sample is so large, it is represented by an array count where count[k] is the number of times that k appears in the sample.
4+
* Author: Hasibul Islam
5+
* Date: 04/04/2023
6+
*/
7+
8+
/**
9+
* @param {number[]} count
10+
* @return {number[]}
11+
*/
12+
/**
13+
* @param {number[]} count
14+
* @return {number[]}
15+
*/
16+
var sampleStats = function (count) {
17+
let res = new Array();
18+
for (let i = 0; i < count.length; i++) {
19+
if (count[i] > 0) {
20+
res.push(i);
21+
break;
22+
}
23+
}
24+
for (let i = count.length - 1; i >= 0; i--) {
25+
if (count[i] > 0) {
26+
res.push(i);
27+
break;
28+
}
29+
}
30+
let sum = count.reduce((acc, cur, ind) => acc + ind * cur, 0);
31+
let len = count.reduce((acc, cur, ind) => acc + cur, 0);
32+
res.push(sum / len);
33+
34+
if (len % 2 == 0) {
35+
let cur = 0,
36+
prev = 0;
37+
for (let i = 0; i < count.length; i++) {
38+
if (len - (cur + count[i]) > len / 2 - 1) {
39+
if (count[i] > 0) prev = i;
40+
cur += count[i];
41+
} else {
42+
if (len - cur == len / 2) res.push((prev + i) / 2);
43+
else res.push(i);
44+
break;
45+
}
46+
}
47+
} else {
48+
let cur = 0;
49+
for (let i = 0; i < count.length; i++) {
50+
if (len - (cur + count[i]) >= len / 2) {
51+
cur += count[i];
52+
} else {
53+
res.push(i);
54+
break;
55+
}
56+
}
57+
}
58+
59+
res.push(count.indexOf(Math.max(...count)));
60+
61+
return res;
62+
};
63+
64+
console.log(sampleStats([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));

0 commit comments

Comments
 (0)