Count Animals from Legs and Population Ratios
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests setting up and solving a system of linear equations with fixed ratios between variables.
You're observing three species in an enclosure: ants, chickens, and cows. Each species has a fixed number of legs, and their populations follow strict proportional relationships.
Given information:
- Ants have 6 legs each
- Chickens have 2 legs each
- Cows have 4 legs each
- The number of ants is exactly twice the number of chickens
- The number of cows is exactly twice the number of ants
Your task: given the total count of legs observed, determine how many ants are present.
If the given leg count cannot be produced by any valid population (where all animal counts are positive integers satisfying the ratio constraints), return 0.
Constraints
0 ≤ total_legs ≤ 10^6- All animal populations must be positive integers
- The ratio constraints must be exactly satisfied
Examples
Example 1
Input: count_ants_from_legs(570)
Output: 38
Example 2
Input: count_ants_from_legs(30)
Output: 2
Example 3
Input: count_ants_from_legs(150)
Output: 10
Hints
Hint 1
Express all populations in terms of a single variable, then set up an equation for the total leg count.
Hint 2
The ratios form a chain: chickens determine ants, and ants determine cows. Choose the base of this chain as your variable.
Hint 3
Once you solve for the base variable, check whether it produces a positive integer for all three populations.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The ratio constraints create a dependency chain where all populations are determined by a single base variable. Since ants depend on chickens and cows depend on ants, we express everything in terms of the chicken count.
Let the number of chickens be c. Then:
- Number of ants =
2c(given constraint) - Number of cows =
2 × (2c) = 4c(twice the ants)
Now we calculate the total leg contribution from each species:
- Chickens contribute:
c × 2 = 2clegs - Ants contribute:
2c × 6 = 12clegs - Cows contribute:
4c × 4 = 16clegs
Summing these: 2c + 12c + 16c = 30c total legs.
Therefore, total_legs = 30c, which gives us c = total_legs / 30.
For a valid solution to exist:
total_legsmust be divisible by 30 (socis an integer)cmust be positive (at least one of each animal)
If these conditions hold, the number of ants is 2c = total_legs / 15.
Why This Works
The fixed ratios collapse the three-variable system into a single degree of freedom. Every valid population is simply a scaled version of the minimal family: 1 chicken, 2 ants, 4 cows (30 legs). The total leg count must be a multiple of 30, and dividing by 30 recovers the scaling factor.
Complexity
- Time: O(1) – constant arithmetic operations
- Space: O(1) – no additional storage needed
Solution
def count_ants_from_legs(total_legs: int) -> int:
# Express everything in terms of chickens (c):
# ants = 2c, cows = 2 * (2c) = 4c
# Total legs: 2c + 6(2c) + 4(4c) = 2c + 12c + 16c = 30c
# So total_legs = 30c, meaning c = total_legs / 30
# Ants = 2c = total_legs / 15
if total_legs <= 0 or total_legs % 30 != 0:
return 0
return total_legs // 15