Skip to main content

Command Palette

Search for a command to run...

3Sum-LeetCode

Published
2 min read
3Sum-LeetCode
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

LeetCode Problem - 15

import java.util.*;

class Solution {
    // Method to find all unique triplets in the array 'nums' that sum up to zero
    public List<List<Integer>> threeSum(int[] nums) {
        // If array is null or has less than 3 elements, return an empty list
        if (nums == null || nums.length < 3)
            return new ArrayList<>();

        Arrays.sort(nums);  // Sort the array to efficiently find triplets
        Set<List<Integer>> answer = new HashSet<>();  // Set to store unique triplets

        // Iterate through the array to find triplets
        for (int i = 0; i < nums.length - 2; i++) {
            int left = i + 1;  // Pointer for the element after 'nums[i]'
            int right = nums.length - 1;  // Pointer for the last element in the array

            // Use two pointers technique to find pairs that sum up to -(nums[i])
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];  // Calculate the sum of triplet

                if (sum == 0) {
                    // If sum is zero, add the triplet to the answer set
                    answer.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    left++;  // Move the left pointer to the right to find next valid triplet
                    right--;  // Move the right pointer to the left to find next valid triplet
                } else if (sum < 0) {
                    // If sum is less than zero, move the left pointer to increase the sum
                    left++;
                } else {
                    // If sum is greater than zero, move the right pointer to decrease the sum
                    right--;
                }
            }
        }

        // Convert set of lists to a list of lists and return as the final answer
        return new ArrayList<>(answer);
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.