Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

26. 4Sum

MediumDay 7 of 60Two Pointers

Given an integer array nums and an integer target, return every unique group of four values [a, b, c, d] taken from four different positions whose sum equals target. Two groups are the same if they contain the same values, so the answer must not repeat a group. The groups may be returned 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]]
These are the only three different value groups of size four that add up to 0.
Example 2
Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
Many index choices exist, but they all give the same values, so it is listed once.
Constraints
  • 1 <= nums.length <= 200
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
Hints
Frequently asked at: Amazon, Adobe, Apple, Goldman Sachs
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min