The Hardy-Weinberg principle states that in a large, randomly mating population with
no selection, migration or mutation, genotype frequencies stay constant across generations. Testing a
locus against it is the standard quality-control filter in every genome-wide association study — a
marker that deviates sharply is usually a genotyping artefact rather than a real biological signal, and
GWAS pipelines routinely discard variants failing this test.
Statement
You are given the observed counts of three genotypes at a biallelic locus: homozygous
for the major allele (AA), heterozygous (Aa), and homozygous for the minor allele (aa).
- Compute the major allele frequency
pand minor allele frequencyq. - Compute the expected counts under Hardy-Weinberg equilibrium:
p^2 * N,2pq * N,q^2 * N. - Compute the chi-square statistic: the sum over the three genotypes of
(observed - expected)^2 / expected.
Print p on the first line to four decimal places, and the chi-square statistic on the second line to
four decimal places. Skip any genotype whose expected count is zero when summing the statistic.
Input — read from standard input
| Variable | Type | Description |
|---|---|---|
n_AA
line 1
|
int |
Observed count of homozygous major genotype
0 <= n_AA <= 100000
|
n_Aa
line 1
|
int |
Observed count of heterozygous genotype
0 <= n_Aa <= 100000
|
n_aa
line 1
|
int |
Observed count of homozygous minor genotype
0 <= n_aa <= 100000
|
These variables are already read for you in the starter code on the right.
Output
str major allele frequency p to four decimals on line 1, chi-square statistic to four decimals on line 2
Sample Cases
25 50 25
0.5000
0.0000
100 0 100
0.5000
200.0000
Submit also runs your code against 5 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.
Constraints
- The three counts are given on one line, separated by single spaces
- Total sample size
N = n_AA + n_Aa + n_aa >= 1 p = (2*n_AA + n_Aa) / (2*N)andq = 1 - p- Genotypes with an expected count of exactly zero are excluded from the chi-square sum
Further Reading
- Each individual carries two alleles, so the denominator when computing
pis2N, notN. - Compute expected counts as floats — rounding them to integers first will change the statistic.
- Guard the division: when
porqis 0, one expected count is 0 and that term must be skipped.