Triplety 1 0 3

broken image


  1. Triplet 1 0 3 1
  2. Triplet 1 0 3 Scale

1 0 1 -3 2 1 Time Complexity: O(n 2) Auxiliary Space: O(n) Method 3 (Sorting: O(n 2)) The above method requires extra space. We can solve in O(1) extra space. The idea is based on method 2 of this post. Sort all element of array 2. Run loop from i=0 to n-2. The first loops will run from 0 to n-3 and second loop from i+1 to n-2 and the third loop from j+1 to n-1. The loop counter represents the three elements of the triplet. Check if the sum of elements at i'th, j'th, k'th is equal to zero or not. If yes print the sum else continue. Below is the implementation of the above approach. Creates a criterion that measures the triplet loss given an input tensors x 1 x1 x 1, x 2 x2 x 2, x 3 x3 x 3 and a margin with a value greater than 0 0 0. This is used for measuring a relative similarity between samples. A triplet is composed by a, p and n (i.e., anchor, positive examples. Photolysis (λ 472 nm) of 2-diazo-3-pentyne (11) affords triplet 1,3-dimethylpropynylidene (MeC 3 Me, 3 3), which was characterized spectroscopically in cryogenic matrices. The infrared, electronic absorption, and electron paramagnetic resonance spectra of MeC 3 Me (3 3) are compared with those of the parent system (HC 3 H) to ascertain the effect of alkyl substituents on delocalized carbon.

In find a triplet that sum to a given value problem we have given an array of integers (positive and negative numbers) and you have to find a triplet (3 elements) in array whose sum is equal to the given value X.

Input Format

First-line containing two integer values N and X.

Second-line containing N integer values or input array.

Output Format

Print any triplet whose sum given to X. If there are more than one triplets found print any one of them. If no such triplet found print 0.

Triplet 1 0 3 1

Constraints

  • 1<=N<=100000
  • -1e9<=a[i]<=1e9
  • -1e9<=X<= 1e9

Examples

Input

6, 10

1, 5, 7, 3, 4, 2
Output

1 7 2

Input

8, 11

-1, 12, 5, 7, -2, -4, 5, 1
Output

-2 1 12

Input

8, 0

-1, 12, 5, 7, -2, -4, 5, 1
Output

-4 -1 5

Approach Using 3 Pointers for Find a Triplet That Sum to a Given Value

View all
    1. Sort the given array first which will give us time complexity of (n log n)
    2. Take 3 pointers p1, p2, p3 where p1 is pointing to start of the array, p2 will be pointing next to p1 and p3 will be pointing to the last index of an array.
    3. Have a loop1 to move (Increment) p1 till p1 < p3
    4. For each iteration of loop1 have loop2 to move (Increment) p2 till p2 < p3
    5. If sum of Array[p1] + Array[p2] + Array[p3] > k then Decrement p3
    6. If sum of Array[p1] + Array[p2] + Array[p3]k then Print the elements
    7. p2 = p1 +1 and p3 = Array.length – 1

C++ Program for Find a Triplet That Sum to a Given Value

Complexity Analysis for Find a Triplet That Sum to a Given Value

Triplet 1 0 3 Scale

Time Complexity

O(n*n*n) where n is the length of the array. Here we use 3 pointer method to traverse in the array. In the worst case the loop will run N*N*N times.

Space Complexity

O(1) because here we don't use any auxiliary space here.

Approach 2 for Find a Triplet That Sum to a Given Value

Here also we need to find the triplets from an array whose sum is equal to the given number.

Algorithm

1. First sort the input array
2. Fix the first element as arr[i], where i ranges from 0 to N-2

3. After fixing the first element, for finding the next two elements, take two pointer like variables ( j = i+1, k= N-1) and traverse the algorithm for finding the sum in sorted array.

4. while j is less than k Add the elements at the given indexes ie, arr[i] + arr[j] + arr[k] if Triplet sum is equal to the value X, print the three elements else if Triplet sum is less than the value X, then increase the j value else decrease the value of k.

C++ Program for Find a Triplet That Sum to a Given Value

Complexity Analysis

Time Complexity

O(N2) where N is the size of the array. Here we fix I and then use two pointer methods for j and k values. This will take N*N time in the worst case.

Space Complexity

Ic3d 5 5 64. O(1) because we don't use any auxiliary space.





broken image