LogIn
I don't have account.
Sponsored -53%
Zebronics #1 Best Seller

ZEBRONICS Blanc Slim Wireless Mouse — Rechargeable, BT + 2.4GHz (Black)

Up to 1600 DPI · Silent clicks · 63g · Multicolor LED

★★★★★ 4.0 (9,512) 5K+ bought last month
₹376 M.R.P. ₹799Save ₹423
Buy Now
Adℹ

4 Sum | Find Quads that add up to a target value

HackFury
43 Views
Amazon Pay offer
Adℹ

Given an array nums of integers and an integer target, return all unique quadruplets [a, b, c, d] such that:

a + b + c + d == target

The result must not contain duplicate quadruplets and the quadruplets can be in any order.

Example 1

Input:
nums = [1, 0, -1, 0, -2, 2]
target = 0

Output:
[[-2, -1, 1, 2],
 [-2, 0, 0, 2],
 [-1, 0, 0, 1]]

Example 2

Input:
nums = [2, 2, 2, 2, 2]
target = 8

Output:
[[2, 2, 2, 2]]

Constraints

  • 1 <= nums.length <= 200
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9

Approach 1: Brute Force Approach (4 Nested Loops)

The most straightforward way to solve the 4 Sum problem is to:

  • Check every possible combination of four elements in the array.

  • If their sum equals the target -> add that quadruplet to the result.

  • To avoid duplicate quadruplets (like [1,0,-1,0] vs [0,1,0,-1]), we sort each quadruplet before storing it in a Set.

  • This ensures all unique combinations are captured.

Steps

  • Loop i from 0 -> n-1

  • Loop j from i+1 -> n-1

  • Loop k from j+1 -> n-1

  • Loop l from k+1 -> n-1

  • If (nums[i] + nums[j] + nums[k] + nums[l]) == target, form a quadruplet, sort it and add it to a Set.


#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> fourSumBruteForce(vector<int>& nums, int target) {
    set<vector<int>> resultSet;
    int n = nums.size();
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int k = j + 1; k < n; k++) {
                for (int l = k + 1; l < n; l++) {
                    long long sum = (long long)nums[i] + nums[j] + nums[k] + nums[l];
                    if (sum == target) {
                        vector<int> quad = {nums[i], nums[j], nums[k], nums[l]};
                        sort(quad.begin(), quad.end());
                        resultSet.insert(quad);
                    }
                }
            }
        }
    }
    return vector<vector<int>>(resultSet.begin(), resultSet.end());
}
int main() {
    vector<int> nums = {1, 0, -1, 0, -2, 2};
    int target = 0;
    vector<vector<int>> result = fourSumBruteForce(nums, target);
    cout << "Quadruplets summing to " << target << ":\n";
    for (auto &quad : result) {
        cout << "[ ";
        for (int num : quad) cout << num << " ";
        cout << "]\n";
    }
}

import java.util.*;

public class FourSumProblem {
    public static List<List<Integer>> fourSumBruteForce(int[] nums, int target) {
        Set<List<Integer>> resultSet = new HashSet<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int k = j + 1; k < n; k++) {
                    for (int l = k + 1; l < n; l++) {
                        if ((long)nums[i] + nums[j] + nums[k] + nums[l] == target) {
                            List<Integer> quad = Arrays.asList(nums[i], nums[j], nums[k], nums[l]);
                            Collections.sort(quad);
                            resultSet.add(quad);
                        }
                    }
                }
            }
        }
        return new ArrayList<>(resultSet);
    }
    public static void main(String[] args) {
        int[] nums = {1, 0, -1, 0, -2, 2};
        int target = 0;
        List<List<Integer>> result = fourSumBruteForce(nums, target);
        System.out.println("Quadruplets summing to " + target + ":");
        for (List<Integer> quad : result) {
            System.out.println(quad);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;

public class FourSumProblem {
    public static List<List<int>> FourSumBruteForce(int[] nums, int target) {
        HashSet<string> seen = new HashSet<string>();
        List<List<int>> result = new List<List<int>>();
        int n = nums.Length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int k = j + 1; k < n; k++) {
                    for (int l = k + 1; l < n; l++) {
                        long sum = (long)nums[i] + nums[j] + nums[k] + nums[l];
                        if (sum == target) {
                            var quad = new List<int> { nums[i], nums[j], nums[k], nums[l] };
                            quad.Sort();
                            string key = string.Join(",", quad);
                            if (seen.Add(key))
                                result.Add(quad);
                        }
                    }
                }
            }
        }
        return result;
    }
    public static void Main() {
        int[] nums = {1, 0, -1, 0, -2, 2};
        int target = 0;
        var result = FourSumBruteForce(nums, target);
        Console.WriteLine($"Quadruplets summing to {target}:");
        foreach (var quad in result) {
            Console.WriteLine("[ " + string.Join(", ", quad) + " ]");
        }
    }
}

def four_sum_brute_force(nums, target):
    n = len(nums)
    result = set()
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                for l in range(k + 1, n):
                    if nums[i] + nums[j] + nums[k] + nums[l] == target:
                        quad = tuple(sorted([nums[i], nums[j], nums[k], nums[l]]))
                        result.add(quad)
    return [list(q) for q in result]


if __name__ == "__main__":
    nums = [1, 0, -1, 0, -2, 2]
    target = 0
    res = four_sum_brute_force(nums, target)
    print(f"Quadruplets summing to {target}:")
    for quad in res:
        print(quad)

Quadruplets summing to 0:
[-2, -1, 1, 2]
[-2, 0, 0, 2]
[-1, 0, 0, 1]

Complexity Analysis

  • Time Complexity: O(n⁴), Four nested loops
  • Space Complexity: O(m), For storing unique quadruplets in the set (m = number of valid results)

Approach 2: Better Approach (Using Hashing for 2-Sum Inside 4-Sum)

Instead of checking every possible quadruplet (O(n⁴)), we can fix the first two elements (i, j) and then find two remaining numbers that sum up to the required remainder.

That's exactly the 2-sum problem, which can be efficiently solved using a HashSet.

  • For each pair (i, j):

    • The remaining target = target - (nums[i] + nums[j])

    • We traverse the remaining part of the array (k onwards)

    • Use a HashSet to check if remainingTarget - nums[k] exists -> if yes, we found a valid quadruplet.

Steps

  • Sort the array to simplify duplicate handling (though duplicates are still tricky here).

  • Loop i from 0 -> n-1.

  • Loop j from i+1 -> n-1.

  • Initialize a HashSet to track seen numbers for the 2-sum part.

  • For each k from j+1 -> n-1:

    • Compute sum = nums[i] + nums[j] + nums[k]

    • If (target - sum) exists in the set -> we found a quadruplet.

    • Else -> add nums[k] to the set.

  • Store quadruplets in a Set<List> to avoid duplicates.


#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> fourSumBetter(vector<int>& nums, int target) {
    set<vector<int>> result;
    int n = nums.size();
    sort(nums.begin(), nums.end());
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            unordered_set<int> seen;
            for (int k = j + 1; k < n; k++) {
                int remaining = target - (nums[i] + nums[j] + nums[k]);
                if (seen.count(remaining)) {
                    vector<int> quad = {nums[i], nums[j], nums[k], remaining};
                    sort(quad.begin(), quad.end());
                    result.insert(quad);
                }
                seen.insert(nums[k]);
            }
        }
    }
    return vector<vector<int>>(result.begin(), result.end());
}

int main() {
    vector<int> nums = {1, 0, -1, 0, -2, 2};
    int target = 0;
    auto res = fourSumBetter(nums, target);
    cout << "Quadruplets summing to " << target << ":\n";
    for (auto& quad : res) {
        cout << "[ ";
        for (int num : quad) cout << num << " ";
        cout << "]\n";
    }
}

import java.util.*;

public class FourSumHashing {
    public static List<List<Integer>> fourSumBetter(int[] nums, int target) {
        Set<List<Integer>> result = new HashSet<>();
        int n = nums.length;
        Arrays.sort(nums); // Helps maintain order and avoid duplicates
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                Set<Integer> seen = new HashSet<>();
                for (int k = j + 1; k < n; k++) {
                    int remaining = target - (nums[i] + nums[j] + nums[k]);
                    if (seen.contains(remaining)) {
                        List<Integer> quad = Arrays.asList(nums[i], nums[j], nums[k], remaining);
                        Collections.sort(quad);
                        result.add(quad);
                    }
                    seen.add(nums[k]);
                }
            }
        }
        return new ArrayList<>(result);
    }
    public static void main(String[] args) {
        int[] nums = {1, 0, -1, 0, -2, 2};
        int target = 0;
        List<List<Integer>> res = fourSumBetter(nums, target);
        System.out.println("Quadruplets summing to " + target + ":");
        for (List<Integer> quad : res) {
            System.out.println(quad);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;

public class FourSumHashing {
    public static List<List<int>> FourSumBetter(int[] nums, int target) {
        HashSet<string> seenCombos = new HashSet<string>();
        List<List<int>> result = new List<List<int>>();
        Array.Sort(nums);
        int n = nums.Length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                HashSet<int> seen = new HashSet<int>();
                for (int k = j + 1; k < n; k++) {
                    int remaining = target - (nums[i] + nums[j] + nums[k]);
                    if (seen.Contains(remaining)) {
                        var quad = new List<int> { nums[i], nums[j], nums[k], remaining };
                        quad.Sort();
                        string key = string.Join(",", quad);
                        if (seenCombos.Add(key))
                            result.Add(quad);
                    }
                    seen.Add(nums[k]);
                }
            }
        }
        return result;
    }
    public static void Main() {
        int[] nums = {1, 0, -1, 0, -2, 2};
        int target = 0;
        var res = FourSumBetter(nums, target);
        Console.WriteLine($"Quadruplets summing to {target}:");
        foreach (var quad in res) {
            Console.WriteLine("[ " + string.Join(", ", quad) + " ]");
        }
    }
}

def four_sum_better(nums, target):
    n = len(nums)
    nums.sort()
    result = set()
    for i in range(n):
        for j in range(i + 1, n):
            seen = set()
            for k in range(j + 1, n):
                remaining = target - (nums[i] + nums[j] + nums[k])
                if remaining in seen:
                    quad = tuple(sorted([nums[i], nums[j], nums[k], remaining]))
                    result.add(quad)
                seen.add(nums[k])
    return [list(q) for q in result]

if __name__ == "__main__":
    nums = [1, 0, -1, 0, -2, 2]
    target = 0
    res = four_sum_better(nums, target)
    print(f"Quadruplets summing to {target}:")
    for quad in res:
        print(quad)

Quadruplets summing to 0:
[-2, -1, 1, 2]
[-2, 0, 0, 2]
[-1, 0, 0, 1]

Complexity Analysis

  • Time Complexity: O(n³), Two nested loops (i, j), one traversal for k
  • Space Complexity: O(n), For the HashSet used in each (i, j) pair

Approach 3: Optimal Approach (Sorting + Two Pointers)

The 4 Sum problem can be efficiently solved by combining sorting and the two-pointer technique, similar to how we solve 3 Sum or 2 Sum.

Here’s the intuition:

  • We fix the first two numbers (i and j).

  • Then we use two pointers (left, right) to find the remaining two numbers whose sum completes the target.

  • Sorting helps in duplicate handling and allows efficient two-pointer movement based on the sum comparison.

  • This approach efficiently brings down time complexity from O(n⁴) to O(n³) and uses only O(1) extra space.

Step-by-Step Explanation

  • Sort the array , this is crucial for skipping duplicates and for pointer movement logic.

  • Fix the first two elements using nested loops (i and j).

  • Apply the two-pointer approach for the remaining two elements:

    • Initialize left = j + 1 and right = n - 1

    • Compute sum = nums[i] + nums[j] + nums[left] + nums[right]

    • If sum equals target -> store the quadruplet and skip duplicates.

    • If sum < target -> move left++ (need a bigger sum).

    • If sum > target -> move right-- (need a smaller sum).

  • Skip duplicates for all four indices (i, j, left and right) to avoid repeated quadruplets.


#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> fourSum(vector<int>& nums, int target) {
    vector<vector<int>> result;
    sort(nums.begin(), nums.end());
    int n = nums.size();
    for (int i = 0; i < n - 3; i++) {
        if (i > 0 && nums[i] == nums[i - 1]) continue;
        for (int j = i + 1; j < n - 2; j++) {
            if (j > i + 1 && nums[j] == nums[j - 1]) continue;
            int left = j + 1, right = n - 1;
            while (left < right) {
                long long sum = (long long)nums[i] + nums[j] + nums[left] + nums[right];
                if (sum == target) {
                    result.push_back({nums[i], nums[j], nums[left], nums[right]});
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;
                    left++;
                    right--;
                } else if (sum < target)
                    left++;
                else
                    right--;
            }
        }
    }
    return result;
}
int main() {
    vector<int> nums = {1, 0, -1, 0, -2, 2};
    int target = 0;
    auto res = fourSum(nums, target);
    cout << "Quadruplets summing to " << target << ":\n";
    for (auto& quad : res) {
        cout << "[ ";
        for (int x : quad) cout << x << " ";
        cout << "]\n";
    }
}

import java.util.*;

public class FourSumOptimal {
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        int n = nums.length;
        Arrays.sort(nums);
        for (int i = 0; i < n - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicate i
            for (int j = i + 1; j < n - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue; // skip duplicate j
                int left = j + 1, right = n - 1;
                while (left < right) {
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum == target) {
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while (left < right && nums[left] == nums[left + 1]) left++;
                        while (left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        right--;
                    }
                }
            }
        }
        return result;
    }
    public static void main(String[] args) {
        int[] nums = {1, 0, -1, 0, -2, 2};
        int target = 0;
        List<List<Integer>> res = fourSum(nums, target);
        System.out.println("Quadruplets summing to " + target + ":");
        for (List<Integer> quad : res) {
            System.out.println(quad);
        }
    }
}

using System;
using System.Collections.Generic;

public class FourSumOptimal {
    public static List<List<int>> FourSum(int[] nums, int target) {
        Array.Sort(nums);
        List<List<int>> result = new List<List<int>>();
        int n = nums.Length;
        for (int i = 0; i < n - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < n - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                int left = j + 1, right = n - 1;
                while (left < right) {
                    long sum = (long)nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum == target) {
                        result.Add(new List<int> { nums[i], nums[j], nums[left], nums[right] });
                        while (left < right && nums[left] == nums[left + 1]) left++;
                        while (left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    } else if (sum < target) left++;
                    else right--;
                }
            }
        }
        return result;
    }
    public static void Main() {
        int[] nums = {1, 0, -1, 0, -2, 2};
        int target = 0;
        var res = FourSum(nums, target);
        Console.WriteLine($"Quadruplets summing to {target}:");
        foreach (var quad in res) {
            Console.WriteLine("[ " + string.Join(", ", quad) + " ]");
        }
    }
}

def four_sum(nums, target):
    nums.sort()
    res = []
    n = len(nums)
    for i in range(n - 3):
        if i > 0 and nums[i] == nums[i - 1]:
            continue
        for j in range(i + 1, n - 2):
            if j > i + 1 and nums[j] == nums[j - 1]:
                continue
            left, right = j + 1, n - 1
            while left < right:
                s = nums[i] + nums[j] + nums[left] + nums[right]
                if s == target:
                    res.append([nums[i], nums[j], nums[left], nums[right]])
                    while left < right and nums[left] == nums[left + 1]:
                        left += 1
                    while left < right and nums[right] == nums[right - 1]:
                        right -= 1
                    left += 1
                    right -= 1
                elif s < target:
                    left += 1
                else:
                    right -= 1
    return res

if __name__ == "__main__":
    nums = [1, 0, -1, 0, -2, 2]
    target = 0
    result = four_sum(nums, target)
    print(f"Quadruplets summing to {target}:")
    for quad in result:
        print(quad)

Quadruplets summing to 0:
[-2, -1, 1, 2]
[-2, 0, 0, 2]
[-1, 0, 0, 1]

Complexity Analysis

  • Time Complexity: O(n³), Double loops for i, j and two-pointer traversal
  • Space Complexity: O(1), Only uses result list, no extra data structure
  • Sorting Overhead: O(n log n), Negligible compared to O(n³) main loop

Complexity Analysis

Approach Time Complexity Space Complexity Notes
Brute Force (4 loops) O(n⁴) O(n) Too slow
Hashing-based 3-loop O(n³) O(n) Better but still heavy
Sorting + Two Pointers (Optimal) O(n³) O(1) Best approach
Responses (0)

Write a response

CommentHide Comments

No Comments yet.

"},"4":{"id":4,"name":"ZEBRONICS Blanc mouse ad","otherData":"{}","contentFormat":2,"content":"
Sponsored -53%
Zebronics #1 Best Seller

ZEBRONICS Blanc Slim Wireless Mouse — Rechargeable, BT + 2.4GHz (Black)

Up to 1600 DPI · Silent clicks · 63g · Multicolor LED

★★★★★ 4.0 (9,512) 5K+ bought last month
₹376 M.R.P. ₹799Save ₹423
Buy Now
"},"2":{"id":2,"name":"Crousal Ads","otherData":"{}","contentFormat":2,"content":"
Sponsored -34%
iQOO Amazon's Choice

iQOO Z10 Lite 5G — Cyber Green, 4GB RAM, 128GB Storage

Dimensity 6300 · 50MP Sony AI Camera · 6000 mAh · IP64

★★★★★ 4.0 (2,052) 500+ bought last month
₹18,997 M.R.P. ₹28,999Save ₹10,002
Buy Now
"},"1":{"id":1,"name":"Banner Ads","otherData":"{}","contentFormat":2,"content":"\n
\n \n \n \"Amazon\n \n
"}}},"blogDetails":{"isWriter":false,"likes":0,"isLikedByUser":false,"tagIds":[17,62,133,169],"tags":[{"id":17,"name":"Array","maskingName":"array","isFeatured":true},{"id":62,"name":"Sorting","maskingName":"sorting","isFeatured":true},{"id":133,"name":"Coding Problems","maskingName":"coding-problems","isFeatured":true},{"id":169,"name":"Two Pointes","maskingName":"two-pointers","isFeatured":true}],"comments":[],"bookmark":{"isBookmarked":false,"count":0},"author":{"userId":"MjlfNF84LVs0XTJuLXRDX0FOZ2VfX0l0","name":"HackFury"},"viewCount":42,"showBannerImage":false,"seoTags":null,"content":"

Given an array nums of integers and an integer target, return all unique quadruplets [a, b, c, d] such that:

\n

a + b + c + d == target

\n

The result must not contain duplicate quadruplets and the quadruplets can be in any order.

\n

Example 1

\n
Input:\nnums = [1, 0, -1, 0, -2, 2]\ntarget = 0\n\nOutput:\n[[-2, -1, 1, 2],\n [-2, 0, 0, 2],\n [-1, 0, 0, 1]]\n
\n

Example 2

\n
Input:\nnums = [2, 2, 2, 2, 2]\ntarget = 8\n\nOutput:\n[[2, 2, 2, 2]]\n
\n

Constraints

\n\n

Approach 1: Brute Force Approach (4 Nested Loops)

\n

The most straightforward way to solve the 4 Sum problem is to:

\n\n

Steps

\n\n
\n#include <bits/stdc++.h>\nusing namespace std;\n\nvector<vector<int>> fourSumBruteForce(vector<int>& nums, int target) {\n    set<vector<int>> resultSet;\n    int n = nums.size();\n    for (int i = 0; i < n; i++) {\n        for (int j = i + 1; j < n; j++) {\n            for (int k = j + 1; k < n; k++) {\n                for (int l = k + 1; l < n; l++) {\n                    long long sum = (long long)nums[i] + nums[j] + nums[k] + nums[l];\n                    if (sum == target) {\n                        vector<int> quad = {nums[i], nums[j], nums[k], nums[l]};\n                        sort(quad.begin(), quad.end());\n                        resultSet.insert(quad);\n                    }\n                }\n            }\n        }\n    }\n    return vector<vector<int>>(resultSet.begin(), resultSet.end());\n}\nint main() {\n    vector<int> nums = {1, 0, -1, 0, -2, 2};\n    int target = 0;\n    vector<vector<int>> result = fourSumBruteForce(nums, target);\n    cout << \"Quadruplets summing to \" << target << \":\\n\";\n    for (auto &quad : result) {\n        cout << \"[ \";\n        for (int num : quad) cout << num << \" \";\n        cout << \"]\\n\";\n    }\n}\n
\nimport java.util.*;\n\npublic class FourSumProblem {\n    public static List<List<Integer>> fourSumBruteForce(int[] nums, int target) {\n        Set<List<Integer>> resultSet = new HashSet<>();\n        int n = nums.length;\n        for (int i = 0; i < n; i++) {\n            for (int j = i + 1; j < n; j++) {\n                for (int k = j + 1; k < n; k++) {\n                    for (int l = k + 1; l < n; l++) {\n                        if ((long)nums[i] + nums[j] + nums[k] + nums[l] == target) {\n                            List<Integer> quad = Arrays.asList(nums[i], nums[j], nums[k], nums[l]);\n                            Collections.sort(quad);\n                            resultSet.add(quad);\n                        }\n                    }\n                }\n            }\n        }\n        return new ArrayList<>(resultSet);\n    }\n    public static void main(String[] args) {\n        int[] nums = {1, 0, -1, 0, -2, 2};\n        int target = 0;\n        List<List<Integer>> result = fourSumBruteForce(nums, target);\n        System.out.println(\"Quadruplets summing to \" + target + \":\");\n        for (List<Integer> quad : result) {\n            System.out.println(quad);\n        }\n    }\n}\n
\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\npublic class FourSumProblem {\n    public static List<List<int>> FourSumBruteForce(int[] nums, int target) {\n        HashSet<string> seen = new HashSet<string>();\n        List<List<int>> result = new List<List<int>>();\n        int n = nums.Length;\n        for (int i = 0; i < n; i++) {\n            for (int j = i + 1; j < n; j++) {\n                for (int k = j + 1; k < n; k++) {\n                    for (int l = k + 1; l < n; l++) {\n                        long sum = (long)nums[i] + nums[j] + nums[k] + nums[l];\n                        if (sum == target) {\n                            var quad = new List<int> { nums[i], nums[j], nums[k], nums[l] };\n                            quad.Sort();\n                            string key = string.Join(\",\", quad);\n                            if (seen.Add(key))\n                                result.Add(quad);\n                        }\n                    }\n                }\n            }\n        }\n        return result;\n    }\n    public static void Main() {\n        int[] nums = {1, 0, -1, 0, -2, 2};\n        int target = 0;\n        var result = FourSumBruteForce(nums, target);\n        Console.WriteLine($\"Quadruplets summing to {target}:\");\n        foreach (var quad in result) {\n            Console.WriteLine(\"[ \" + string.Join(\", \", quad) + \" ]\");\n        }\n    }\n}\n
\ndef four_sum_brute_force(nums, target):\n    n = len(nums)\n    result = set()\n    for i in range(n):\n        for j in range(i + 1, n):\n            for k in range(j + 1, n):\n                for l in range(k + 1, n):\n                    if nums[i] + nums[j] + nums[k] + nums[l] == target:\n                        quad = tuple(sorted([nums[i], nums[j], nums[k], nums[l]]))\n                        result.add(quad)\n    return [list(q) for q in result]\n\n\nif __name__ == \"__main__\":\n    nums = [1, 0, -1, 0, -2, 2]\n    target = 0\n    res = four_sum_brute_force(nums, target)\n    print(f\"Quadruplets summing to {target}:\")\n    for quad in res:\n        print(quad)\n
\n
\nQuadruplets summing to 0:\n[-2, -1, 1, 2]\n[-2, 0, 0, 2]\n[-1, 0, 0, 1]\n
\n

Complexity Analysis

\n\n

Approach 2: Better Approach (Using Hashing for 2-Sum Inside 4-Sum)

\n

Instead of checking every possible quadruplet (O(n⁴)),\nwe can fix the first two elements (i, j) and then find two remaining numbers that sum up to the required remainder.

\n

That's exactly the 2-sum problem, which can be efficiently solved using a HashSet.

\n\n

Steps

\n\n
\n#include <bits/stdc++.h>\nusing namespace std;\n\nvector<vector<int>> fourSumBetter(vector<int>& nums, int target) {\n    set<vector<int>> result;\n    int n = nums.size();\n    sort(nums.begin(), nums.end());\n    for (int i = 0; i < n; i++) {\n        for (int j = i + 1; j < n; j++) {\n            unordered_set<int> seen;\n            for (int k = j + 1; k < n; k++) {\n                int remaining = target - (nums[i] + nums[j] + nums[k]);\n                if (seen.count(remaining)) {\n                    vector<int> quad = {nums[i], nums[j], nums[k], remaining};\n                    sort(quad.begin(), quad.end());\n                    result.insert(quad);\n                }\n                seen.insert(nums[k]);\n            }\n        }\n    }\n    return vector<vector<int>>(result.begin(), result.end());\n}\n\nint main() {\n    vector<int> nums = {1, 0, -1, 0, -2, 2};\n    int target = 0;\n    auto res = fourSumBetter(nums, target);\n    cout << \"Quadruplets summing to \" << target << \":\\n\";\n    for (auto& quad : res) {\n        cout << \"[ \";\n        for (int num : quad) cout << num << \" \";\n        cout << \"]\\n\";\n    }\n}\n
\nimport java.util.*;\n\npublic class FourSumHashing {\n    public static List<List<Integer>> fourSumBetter(int[] nums, int target) {\n        Set<List<Integer>> result = new HashSet<>();\n        int n = nums.length;\n        Arrays.sort(nums); // Helps maintain order and avoid duplicates\n        for (int i = 0; i < n; i++) {\n            for (int j = i + 1; j < n; j++) {\n                Set<Integer> seen = new HashSet<>();\n                for (int k = j + 1; k < n; k++) {\n                    int remaining = target - (nums[i] + nums[j] + nums[k]);\n                    if (seen.contains(remaining)) {\n                        List<Integer> quad = Arrays.asList(nums[i], nums[j], nums[k], remaining);\n                        Collections.sort(quad);\n                        result.add(quad);\n                    }\n                    seen.add(nums[k]);\n                }\n            }\n        }\n        return new ArrayList<>(result);\n    }\n    public static void main(String[] args) {\n        int[] nums = {1, 0, -1, 0, -2, 2};\n        int target = 0;\n        List<List<Integer>> res = fourSumBetter(nums, target);\n        System.out.println(\"Quadruplets summing to \" + target + \":\");\n        for (List<Integer> quad : res) {\n            System.out.println(quad);\n        }\n    }\n}\n
\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\npublic class FourSumHashing {\n    public static List<List<int>> FourSumBetter(int[] nums, int target) {\n        HashSet<string> seenCombos = new HashSet<string>();\n        List<List<int>> result = new List<List<int>>();\n        Array.Sort(nums);\n        int n = nums.Length;\n        for (int i = 0; i < n; i++) {\n            for (int j = i + 1; j < n; j++) {\n                HashSet<int> seen = new HashSet<int>();\n                for (int k = j + 1; k < n; k++) {\n                    int remaining = target - (nums[i] + nums[j] + nums[k]);\n                    if (seen.Contains(remaining)) {\n                        var quad = new List<int> { nums[i], nums[j], nums[k], remaining };\n                        quad.Sort();\n                        string key = string.Join(\",\", quad);\n                        if (seenCombos.Add(key))\n                            result.Add(quad);\n                    }\n                    seen.Add(nums[k]);\n                }\n            }\n        }\n        return result;\n    }\n    public static void Main() {\n        int[] nums = {1, 0, -1, 0, -2, 2};\n        int target = 0;\n        var res = FourSumBetter(nums, target);\n        Console.WriteLine($\"Quadruplets summing to {target}:\");\n        foreach (var quad in res) {\n            Console.WriteLine(\"[ \" + string.Join(\", \", quad) + \" ]\");\n        }\n    }\n}\n
\ndef four_sum_better(nums, target):\n    n = len(nums)\n    nums.sort()\n    result = set()\n    for i in range(n):\n        for j in range(i + 1, n):\n            seen = set()\n            for k in range(j + 1, n):\n                remaining = target - (nums[i] + nums[j] + nums[k])\n                if remaining in seen:\n                    quad = tuple(sorted([nums[i], nums[j], nums[k], remaining]))\n                    result.add(quad)\n                seen.add(nums[k])\n    return [list(q) for q in result]\n\nif __name__ == \"__main__\":\n    nums = [1, 0, -1, 0, -2, 2]\n    target = 0\n    res = four_sum_better(nums, target)\n    print(f\"Quadruplets summing to {target}:\")\n    for quad in res:\n        print(quad)\n
\n
\nQuadruplets summing to 0:\n[-2, -1, 1, 2]\n[-2, 0, 0, 2]\n[-1, 0, 0, 1]\n
\n

Complexity Analysis

\n\n

Approach 3: Optimal Approach (Sorting + Two Pointers)

\n

The 4 Sum problem can be efficiently solved by combining sorting and the two-pointer technique, similar to how we solve 3 Sum or 2 Sum.

\n

Here’s the intuition:

\n\n

Step-by-Step Explanation

\n\n
\n#include <bits/stdc++.h>\nusing namespace std;\n\nvector<vector<int>> fourSum(vector<int>& nums, int target) {\n    vector<vector<int>> result;\n    sort(nums.begin(), nums.end());\n    int n = nums.size();\n    for (int i = 0; i < n - 3; i++) {\n        if (i > 0 && nums[i] == nums[i - 1]) continue;\n        for (int j = i + 1; j < n - 2; j++) {\n            if (j > i + 1 && nums[j] == nums[j - 1]) continue;\n            int left = j + 1, right = n - 1;\n            while (left < right) {\n                long long sum = (long long)nums[i] + nums[j] + nums[left] + nums[right];\n                if (sum == target) {\n                    result.push_back({nums[i], nums[j], nums[left], nums[right]});\n                    while (left < right && nums[left] == nums[left + 1]) left++;\n                    while (left < right && nums[right] == nums[right - 1]) right--;\n                    left++;\n                    right--;\n                } else if (sum < target)\n                    left++;\n                else\n                    right--;\n            }\n        }\n    }\n    return result;\n}\nint main() {\n    vector<int> nums = {1, 0, -1, 0, -2, 2};\n    int target = 0;\n    auto res = fourSum(nums, target);\n    cout << \"Quadruplets summing to \" << target << \":\\n\";\n    for (auto& quad : res) {\n        cout << \"[ \";\n        for (int x : quad) cout << x << \" \";\n        cout << \"]\\n\";\n    }\n}\n
\nimport java.util.*;\n\npublic class FourSumOptimal {\n    public static List<List<Integer>> fourSum(int[] nums, int target) {\n        List<List<Integer>> result = new ArrayList<>();\n        int n = nums.length;\n        Arrays.sort(nums);\n        for (int i = 0; i < n - 3; i++) {\n            if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicate i\n            for (int j = i + 1; j < n - 2; j++) {\n                if (j > i + 1 && nums[j] == nums[j - 1]) continue; // skip duplicate j\n                int left = j + 1, right = n - 1;\n                while (left < right) {\n                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];\n                    if (sum == target) {\n                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));\n                        while (left < right && nums[left] == nums[left + 1]) left++;\n                        while (left < right && nums[right] == nums[right - 1]) right--;\n                        left++;\n                        right--;\n                    } else if (sum < target) {\n                        left++;\n                    } else {\n                        right--;\n                    }\n                }\n            }\n        }\n        return result;\n    }\n    public static void main(String[] args) {\n        int[] nums = {1, 0, -1, 0, -2, 2};\n        int target = 0;\n        List<List<Integer>> res = fourSum(nums, target);\n        System.out.println(\"Quadruplets summing to \" + target + \":\");\n        for (List<Integer> quad : res) {\n            System.out.println(quad);\n        }\n    }\n}\n
\nusing System;\nusing System.Collections.Generic;\n\npublic class FourSumOptimal {\n    public static List<List<int>> FourSum(int[] nums, int target) {\n        Array.Sort(nums);\n        List<List<int>> result = new List<List<int>>();\n        int n = nums.Length;\n        for (int i = 0; i < n - 3; i++) {\n            if (i > 0 && nums[i] == nums[i - 1]) continue;\n            for (int j = i + 1; j < n - 2; j++) {\n                if (j > i + 1 && nums[j] == nums[j - 1]) continue;\n                int left = j + 1, right = n - 1;\n                while (left < right) {\n                    long sum = (long)nums[i] + nums[j] + nums[left] + nums[right];\n                    if (sum == target) {\n                        result.Add(new List<int> { nums[i], nums[j], nums[left], nums[right] });\n                        while (left < right && nums[left] == nums[left + 1]) left++;\n                        while (left < right && nums[right] == nums[right - 1]) right--;\n                        left++;\n                        right--;\n                    } else if (sum < target) left++;\n                    else right--;\n                }\n            }\n        }\n        return result;\n    }\n    public static void Main() {\n        int[] nums = {1, 0, -1, 0, -2, 2};\n        int target = 0;\n        var res = FourSum(nums, target);\n        Console.WriteLine($\"Quadruplets summing to {target}:\");\n        foreach (var quad in res) {\n            Console.WriteLine(\"[ \" + string.Join(\", \", quad) + \" ]\");\n        }\n    }\n}\n
\ndef four_sum(nums, target):\n    nums.sort()\n    res = []\n    n = len(nums)\n    for i in range(n - 3):\n        if i > 0 and nums[i] == nums[i - 1]:\n            continue\n        for j in range(i + 1, n - 2):\n            if j > i + 1 and nums[j] == nums[j - 1]:\n                continue\n            left, right = j + 1, n - 1\n            while left < right:\n                s = nums[i] + nums[j] + nums[left] + nums[right]\n                if s == target:\n                    res.append([nums[i], nums[j], nums[left], nums[right]])\n                    while left < right and nums[left] == nums[left + 1]:\n                        left += 1\n                    while left < right and nums[right] == nums[right - 1]:\n                        right -= 1\n                    left += 1\n                    right -= 1\n                elif s < target:\n                    left += 1\n                else:\n                    right -= 1\n    return res\n\nif __name__ == \"__main__\":\n    nums = [1, 0, -1, 0, -2, 2]\n    target = 0\n    result = four_sum(nums, target)\n    print(f\"Quadruplets summing to {target}:\")\n    for quad in result:\n        print(quad)\n
\n
\nQuadruplets summing to 0:\n[-2, -1, 1, 2]\n[-2, 0, 0, 2]\n[-1, 0, 0, 1]\n
\n

Complexity Analysis

\n\n

Complexity Analysis

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ApproachTime ComplexitySpace ComplexityNotes
Brute Force (4 loops)O(n⁴)O(n)Too slow
Hashing-based 3-loopO(n³)O(n)Better but still heavy
Sorting + Two Pointers (Optimal)O(n³)O(1)Best approach
","categoryId":177,"subCategoryId":177,"contentFormat":5,"blogId":27,"userId":"MjlfNF84LVs0XTJuLXRDX0FOZ2VfX0l0","title":"4 Sum | Find Quads that add up to a target value","url":"4-sum-find-quads-that-add-up-to-a-target-value","bannerImage":"","seoDescription":"","generatedOn":"2025-10-05T12:57:31","updatedOn":"2025-10-05T12:57:31"},"popularContents":[{"id":138,"title":"DSA Patterns for Coding Interviews: Complete LeetCode Roadmap (Beginner to Advanced)","url":"dsa-patterns-for-coding-interviews-complete-leetcode-roadmap-beginner-to-advanced","description":null,"seoDescription":"Master every LeetCode pattern for coding interviews. Learn Arrays, Sliding Window, Graphs, Dynamic Programming, Trees, Heaps and more with 250+ curated problem","contentType":5,"generatedOn":"2026-10-14T22:54:36.272838Z","viewCount":0},{"id":172,"title":"Google L4 Software Engineer III interview experience | Selected","url":"google-l4-software-engineer-iii-interview-experience-selected","description":null,"seoDescription":"Google L4 Software Engineer III interview experience with API design, Googlyness, route matching, graph coding rounds, follow-up questions and tips.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2728435Z","viewCount":0},{"id":173,"title":"Uber SDE-2 Interview Experience : LLD, HLD, DSA & Hiring Manager Round","url":"uber-sde-2-interview-experience-lld-hld-dsa-hiring-manager-round","description":null,"seoDescription":"Read a real Uber SDE-2 interview experience covering DSA, low-level design, high-level system design, behavioral rounds, hiring freeze and key takeaways.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2728453Z","viewCount":0},{"id":168,"title":"Amazon SDE-2 Interview Experience (3 Years Experience)","url":"amazon-sde-2-interview-experience-3-years-experience","description":null,"seoDescription":" Read a real Amazon SDE-2 interview experience covering the online assessment, coding rounds, object-oriented design, leadership principles and system design.","contentType":5,"generatedOn":"2026-10-14T22:54:36.272842Z","viewCount":0},{"id":167,"title":"Meesho SDE-1 Online Assessment Interview Experience (Remote) - Rejected","url":"meesho-sde-1-online-assessment-interview-experience-remote-rejected","description":null,"seoDescription":"Read a real Meesho SDE-1 online assessment experience with coding questions, DSA topics, solutions, difficulty level, preparation tips and interview insights.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2728398Z","viewCount":0},{"id":166,"title":"System Design Interview – BIGGEST Mistakes to Avoid","url":"system-design-interview-biggest-mistakes-to-avoid","description":null,"seoDescription":"Learn the biggest mistakes to avoid in a system design interview, from unclear requirements to poor trade-off analysis. Includes real-world cases, advanced insights and examples to help you ace your next system design interview.","contentType":2,"generatedOn":"2026-10-14T22:54:36.2723122Z","viewCount":0},{"id":122,"title":"Zepto SDE-1 Interview Experience (Backend Developer) : Real DSA, LLD, Chess Game Design","url":"zepto-sde-1-interview-experience-backend-developer-real-dsa-lld-chess-game-design","description":null,"seoDescription":" Zepto SDE-1 Backend Interview Experience covering DSA, Dynamic Programming, LLD, API Design, DB Schema, OOP & Product Company Rounds.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2728363Z","viewCount":0},{"id":213,"title":"Amazon SDE II Interview Experience (Bangalore, Selected) ~ Aug 2026","url":"amazon-sde-ii-interview-experience-bangalore-selected-aug-2026","description":null,"seoDescription":"Amazon SDE II interview experience from Bangalore covering 4 rounds, DSA, LLD, system design, Leadership Principles, coding questions and selection.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2728469Z","viewCount":0},{"id":83,"title":"Uber SDE 2 Interview Experience (5 Rounds, Selected) - Complete DSA Questions with solution, System Design & Managerial Round","url":"uber-sde-2-interview-experience","description":null,"seoDescription":"Uber SDE 2 interview experience with 5 rounds, real DSA questions, system design, coding round, and preparation tips to crack Uber interviews.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2728294Z","viewCount":0},{"id":118,"title":"JioHotstar Staff Software Engineer Interview Experience (Bangalore)","url":"jiohotstar-staff-software-engineer-interview-experience-bangalore","description":null,"seoDescription":"JioHotstar Staff Software Engineer interview experience covering Uber system design, API rate limiter LLD, leadership rounds, bar raiser and HR insights.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2728343Z","viewCount":0}],"latestContents":[{"id":219,"title":"6 Tools That Made My Life Easier as a Software Engineer","url":"6-tools-that-made-my-life-easier-as-a-software-engineer","description":null,"seoDescription":"The 6 developer tools I use daily as a software engineer: Git, VS Code, Docker, Postman, Chrome DevTools, and AI assistants, with real tips, mistakes, and trade","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693003Z","viewCount":0},{"id":218,"title":"When Should a Business Use Multiple AI Agents Instead of One?","url":"when-should-a-business-use-multiple-ai-agents-instead-of-one","description":null,"seoDescription":"Single agent or multi-agent AI? Learn the 4 signals that justify splitting agents by knowledge, permissions, and risk, plus routing and handoff tips.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693091Z","viewCount":0},{"id":216,"title":"AI in Clinical Trials Market Estimated to Experience a Hike in Growth by 2035","url":"ai-in-clinical-trials-market-estimated-to-experience-a-hike-in-growth-by-2035","description":null,"seoDescription":"The exclusive information about market dynamics serves as a valuable guide to predict economic scenarios and initiatives taken to enhance future growth. Our mar","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693106Z","viewCount":0},{"id":217,"title":"AI in Clinical Trials Market Estimated to Experience a Hike in Growth by 2035","url":"ai-in-clinical-trials-market-estimated-to-experience-a-hike-in-growth-by-2035","description":null,"seoDescription":"The exclusive information about market dynamics serves as a valuable guide to predict economic scenarios and initiatives taken to enhance future growth. Our mar","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693121Z","viewCount":0},{"id":215,"title":"What Happens When an AI Agent Doesn't Know the Answer?","url":"what-happens-when-an-ai-agent-doesnt-know-the-answer","description":null,"seoDescription":"Most AI agents fail quietly. They guess instead of saying \"I don't know.\" Here's how to design better fallback and human-handoff behavior.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693135Z","viewCount":0},{"id":214,"title":"Greedy Algorithms Explained: How They Work, When They Fail and How to Use Them","url":"greedy-algorithms-explained-how-they-work-when-they-fail-and-how-to-use-them","description":null,"seoDescription":"Learn greedy algorithms step by step with C# code Dijkstra, Huffman coding, knapsack and MSTs plus when greedy fails and how to prove it's correct","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693152Z","viewCount":0},{"id":213,"title":"Amazon SDE II Interview Experience (Bangalore, Selected) ~ Aug 2026","url":"amazon-sde-ii-interview-experience-bangalore-selected-aug-2026","description":null,"seoDescription":"Amazon SDE II interview experience from Bangalore covering 4 rounds, DSA, LLD, system design, Leadership Principles, coding questions and selection.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693167Z","viewCount":0},{"id":212,"title":"How to Get 10x Better AI Answers Without Writing Better Prompts : 10 Proven Techniques","url":"how-to-get-10x-better-ai-answers-without-writing-better-prompts-10-proven-techniques","description":null,"seoDescription":"Get better AI answers without complex prompts. Learn 10 practical techniques using context, examples, tools, feedback, and verification.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693181Z","viewCount":0},{"id":211,"title":"I Interviewed for a Microsoft SDE I Role. Here's Everything That Happened.","url":"i-interviewed-for-a-microsoft-sde-i-role-heres-everything-that-happened","description":null,"seoDescription":"My real Microsoft SDE I interview experience, all 4 rounds, the exact DSA problems with fully tested solutions, the WhatsApp system design round","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693196Z","viewCount":0},{"id":140,"title":"PolicyBazaar Software Development Engineer (SDE) Interview Experience | DSA, Core CS & System Design","url":"policybazaar-software-development-engineer-sde-interview-experience-dsa-core-cs-system-design","description":null,"seoDescription":"Detailed PolicyBazaar SDE interview experience covering 3 rounds: DSA coding, core CS assessment and system design with key learnings and insights.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2693212Z","viewCount":0}],"relatedContents":[{"id":207,"title":"How to Find the Right View of a Binary Tree","url":"right-view-of-a-binary-tree","description":null,"seoDescription":"Learn how to find the right view of a binary tree using DFS and BFS, with examples, code, complexity analysis and coding interview tips.","contentType":5,"generatedOn":"2026-10-14T22:54:36.278753Z","viewCount":0},{"id":206,"title":"Left View of a Binary Tree: DFS and BFS Approaches Explained","url":"left-view-of-a-binary-tree","description":null,"seoDescription":"Learn how to find the left view of a binary tree using DFS and BFS, with examples, code, complexity analysis, common mistakes and interview tips.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2787573Z","viewCount":0},{"id":205,"title":"Top View of a Binary Tree Using BFS and DFS | Complete Guide","url":"top-view-of-a-binary-tree","description":null,"seoDescription":"Learn how to find the top view of a binary tree using DFS and BFS with step-by-step explanations, examples, code, complexity analysis and interview insights.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2787589Z","viewCount":0},{"id":201,"title":"Bottom View of a Binary Tree: DFS vs BFS Approach","url":"bottom-view-of-a-binary-tree-dfs-vs-bfs-approach","description":null,"seoDescription":"Learn to find the bottom view of a binary tree using horizontal distance. Compares a buggy DFS attempt, a fixed DFS and the optimal BFS solution in Java.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2787639Z","viewCount":0},{"id":200,"title":" Longest Playlist With No Repeated Songs : Brute Force to Optimal","url":"longest-playlist-with-no-repeated-songs-brute-force-to-optimal","description":null,"seoDescription":"Find the longest playlist with no repeated songs using brute force, HashSet, and HashMap sliding window techniques, with Java code and dry runs.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2787652Z","viewCount":0},{"id":90,"title":"Contain Virus : Deep, Intuitive & Fully Explained Guide","url":"contain-virus-deep-intuitive-fully-explained-guide","description":null,"seoDescription":"Contain Virus problem explained with DFS + greedy approach, step-by-step simulation, examples and optimized solutions for coding interviews.","contentType":5,"generatedOn":"2026-10-14T22:54:36.2787668Z","viewCount":0},{"id":37,"title":"Find the repeating and missing numbers","url":"find-the-repeating-and-missing-numbers","description":null,"seoDescription":"Find the repeating and missing numbers in an array from 1..n using brute force, hashmap, math and XOR, step-by-step explained with real-world use cases.","contentType":5,"generatedOn":"2026-10-14T22:54:36.278768Z","viewCount":0},{"id":26,"title":"Top 50 Most Frequently Asked String Coding Problems (Interview Preparation )","url":"top-50-most-frequently-asked-string-coding-problems-interview-preparation","description":null,"seoDescription":"","contentType":5,"generatedOn":"2026-10-14T22:54:36.2787703Z","viewCount":0},{"id":25,"title":"Intersection of Two Sorted Arrays","url":"intersection-of-two-sorted-arrays","description":null,"seoDescription":"","contentType":5,"generatedOn":"2026-10-14T22:54:36.2787716Z","viewCount":0}]}}},"source":{"isMobile":false}}; window.__CLIENT_RENDER__ = false;