DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Array

4Sum

**Problem Statement:** Given an array `nums` of `n` integers, return an array of all the unique quadruplets `[nums[a], nums[b], nums[c], nums[d]]` such that `nums[a] + nums[b] + nums[c] + nums[d] == target`. **Hint / Expected Approach:** Sort + reduce to 3Sum + two-pointer **Edge Cases to Consider:** - (1) Multiple duplicate quadruplets - (2) Array size < 4 - (3) Large numbers causing overflow

Examples

Example 1
Input: [1,0,-1,0,-2,2], 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2
Input: [2,2,2,2,2], 8
Output: [[2,2,2,2]]
Example 3
Input: [1,2,3,4], 10
Output: [[1,2,3,4]]

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪Multiple duplicate quadruplets
  • ▪Array size < 4
  • ▪Large numbers causing overflow
Frequently Asked At
AmazonGoogleAdobeInfosys