Day 1

100 Days to Amazon

Unique Triplets

Hey Guys, From today. I am challenging myself to solve 1 problem each day for 100 days. Used up your free stories Here is my Friend Link.

I am naming this series as 100 Days to Amazon.

All these problems are taken from the following e-book. 🎓

This is completely free 🆓 if you have an amazon kindle subscription.

Begin Your ascent to greatness🚀

Keep reading and you can find out the companies that have actually asked these questions in real interviews.

Day 1– 3 Sum 🏁

AIM:🏹

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0?

Find all unique triplets in the array which gives the sum of zero

Code👇

Algorithm👨‍🎓

Create two Lists and one TreeSet. Sort the given input array. using Arrays.sort() function. Create a for loop that iterates from 0 to n-2. Create two variables l and r. l is to iterate from the left and r is to iterate from the end. The logic behind this approach is after we have sorted that data. At any moment of time if the triplet sum is greater than the target value. Then we should search for the triplet before the given indices. Similarly, if the sum value is lesser than the target we should increase the iterator from the left. Now if the triplet equals the sum. Add the elements in a list (row).📃 Add that particular row into the tree set. When we save it in tree set. if we use contains function it results in O(1). So that retrieval it is more efficient. After looping ➰for all each iteration. Return the Result.🔚

Conclusion:

Don’t forget to hit the follow button✅to receive updates when we post new coding challenges. Tell us how you solved this problem in the comments section below. 🔥 We would be thrilled to read them. ❤

I have published an ebook. A compilation of 100 Java(Interview) Programming problems which have been solved. (HackerRank) 🐱‍💻

This is completely free 🆓 if you have amazon kindle subscription.

Companies

Other Data Structure Articles you may like: