Trends

Maximum triplets containing atleast one x and one y

Trending 1 year ago
beritaja.com

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++

// C++ implementation of nan supra approach #include <bits/stdc++.h> using namespace std; // Function to find maximum number of // triplet that we tin make utilizing given // count of x, y and z int maxTriplets(int x, int y, int z) { int l = 0; // Intialize first scale of search // scope to 0 int r = min(x, y); // Intialize past scale of search // scope to min(x, y) int ans = 0; while (l <= r) { int mid = (l + r) / 2; // Finding mid of hunt range bool triplet = false; // Initially assume, we tin not // make mid number of triplets if (x >= mid and y >= mid) // Checking if count of x and y is // atleast mid { int remx = x - mid; // x remaining aft x mid // fixed successful mid triplet int remy = y - mid; // y remaining aft y mid // fixed successful mid triplet // Adding other count x and // y and count of z int remaining = remx + remy + z; if (remaining >= mid) // Checking we tin make // mid no. of triplet { triplet = true; } } // Checking if we tin make mid // number of triplet aliases not if (triplet) { // If we tin make mid number // of triplet ans = mid; // Update our answer l = mid + 1; // Update hunt scope to // [mid+1, R] because we tin make // atleast mid no. of triplets } other { r = mid - 1; // If we can't make mid number // of triplet update hunt range // to [l, mid-1] because we can // not make mid no. of triplets } } return ans; // Return answer } // Drive Code int main() { int x = 2, y = 1, z = 3; // Funtion telephone for trial lawsuit 1 cout << maxTriplets(x, y, z) << "\n"; x = 3, y = 4, z = 1; // Funtion telephone for trial lawsuit 2 cout << maxTriplets(x, y, z) << "\n"; return 0; }

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++

// C++ programme for nan supra approach #include <bits/stdc++.h> using namespace std; // Function to find nan maximum number of // triplets tin beryllium made that contain // slightest 1 x and 1 y int maxTriplets(int x, int y, int z) { int mi = min(x, y), ans; if (mi <= z) // If min(x, y) little than aliases adjacent to // count of z // Then triplets tin beryllium made at // astir min(x, y) { ans = mi; } else // If min(x, y) greater than count of z { ans = z; // Then first we adhd z to our // reply because we x -= z; // tin make atleast count // of z triplet y -= z; // Using each count of z mi = min(x, y); // min(x, y), aft making count // of z triplets int ma = max(x, y); // max(x, y), aft making count // of z triplets if (mi * 2 <= ma) { // If 2 times of min <= ma, so // we tin make astatine astir mi // triplets utilizing count of // x and y only. // In mi*2 <= ma, we can't use // each count of x and y. ans += mi; } other { // Otherwise we tin make (mi+ma)/3 // triplet utilizing count of x and y // only because if mi*2>ma we can // make triplet utilizing each x and y ans += (mi + ma) / 3; } } return ans; // Return last answer } // Driver's code int main() { int x = 2, y = 1, z = 3; // Funtion telephone for trial lawsuit 1 cout << maxTriplets(x, y, z) << "\n"; x = 3, y = 4, z = 1; // Funtion telephone for trial lawsuit 2 cout << maxTriplets(x, y, z) << "\n"; return 0; }

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

Editor: Naga



Read other contents from Beritaja.com at
More Source



close