Maximum triplets containing atleast one x and one y
Last Updated :
05 Apr, 2023
Given counts of x, y, and z, the task is to find the maximum number of triplets that can be made from the count of x, y, and z such that one triplet contains at least one x and at least one y. It is not necessary to use all x, y, and z.
Examples:
Input: countX = 2, countY = 1, countZ = 3
Output: 1
Explanation: The first triplet contains one x, one y, and one z.
Input: countX = 3, countY = 4, countZ = 1
Output: 2
Explanation: The first triplet contains one x, one y, and one z.
The second triplet contains two x and one y.
Approach: To solve the problem follow the below idea:
We can solve this problem using binary search because the range from 0 to min(CountX, CountY) is monotonic increasing. Because our answer lies in this range and can’t be greater than min(CountX, CountY) .
Illustration:
Let take example 2: countX = 3, countY = 4, countZ = 1
- Min(countX, CountY) = 3. So our search range will be from 0 to 3.
- First, L = 0 and R = 3, mid = 1; we can find that we can make 1 triplet using one x, one y, and one z.so we will move L to mid + 1. Then update our answer to 1.
- Now, L = 2 and R = 3, mid = 2; we can find that we can make 2 triplets using three x, two y, and one z.so we will move L to mid + 1. Then update our answer to 2.
- Now, L = 3 and R = 3, mid = 3; we see that it is not possible to make 3 triplets using 3 counts of x, 4 counts of y, and 1 count of z . so we will move R to mid -1. Then don’t update our answer because the condition is not satisfy.
- Now, L = 3 and R = 2, Since, L > R, our binary search is complete, and the largest possible answer is 2.
Steps were to follow this problem:
- We will apply a binary search whose range is from 0 to min(countX, countY).
- Then Each time find the middle element of the search space.
- If the middle element satisfies a condition we can make a middle number of triplets such that one triplet contains at least one x and at least one y. Then we will update our search range from mid+1 to r ( where r is the last index of the previous search range) and update our answer to mid.
- If the middle element isn’t satisfied a condition that we can’t make a middle number of triplets such that one triplet contains at least one x and at least one y. Then we will update our search range from l to mid-1 ( where l is the firstindex of the previous search range).
- When the binary search is complete, return the answer ( last mid which is satisfying the condition).
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxTriplets( int x, int y, int z)
{
int l = 0;
int r = min(x, y);
int ans = 0;
while (l <= r) {
int mid = (l + r) / 2;
bool triplet = false ;
if (x >= mid and y >= mid)
{
int remx = x - mid;
int remy = y - mid;
int remaining = remx + remy + z;
if (remaining >= mid)
{
triplet = true ;
}
}
if (triplet) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
return ans;
}
int main()
{
int x = 2, y = 1, z = 3;
cout << maxTriplets(x, y, z) << "\n" ;
x = 3, y = 4, z = 1;
cout << maxTriplets(x, y, z) << "\n" ;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int maxTriplets( int x, int y, int z)
{
int l = 0 ;
int r = Math.min(x, y);
int ans = 0 ;
while (l <= r) {
int mid = (l + r) / 2 ;
boolean triplet = false ;
if (x >= mid && y >= mid)
{
int remx = x - mid;
int remy = y - mid;
int remaining = remx + remy + z;
if (remaining >= mid)
{
triplet = true ;
}
}
if (triplet) {
ans = mid;
l = mid + 1 ;
}
else {
r = mid - 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int x = 2 , y = 1 , z = 3 ;
System.out.println(maxTriplets(x, y, z));
x = 3 ;
y = 4 ;
z = 1 ;
System.out.println(maxTriplets(x, y, z));
}
}
|
C#
using System;
public class GFG {
public static int maxTriplets( int x, int y, int z)
{
int l = 0;
int r = Math.Min(x, y);
int ans = 0;
while (l <= r) {
int mid = (l + r) / 2;
bool triplet = false ;
if (x >= mid && y >= mid)
{
int remx = x - mid;
int remy = y - mid;
int remaining = remx + remy + z;
if (remaining >= mid)
{
triplet = true ;
}
}
if (triplet) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
return ans;
}
static public void Main()
{
int x = 2, y = 1, z = 3;
Console.WriteLine(maxTriplets(x, y, z));
x = 3;
y = 4;
z = 1;
Console.WriteLine(maxTriplets(x, y, z));
}
}
|
Python3
def maxTriplets(x: int , y: int , z: int ) - > int :
l = 0
r = min (x, y)
ans = 0
while l < = r:
mid = (l + r) / / 2
triplet = False
if x > = mid and y > = mid:
remx = x - mid
remy = y - mid
remaining = remx + remy + z
if remaining > = mid:
triplet = True
if triplet:
ans = mid
l = mid + 1
else :
r = mid - 1
return ans
if __name__ = = '__main__' :
x = 2
y = 1
z = 3
print (maxTriplets(x, y, z))
x = 3
y = 4
z = 1
print (maxTriplets(x, y, z))
|
Javascript
function maxTriplets(x, y, z) {
let l = 0;
let r = Math.min(x, y);
let ans = 0;
while (l <= r) {
let mid = Math.floor((l + r) / 2);
let triplet = false ;
if (x >= mid && y >= mid)
{
let remx = x - mid;
let remy = y - mid;
let remaining = remx + remy + z;
if (remaining >= mid)
{
triplet = true ;
}
}
if (triplet) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
let x = 2,
y = 1,
z = 3;
console.log(maxTriplets(x, y, z));
x = 3, y = 4, z = 1;
console.log(maxTriplets(x, y, z));
|
Time Complexity: O(log2n)
Auxiliary Space: O(1)
Efficient Approach: We can also solve this problem efficiently by making these observations:
- We can see that the maximum number of triplets can be made that contain at least one x and one y can’t be greater than the minimum count of x and y.
- If the count of z is greater than or equal to the minimum(count of x, count of y). So our answer will be minimum(count of x, count of y) because we have used our all x or y in making ‘a’ number of triplets where an equal to the minimum( count of x, count of y).
- If the count of z is less than the minimum(count of x, count of y), then our answer will be less than the minimum of the count of x and y and the answer will be the sum no. of triplets can be made using the count of x, y and z and no. of triplets that can be made using the count of x and y only such that one triplet contain at least one x and at least one y.
- Finally, return our final answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxTriplets( int x, int y, int z)
{
int mi = min(x, y), ans;
if (mi <= z)
{
ans = mi;
}
else
{
ans = z;
x -= z;
y -= z;
mi = min(x, y);
int ma = max(x, y);
if (mi * 2 <= ma) {
ans += mi;
}
else {
ans += (mi + ma) / 3;
}
}
return ans;
}
int main()
{
int x = 2, y = 1, z = 3;
cout << maxTriplets(x, y, z) << "\n" ;
x = 3, y = 4, z = 1;
cout << maxTriplets(x, y, z) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int maxTriplets( int x, int y, int z) {
int mi = Math.min(x, y), ans;
if (mi <= z)
ans = mi;
else
{
ans = z;
x -= z;
y -= z;
mi = Math.min(x, y);
int ma = Math.max(x, y);
if (mi * 2 <= ma) {
ans += mi;
} else {
ans += (mi + ma) / 3 ;
}
}
return ans;
}
public static void main(String[] args) {
int x = 2 , y = 1 , z = 3 ;
System.out.println(maxTriplets(x, y, z));
x = 3 ;
y = 4 ;
z = 1 ;
System.out.println(maxTriplets(x, y, z));
}
}
|
Python3
def maxTriplets(x, y, z):
mi = min (x, y)
ans = 0
if mi < = z:
ans = mi
else :
ans = z
x - = z
y - = z
mi = min (x, y)
ma = max (x, y)
if mi * 2 < = ma:
ans + = mi
else :
ans + = (mi + ma) / / 3
return ans
x, y, z = 2 , 1 , 3
print (maxTriplets(x, y, z))
x, y, z = 3 , 4 , 1
print (maxTriplets(x, y, z))
|
C#
using System;
namespace MaxTriplets
{
class GFG
{
static int maxTriplets( int x, int y, int z)
{
int mi = Math.Min(x, y);
int ans;
if (mi <= z)
{
ans = mi;
}
else
{
ans = z;
x -= z;
y -= z;
mi = Math.Min(x, y);
int ma = Math.Max(x, y);
if (mi * 2 <= ma)
{
ans += mi;
}
else
{
ans += (mi + ma) / 3;
}
}
return ans;
}
static void Main( string [] args)
{
int x = 2, y = 1, z = 3;
Console.WriteLine(maxTriplets(x, y, z));
x = 3; y = 4; z = 1;
Console.WriteLine(maxTriplets(x, y, z));
}
}
}
|
Javascript
function maxTriplets(x, y, z)
{
let mi = Math.min(x, y), ans;
if (mi <= z) {
ans = mi;
}
else {
ans = z;
x -= z;
y -= z;
mi = Math.min(x, y);
let ma = Math.max(x, y);
if (mi * 2 <= ma) {
ans += mi;
}
else {
ans += Math.floor((mi + ma) / 3);
}
}
return ans;
}
let x = 2, y = 1, z = 3;
console.log(maxTriplets(x, y, z));
x = 3, y = 4, z = 1;
console.log(maxTriplets(x, y, z));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...