Programming is a vast concept. From DSA to OOPs, the classes, functions, algorithms and approaches seem to have absolutely no end!

That is why, the sheer complexity of coding concepts make it really difficult to cover them all within a course of 2 months before the technical interviews.

One such concept that we shall be discussing today is the triplet sum in array. An array consists of triplets that can be hard to locate.

Which algorithms can be used becomes the main question! But fear not because we are here to save your time. 

Read the entire blog to find out how we would tackle this problem! \

 

What Do You Mean By A Triplet In An Array?

Now, the concept of triplets in an array manifests the presence of elements with three different indices that are usually represented by i, j and k.

The triplet sum in array can essentially be figured out by running the three nested loops algorithm for the entire size/structure of the given array.

Now, in order to detect the location of triplets in an array, we can apply three different algorithms. 

Let's find out how in our next section! 

 

How To Figure Out Triple Sum In A Given Array? 

Before discussing the algorithms, let's have a look at the problem statement:

You have been given an Array and a said value. Now, your task is to figure out whether there is a triplet within this array and, if the value of the triplet equals the given value. 

If you find the triple sum in the array, you can print the specific triplet. Also, return true if the statement turns out to be correct or else you can return false.

 

Solving the problem statement

There are essentially three different approaches that we can apply in order to find triplets within an array:

  • Naive Approach
  • Sorting method to increase efficiency
  • Hashing algorithm

Now, let's discuss all of these methods or approaches in detail.

 

Method 1: The Naive Approach for solving the triple sum array based problem

This approach basically focuses on generating all the available triplets within the array so that we can compare the sum of the triplets to the given value collectively. This approach proves to increase efficiency for the entire program.

Now, as discussed in the definition for triple sum in array the naive approach uses three nested loops for implementing the codes for the program.

Here is the algorithm for applying the three nested loop approach.

Algorithm:

  •  Suppose you have been given an array of the length h and, a sum of 6
  • You have to start with creating three different nested loops that will run from the beginning till the end of the array. 
  • The first loop shall begin from i while the second will begin from i+1 and, it will run till the end to j i.e the last element of the second loop. 
  • Finally, the third loop will begin from j+1 and it will reach its end once it attains the k element.
  • These three loops are representative of the 3 triplets index elements that are present within an array. 
  •  Next up, you will have to find the sum of all the three elements i.e the ith, the jth and the kth elements within the array.
  • If found that the sum is equal to the given value you can go ahead and print the array.
  • If there are no triplets found, then print the same after running the program.

Time Complexity for the problem:

O(n3)

 

Method 2: Using the sorting method to increase the efficiency of the program

Another effective method that can be applied for finding the triple sum in array is the sorting algorithm. Both the efficiency and time complexity of the entire program can be improved by a landslide.

The approach basically insists on using a two pointer technique. You can start by traversing the array and fixing the beginning element of the said triplet that is present within the array. 

Now, the next two pointers can be used for figuring out whether the given array consists of a pair that equals x - array[i]. 

One of the best qualities of the two pointer technique is that it follows a linear approach for finding the triplets hence, this approach is relatively time efficient that the three nested loops.

Check out the algorithm as follows.

Algorithm:

  • Firstly, start with sorting the given array.
  • Loop the entire length of the array and fix the beginning element of any possible triplet that can be found, arr[i].
  • Afterwards, you can fix two different pointers each at i + 1 and n - 1. 
  • Next up, you may consider the sum of the allotted pointers.
  • You can increment the beginning pointer if found that the sum is lesser than the given sum value.
  • If bigger, you need to decrease the last pointer in order to reduce the sum.
  • If equal, you can go ahead and print the Triplet.

Time complexity for the problem:

O(N^2)

 

Method 3: Using the Hashing Algorithm

Finally, for finding the triple sum in the array, we will be employing the hashing algorithm. Although this algorithm is known to occupy extra space in memory, it is even more time efficient than the two-pointers approach.

This algorithm involves running two loops for the entire array. One from the beginning till the end while the other from the inner loop will run from the i+1 element till the last element of the array.

Moving forward, you can create a hashmap for storing all the elements from i+1 to j-1.

Have a look at the algorithm for this approach:

  • Start by traversing the entire array.
  • Implement a hashmap for storing all elements
  • Run the second loop from i+1 till the last element of the array.
  • Find out the sum of the values stored within the hashmap.
  • If it turns out equal to the given value, return the triplets.

Time Complexity for the problem:

O(N^2)

 

Wrapping Up

From Java to Python, nearly all the programming languages give preference to arrays. Afterall, it is the basic fundamental of programming!Now that you have bagged the concept of finding triple sum in an array, next up, we would recommend practicing k sorted array problems from our other blogs.