KatInterview

Determine Animal Quantities in a Barn

Math & Bit ManipulationAuto-graded · 17 test cases

Last updated

Find the integer count of each animal species given the total legs and proportional species constraints.

You are tasked with evaluating the exact population breakdown in a barn containing chickens, ants, and cows.

The populations follow fixed proportional relations:

  • The count of ants is twice the count of chickens.
  • The count of cows is twice the count of ants.

Standard leg counts per individual of each species are:

  • Chickens: 2 legs
  • Ants: 6 legs
  • Cows: 4 legs

Given an integer total_legs, determine if a non-negative integer count exists for each species that satisfies both the proportional rules and the exact leg count.

If a valid configuration exists, return a dictionary containing keys "chickens", "ants", and "cows" mapped to their respective integer counts. If no valid non-negative integer solution exists, return None.

Constraints

  • $0 \le \text{total_legs} \le 10^9$

Examples

Example 1

Input: solve_barn_counts(570)

Output: {'chickens': 19, 'ants': 38, 'cows': 76}