Skip to main content
Commonmark migration
Source Link

Given an array and a value, find all the triplets in the array whose sum is equal to the given value. For example, if the given array is {12, 3, 4, 1, 6, 9} and the given sum is 24, then this is one triplet (12, 3 and 9) which contributes to the total sum of 24.

 

Solution for given example:

 

6, 9, 9

 

6, 6, 12

 

3, 9, 12

Conditions:

  • The ordering of the numbers in the solution does not matter.

  • Duplicate triplet is not allowed.

  • A number is not allowed to be used multiple times.

Given an array and a value, find all the triplets in the array whose sum is equal to the given value. For example, if the given array is {12, 3, 4, 1, 6, 9} and the given sum is 24, then this is one triplet (12, 3 and 9) which contributes to the total sum of 24.

 

Solution for given example:

 

6, 9, 9

 

6, 6, 12

 

3, 9, 12

Conditions:

  • The ordering of the numbers in the solution does not matter.

  • Duplicate triplet is not allowed.

  • A number is not allowed to be used multiple times.

Given an array and a value, find all the triplets in the array whose sum is equal to the given value. For example, if the given array is {12, 3, 4, 1, 6, 9} and the given sum is 24, then this is one triplet (12, 3 and 9) which contributes to the total sum of 24.

Solution for given example:

6, 9, 9

6, 6, 12

3, 9, 12

Conditions:

  • The ordering of the numbers in the solution does not matter.

  • Duplicate triplet is not allowed.

  • A number is not allowed to be used multiple times.

Notice removed Authoritative reference needed by CommunityBot
Bounty Ended with no winning answer by CommunityBot
added 20 characters in body
Source Link
Exploring
  • 345
  • 4
  • 19

Runtime: O(n2)

GitHub

Runtime: O(n2)

GitHub

added 19 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Three sum problem using hash mapHashMap in javaJava

This question was asked in Find all triplets in array that add up to a given sumhere.

Here is my code which addresses all the issues.

  One concern is the code looks a little muddy which I want to avoid in the production grade code. Any constructive feedback is appreciated.

GitHub

Github Link: https://github.com/Ramblers-Code/CodeKata/blob/master/src/main/java/kata/array/ThreeSum.java#L5

Three sum problem using hash map in java

This question was asked in Find all triplets in array that add up to a given sum

Here is my code which addresses all the issues.

  One concern is the code looks a little muddy which I want to avoid in the production grade code. Any constructive feedback is appreciated.

Github Link: https://github.com/Ramblers-Code/CodeKata/blob/master/src/main/java/kata/array/ThreeSum.java#L5

Three sum problem using HashMap in Java

This question was asked here.

Here is my code which addresses all the issues. One concern is the code looks a little muddy which I want to avoid in the production grade code. Any constructive feedback is appreciated.

GitHub

Tweeted twitter.com/StackCodeReview/status/978734988316340224
Notice added Authoritative reference needed by Exploring
Bounty Started worth 50 reputation by Exploring
added 36 characters in body
Source Link
Exploring
  • 345
  • 4
  • 19
Loading
Source Link
Exploring
  • 345
  • 4
  • 19
Loading