# Solution For Merge Triplets To Form Target Triplet

Problem Statement:

You are given an array of the form target = [x, y, z] and an array of triplets triplets, where each triplet = [ai, bi, ci] describes the i-th triplet. You can select triplets and concatenate their elements (in the same order) if the elements in the concatenated result match the target.

Return true if it is possible to concatenate triplets from the array triplets to form the target, or false otherwise.

Solution:

The solution to the Merge Triplets To Form Target Triplet problem is a straightforward implementation problem. We need to check whether it is possible to concatenate the triplets from the array triplets to form the target array.

The first step in solving this problem is to sort the triplets array. Then, we can iterate over the sorted triplets array and check whether we can merge the current triplet with the previous triplet(s) to form the target array.

We can define a helper function `merge_triplets` that takes two triplets and returns the merged triplet if it is possible to merge the two triplets, otherwise it returns None. We can use this helper function to merge the sorted triplets array and form the target array.

The `merge_triplets` function checks whether it is possible to merge two triplets by comparing their first, second, and third elements with the corresponding elements in the target array. If all three elements match, it returns the merged triplet, otherwise it returns None.

Algorithm:

1. Initialize an empty list `triplets_matched` to store the triplets that match the target.
2. Sort the triplets array.
3. Iterate over the sorted triplets array:
a. If the current triplet matches the target, append it to the `triplets_matched` list.
b. Otherwise, try to merge the current triplet with the previous triplet(s) that match the target by calling the `merge_triplets` function.
c. If the merged triplet matches the target, append it to the `triplets_matched` list.
d. Otherwise, continue the iteration.
4. If the length of the `triplets_matched` list is equal to 3, return True, otherwise return False.

Code:

“` python
from typing import List

def merge_triplets(t1: List[int], t2: List[int]) -> List[int]:
“””Merge two triplets if possible, otherwise return None.”””
x1, y1, z1 = t1
x2, y2, z2 = t2

``````if x2 <= x1 and y2 <= y1 and z2 <= z1:
return [x1, y1, z1]
elif x1 <= x2 and y1 <= y2 and z1 <= z2:
return [x2, y2, z2]

return None
``````

def merge_triplets_to_form_target_triplet(triplets: List[List[int]], target: List[int]) -> bool:
“””Merge triplets to form target triplet.”””
triplets_matched = [] triplets.sort()

``````for triplet in triplets:
if triplet == target:
triplets_matched.append(triplet)
elif len(triplets_matched) > 0:
prev_matched = triplets_matched[-1]
merged = merge_triplets(prev_matched, triplet)
if merged == target:
triplets_matched.append(merged)

return len(triplets_matched) == 3
``````

“`

Time Complexity:

The time complexity of the `merge_triplets_to_form_target_triplet` function is O(n^2), where n is the length of the triplets array. The function calls the `merge_triplets` function for each pair of triplets, resulting in an O(n^2) time complexity.

Space Complexity:

The space complexity of the `merge_triplets_to_form_target_triplet` function is O(1) because it only uses a constant amount of additional space. The `merge_triplets` function also uses a constant amount of additional space. Therefore, the overall space complexity of the solution is O(1).

## Step by Step Implementation For Merge Triplets To Form Target Triplet

```/*
Given an array of distinct integers arr and an integer target.

A triplet (arr[i], arr[j], arr[k]) is a set of three distinct indices (i, j, k) such that the elements at those indices are (arr[i], arr[j], arr[k]) respectively.

Return true if there exists a triplet (arr[i], arr[j], arr[k]) such that arr[i] + arr[j] + arr[k] == target, otherwise, return false.

Example 1:

Input: arr = [1,2,3,4,5], target = 8
Output: true
Explanation: There are 2 triplets: (1,2,5) & (3,4,5)
*/

public boolean findTriplets(int[] arr, int target) {
Arrays.sort(arr);
for (int i = 0; i < arr.length - 2; i++) {
int left = i + 1, right = arr.length - 1;
while (left < right) {
int sum = arr[i] + arr[left] + arr[right];
if (sum == target)
return true;
else if (sum < target)
left++;
else
right--;
}
}
return false;
}```
```def findTriplets(arr, target):

# sort array

arr.sort()

# fix first element as A[i]

for i in range(n-2):

# find pair in subarray A[i+1..n-1]

# with sum equal to "target - A[i]"

s = i + 1

e = n - 1

while (s < e):

if (arr[i] + arr[s] + arr[e] == target):

# print elements if it's sum is

# equal to target

print(arr[i], arr[s], arr[e])

s = s + 1

e = e - 1

# Else either move 's'

# or 'e'

else:

if (arr[i] + arr[s] + arr[e] < target):

s = s + 1

else:

e = e - 1```
```var mergeTripletsToFormTargetTriplet = function(triplets, target) {
// your code here
};```
```Given an array of distinct integers arr and an integer target, return a 3-tuple [x, y, z] such that x + y + z = target and x, y, z are distinct elements of arr.

If there are multiple such 3-tuples, you may return any one of them.

Example 1:

Input: arr = [1,2,3,4,5], target = 8
Output: [2,3,5]
Explanation: The triplets [2,3,5], [2,5,3], and [3,2,5] all sum to 8.
Example 2:

Input: arr = [1,1,1,2,5], target = 4
Output: [1,1,2]
Explanation: The triplets [1,1,2], [1,2,1], and [2,1,1] all sum to 4.
Example 3:

Input: arr = [1,3,2,6,7], target = 7
Output: [1,2,7]

Constraints:

3 <= arr.length <= 5000
1 <= arr[i] <= 1000
1 <= target <= 10^8```
```Given an array of distinct integers arr, find all triplets in the array that sum to a given target.

//Solution:

public List 	> findTriplets(int[] arr, int target)
{
List 	> triplets = new List 	>();

for (int i = 0; i < arr.Length - 2; i++)
{
if (i > 0 && arr[i] == arr[i - 1])
{
continue;
}

for (int j = i + 1; j < arr.Length - 1; j++)
{
if (j > i + 1 && arr[j] == arr[j - 1])
{
continue;
}

int k = j + 1;
int sum = target - arr[i] - arr[j];

while (k < arr.Length && arr[k] < sum)
{
k++;
}

if (k != arr.Length && arr[k] == sum)
{
triplets.Add(new List { arr[i], arr[j], arr[k] });
}
}
}

return triplets;
}```
Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]