-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Added FourSumProblem.java #5786 #5853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
class Solution { | ||
public List<List<Integer>> fourSum(int[] nums, int target) { | ||
int n = nums.length; | ||
|
||
List<List<Integer>> ans = new ArrayList<>(); | ||
Arrays.sort(nums); | ||
|
||
// select the first element of the quadruplet | ||
for (int i = 0; i < n; i++) { | ||
// Skip duplicate | ||
if (i > 0 && nums[i] == nums[i - 1]) continue; | ||
|
||
// select the second element | ||
for (int j = i + 1; j < n; j++) { | ||
// Skip duplicate | ||
if (j != i + 1 && nums[j] == nums[j - 1]) continue; | ||
//assign two pointers | ||
int k = j + 1; | ||
int l = n - 1; | ||
|
||
// Two-pointer approach to find the remaining two elements | ||
while (k < l) { | ||
// Calculate the sum of the current quadruplet | ||
long sum = nums[i]; | ||
sum += nums[j]; | ||
sum += nums[k]; | ||
sum += nums[l]; | ||
|
||
if (sum == target) { | ||
//if the sum matches the target store it in ans | ||
List<Integer> temp = new ArrayList<>(); | ||
temp.add(nums[i]); | ||
temp.add(nums[j]); | ||
temp.add(nums[k]); | ||
temp.add(nums[l]); | ||
ans.add(temp); | ||
|
||
// Move both pointers | ||
k++; | ||
l--; | ||
|
||
// skip duplicates for the third element & forth ele | ||
while (k < l && nums[k] == nums[k - 1]) k++; | ||
while (k < l && nums[l] == nums[l + 1]) l--; | ||
} | ||
else if (sum < target) | ||
// If the sum < target, move the left pointer (k) to increase the sum | ||
//bcz array is in sorting order so next element always greater | ||
k++; | ||
else | ||
// If the sum > the target, move the right pointer (l) to decrease the sum | ||
//bcz array is in sorting order so previous element of 'l' pointer always smaller | ||
l--; | ||
} | ||
} | ||
} | ||
return ans; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also please add tests. |
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refactor the class name to be the same as the name of algorithm.