Striver-SDE-Sheet

Repository containing solution for #SdeSheetChallenge by striver

View the Project on GitHub

3Sum

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.

O(N^3 Log m) Brute force

    for(i=0,n-1)
        for(j=i+1,n-1)
            for(k=j+1,n-1)
                a+b+c==0, cnt++

O(N^2 Log M) Time and O(N)+O(M) Space

Two pointers.

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        vector<vector<int>> res;

        int n = nums.size();
        for (int i = 0; i < n - 2; i++) {
            if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
                int lo = i + 1, hi = n - 1, sum = 0 - nums[i];

                while (lo < hi) {
                    if (nums[lo] + nums[hi] == sum) {
                        res.push_back({nums[i], nums[lo], nums[hi]});

                        while (lo < hi && nums[lo] == nums[lo + 1]) lo++;
                        while (lo < hi && nums[hi] == nums[hi - 1]) hi--;

                        lo++, hi--;
                    } else if (nums[lo] + nums[hi] < sum) lo++;
                    else hi--;
                }
            }
        }
        return res;
    }
};
// codestudio
#include <bits/stdc++.h>
vector<vector<int>> findTriplets(vector<int>arr, int n, int K) {
    sort(arr.begin(),arr.end());
    vector<vector<int>> res;
    for(int i = 0 ; i< n-2 ; i++){
        if(i==0 || (i>0 && arr[i]!=arr[i-1])){
            int lo = i+1;
            int hi = n-1;
            int sum = K - arr[i];

            // two pointer
            while(lo<hi){
                if(arr[lo]+arr[hi]==sum)
                {
                    res.push_back({arr[i],arr[lo],arr[hi]});

                    while(lo<hi && arr[lo]==arr[lo+1])lo++;
                    while(lo<hi && arr[hi]==arr[hi+1])hi--;

                    lo++;
                    hi--;
                }else if(arr[lo]+arr[hi]<sum)lo++;
                else hi--;
            }
        }
    }
    return res;
}