Chi-Square Statistic

HardStatisticsMathArray

Description

Given an array of observed counts and a parallel array of expected counts, return the chi-square statistic: the sum over categories of (observed - expected)^2 / expected. Round the result to 4 decimal places.

Examples

Input:[10,20,30], [20,20,20]
Output:10
Explanation:

The chi-square statistic adds the squared gap between observed and expected counts, each scaled by its expected value.

Input:[18,22], [20,20]
Output:0.4
Explanation:

The chi-square statistic adds the squared gap between observed and expected counts, each scaled by its expected value.

Input:[30,10], [20,20]
Output:10
Explanation:

The chi-square statistic adds the squared gap between observed and expected counts, each scaled by its expected value.

Constraints

  • 1 ≤ length ≤ 10⁴
  • every expected count > 0

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.