Google Coding Question – Solved

4 Live
Balanced mixture Given A number of powerful resources and B number of weak resources. You have to select a balanced mixture of these resources. A balanced mixture has C resources containing no less than 4 powerful resources and no less than one weak resource. You are given integers A, B, and C. Each powerful and weak resource is unique in itself. Hence, two mixtures with the same number of powerful and weak resources are considered different if at least one of the resources is different in the 2 mixtures. Print the number of ways of selecting a balanced mixture with the given number of resources. Function description Complete the function Balanced(). This function takes the following 3 parameters and returns the required answer: - A: Represents the number of powerful resources - B: Represents the number of weak resources - C. Represents the number of resources in a balanced mixture Input format for custom testing Note: Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code - The first line contains an integer A denoting the number of powerful resources. - The second line contains an integer B denoting the number of weak resources - The third line contains an integer C denoting the number of resources in a balanced mixture

Asked in: Google

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from functools import lru_cache
from math import comb
def Balanced (A, B, C):
    ans = 0
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, begin by deeply understanding the constraints and requirements outlined. You're given three integers: A, B, and C. Here, A is the number of available powerful resources, B is the number of available weak resources, and C is the number of total resources that a balanced mixture must contain.

The definition of a "balanced mixture" is crucial. For a mixture to be considered balanced, it must consist of:
1. No less than 4 powerful resources.
2. No less than 1 weak resource.
3. Exactly C resources in total.

With these three constraints, you are tasked with determining in how many ways you can choose such a mixture from the available resources. It's important to realize that each resource is distinct or unique, which means that combinations involving different individual resources (even if they have the same count) are treated as different. This uniqueness means the problem is about combinatorics — counting the number of valid combinations under certain rules.

Now, begin thinking about how you can explore all valid distributions of powerful and weak resources that satisfy the constraints. Suppose you’re building a mixture of exactly C resources. To meet the requirement of at least 4 powerful resources and at least 1 weak resource, the number of powerful resources (let’s call it `p`) in a valid combination must range from 4 up to a maximum that depends on both A and C. Why? Because you can’t use more powerful resources than you have (A), and the total number of resources in the mixture must be exactly C. Similarly, the number of weak resources in any such combination (let’s call it `w`) will be `C - p`, because the total number of resources in the mixture is fixed at C.

So your first task is to iterate over all possible values of `p` (the count of powerful resources) from 4 up to the smaller of A and C - 1. You use `C - 1` as the upper limit because at least one weak resource is needed (so `w = C - p` must be at least 1). For each valid `p`, compute the corresponding `w = C - p`. Then check that `w` is no more than B, because you can’t select more weak resources than you have.

For every valid pair (p, w), where both counts satisfy the constraints, you need to count the number of ways to select p powerful resources from A available ones and w weak resources from B available ones. Because the resources are unique, you’re counting combinations, not permutations. So you think in terms of “n choose k” — that is, the binomial coefficient which gives the number of ways to choose k elements from a set of n elements without regard to order.

To compute the total number of balanced mixtures, iterate through all valid values of p as described, compute the valid corresponding w, ensure both p and w meet the constraints, and then compute the number of ways to choose p powerful and w weak resources. Multiply these two counts together to get the number of combinations for that pair, and accumulate these values across all valid (p, w) pairs.

An important aspect of your thinking should also involve ensuring efficiency and correctness in computing the combinations. Although we’re not focusing on optimization here, in a real scenario you'd want to handle large numbers properly and avoid recomputing factorials redundantly. But for now, the main goal is building the correct logical flow.

You should also think about the edge cases. For example, if A < 4 or B < 1 or C < 5, then it’s impossible to form a balanced mixture at all. Another edge case is when C is greater than A + B, meaning the total number of resources needed exceeds the total available resources. In all these cases, the function should return 0 because no mixture is possible.

To summarize your approach: loop through all possible values for the number of powerful resources from 4 up to the minimum of A and C - 1. For each value, compute the corresponding number of weak resources needed to complete the mixture to size C. Check whether the weak resource count is valid (between 1 and B). For each valid (p, w) pair, compute the combinations of selecting p from A and w from B, and add the product to your running total. This total will be the number of ways to form a balanced mixture under the given constraints.
```


Related Questions