Given counts of **x, y,** and **z, **the task is to find nan **maximum number of triplets** that tin beryllium made from nan count of x, y, and z specified that **one triplet contains astatine slightest 1 x and astatine slightest 1 y**. It is not basal to usage each x, y, and z.

**Examples**:

**Input**: countX = 2, countY = 1, countZ = 3**Output**: 1**Explanation**: The first triplet contains 1 x, 1 y, and 1 z.

**Input**: countX = 3, countY = 4, countZ = 1 **Output**: 2**Explanation**: The first triplet contains 1 x, 1 y, and 1 z.

The 2nd triplet contains 2 x and 1 y.

**Approach: **To lick nan problem travel nan beneath idea:** **

We tin lick this problem utilizing binary search because nan **range from 0 to min(CountX, CountY)** is monotonic increasing. Because our reply lies successful this scope and can’t beryllium greater than min(CountX, CountY) .

**Illustration:**

Let return illustration 2: countX = 3, countY = 4, countZ = 1

- Min(countX, CountY) = 3. So our hunt scope will beryllium from 0 to 3.
- First, L = 0 and R = 3, mid = 1; we tin find that we tin make 1 triplet utilizing 1 x, 1 y, and 1 z.so we will move L to mid + 1. Then update our reply to 1.
- Now, L = 2 and R = 3, mid = 2; we tin find that we tin make 2 triplets utilizing 3 x, 2 y, and 1 z.so we will move L to mid + 1. Then update our reply to 2.
- Now, L = 3 and R = 3, mid = 3; we spot that it is not imaginable to make 3 triplets utilizing 3 counts of x, 4 counts of y, and 1 count of z . truthful we will move R to mid -1. Then don’t update our reply because nan information is not satisfy.
- Now, L = 3 and R = 2, Since, L > R, our binary hunt is complete, and nan largest imaginable reply is 2.

Steps were to travel this problem:

- We will use a binary hunt whose scope is from
**0 to min(countX, countY)**. - Then Each clip find nan mediate constituent of nan hunt space.
- If nan mediate constituent satisfies a information we tin make a mediate number of triplets specified that 1 triplet contains astatine slightest 1 x and astatine slightest 1 y. Then we will update our hunt scope from mid+1 to r ( wherever r is nan past scale of nan erstwhile hunt range) and update our reply to mid.
- If nan mediate constituent isn’t satisfied a information that we can’t make a mediate number of triplets specified that 1 triplet contains astatine slightest 1 x and astatine slightest 1 y. Then we will update our hunt scope from l to mid-1 ( wherever l is nan firstindex of nan erstwhile hunt range).
- When nan binary hunt is complete, return nan reply ( past mid which is satisfying nan condition).

Below is nan implementation for nan supra approach:

## C++

**Time Complexity: **O(log2n)**Auxiliary Space:** O(1)

**Efficient Approach: **We tin besides lick this problem efficiently by making these observations:

- We tin spot that nan maximum number of triplets tin beryllium made that incorporate astatine slightest 1 x and 1 y can’t beryllium greater than nan minimum count of x and y.
- If nan count of z is greater than aliases adjacent to nan minimum
**(count of x, count of y).**So our reply will beryllium minimum**(count of x, count of y)**because we person utilized our each x aliases y successful making ‘a’ number of triplets wherever an adjacent to nan minimum( count of x, count of y). - If nan count of z is little than nan minimum
**(count of x, count of y)**, past our reply will beryllium little than nan minimum of nan count of x and y and nan reply will beryllium nan sum no. of triplets tin beryllium made utilizing nan count of x, y and z and no. of triplets that tin beryllium made utilizing nan count of x and y only specified that 1 triplet incorporate astatine slightest 1 x and astatine slightest 1 y. - Finally, return our last answer.

Below is nan implementation of nan supra approach:

## C++

**Time Complexity:** O(1)**Auxiliary Space:** O(1)

**Editor:**Naga

**Beritaja.com**at