[BAEL-1641] Find all pairs of numbers in an array that add up to a given sum (#3890)
* [BAEL-1641] Find all pairs of numbers in an array that add up to a given sum * Commiting editor's suggested changes * Commiting article Spring Data Reactive Mongo DB microservice in Kotlin * Revert commit for BAEL 1687 - Moving those files to a new branch * Use AssertJ and BDD-style on unit testing
This commit is contained in:
parent
5e78d679d8
commit
5b5386636b
|
@ -6,6 +6,7 @@ import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing;
|
|||
import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization;
|
||||
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
|
||||
import com.baeldung.algorithms.slope_one.SlopeOne;
|
||||
import com.baeldung.algorithms.pairsaddupnumber.FindPairs;
|
||||
|
||||
public class RunAlgorithm {
|
||||
|
||||
|
@ -17,6 +18,7 @@ public class RunAlgorithm {
|
|||
System.out.println("3 - Simple Genetic Algorithm");
|
||||
System.out.println("4 - Ant Colony");
|
||||
System.out.println("5 - Dijkstra");
|
||||
System.out.println("6 - All pairs in an array that add up to a given sum");
|
||||
int decision = in.nextInt();
|
||||
switch (decision) {
|
||||
case 1:
|
||||
|
@ -37,6 +39,12 @@ public class RunAlgorithm {
|
|||
case 5:
|
||||
System.out.println("Please run the DijkstraAlgorithmTest.");
|
||||
break;
|
||||
case 6:
|
||||
final FindPairs findPairs = new FindPairs();
|
||||
final int[] input = {1, 4, 3, 2, 1, 4, 4, 3, 3};
|
||||
final int sum = 6;
|
||||
findPairs.execute(input, sum);
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unknown option");
|
||||
break;
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
package com.baeldung.algorithms.pairsaddupnumber;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* Find all different pairs of numbers in an array that add up to a given sum - Complexity O(n)
|
||||
*/
|
||||
public class DifferentPairs {
|
||||
|
||||
/**
|
||||
* Show all different pairs using traditional "for" loop
|
||||
*
|
||||
* @param input - number's array
|
||||
* @param sum - given sum
|
||||
* @return - number's array with all existing pairs. This list will contain just one pair's element because
|
||||
* the other one can be calculated with SUM - element_1 = element_2
|
||||
*/
|
||||
public static List<Integer> findPairsWithForLoop(int[] input, int sum) {
|
||||
final List<Integer> allDifferentPairs = new ArrayList<>();
|
||||
// Aux. hash map
|
||||
final Map<Integer, Integer> pairs = new HashMap();
|
||||
for (int i : input) {
|
||||
if (pairs.containsKey(i)) {
|
||||
if (pairs.get(i) != null) {
|
||||
// Add pair to returned list
|
||||
allDifferentPairs.add(i);
|
||||
}
|
||||
// Mark pair as added to prevent duplicates
|
||||
pairs.put(sum - i, null);
|
||||
} else if (!pairs.containsValue(i)) {
|
||||
// Add pair to aux. hash map
|
||||
pairs.put(sum - i, i);
|
||||
}
|
||||
}
|
||||
return allDifferentPairs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show all different pairs using Java 8 stream API
|
||||
*
|
||||
* @param input - number's array
|
||||
* @param sum - given sum
|
||||
* @return - number's array with all existing pairs. This list will contain just one pair's element because
|
||||
* the other one can be calculated with SUM - element_1 = element_2
|
||||
*/
|
||||
public static List<Integer> findPairsWithStreamApi(int[] input, int sum) {
|
||||
final List<Integer> allDifferentPairs = new ArrayList<>();
|
||||
// Aux. hash map
|
||||
final Map<Integer, Integer> pairs = new HashMap();
|
||||
IntStream.range(0, input.length).forEach(i -> {
|
||||
if (pairs.containsKey(input[i])) {
|
||||
if (pairs.get(input[i]) != null) {
|
||||
// Add pair to returned list
|
||||
allDifferentPairs.add(input[i]);
|
||||
}
|
||||
// Mark pair as added to prevent duplicates
|
||||
pairs.put(sum - input[i], null);
|
||||
} else if (!pairs.containsValue(input[i])) {
|
||||
// Add pair to aux. hash map
|
||||
pairs.put(sum - input[i], input[i]);
|
||||
}
|
||||
}
|
||||
);
|
||||
return allDifferentPairs;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.algorithms.pairsaddupnumber;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* Find all existing pairs of numbers in an array that add up to a given sum - Complexity O(n^2) "Brute force"
|
||||
*/
|
||||
public class ExistingPairs {
|
||||
|
||||
/**
|
||||
* Show all existing pairs using traditional "for" loop
|
||||
*
|
||||
* @param input - number's array
|
||||
* @param sum - given sum
|
||||
* @return - number's array with all existing pairs. This list will contain just one pair's element because
|
||||
* the other one can be calculated with SUM - element_1 = element_2
|
||||
*/
|
||||
public static List<Integer> findPairsWithForLoop(int[] input, int sum) {
|
||||
final List<Integer> allExistingPairs = new ArrayList<>();
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
for (int j = 0; j < input.length; j++) {
|
||||
if (j != i && (input[i] + input[j]) == sum) {
|
||||
allExistingPairs.add(input[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return allExistingPairs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show all existing pairs using Java 8 stream API
|
||||
*
|
||||
* @param input - number's array
|
||||
* @param sum - given sum
|
||||
* @return - number's array with all existing pairs. This list will contain just one pair's element because
|
||||
* the other one can be calculated with SUM - element_1 = element_2
|
||||
*/
|
||||
public static List<Integer> findPairsWithStreamApi(int[] input, int sum) {
|
||||
final List<Integer> allExistingPairs = new ArrayList<>();
|
||||
IntStream.range(0, input.length).forEach(i ->
|
||||
IntStream.range(0, input.length)
|
||||
.filter(j -> i != j && input[i] + input[j] == sum)
|
||||
.forEach(j -> allExistingPairs.add(input[i]))
|
||||
);
|
||||
return allExistingPairs;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
package com.baeldung.algorithms.pairsaddupnumber;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
|
||||
public class FindPairs {
|
||||
|
||||
public void execute(int[] input, int sum) {
|
||||
final StringBuilder inputArray = new StringBuilder();
|
||||
inputArray.append("{");
|
||||
IntStream.range(0, input.length).forEach(i -> inputArray.append(input[i] + ", "));
|
||||
inputArray.append("}");
|
||||
System.out.println(" Given number array: " + inputArray.toString());
|
||||
System.out.println(" Given sum: " + sum);
|
||||
/* Call services */
|
||||
getDifferentPairs(input, sum);
|
||||
getExistingPairs(input, sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print all existing pairs for the given inputs: input array & sum number
|
||||
*/
|
||||
private static void getExistingPairs(int[] input, int sum) {
|
||||
List<Integer> pairs = new ArrayList<>();
|
||||
System.out.println("~ All existing pairs ~");
|
||||
|
||||
/* Traditional FOR loop */
|
||||
// Call method
|
||||
pairs = ExistingPairs.findPairsWithForLoop(input, sum);
|
||||
// Create a pretty printing
|
||||
final StringBuilder output1 = new StringBuilder();
|
||||
pairs.forEach((pair) -> output1.append("{" + pair + ", " + (sum - pair) + "}, "));
|
||||
// Print result
|
||||
System.out.println("Traditional \"for\" loop: " + output1.toString().substring(0, output1.length() - 2));
|
||||
|
||||
/* Java 8 stream API */
|
||||
// Call the method
|
||||
pairs = ExistingPairs.findPairsWithStreamApi(input, sum);
|
||||
// Create a pretty printing
|
||||
final StringBuilder output2 = new StringBuilder();
|
||||
pairs.forEach((pair) -> output2.append("{" + pair + ", " + (sum - pair) + "}, "));
|
||||
// Print result
|
||||
System.out.println("Java 8 streams API: " + output2.toString().substring(0, output2.length() - 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print all different pairs for the given inputs: input array & sum number
|
||||
*/
|
||||
private static void getDifferentPairs(int[] input, int sum) {
|
||||
List<Integer> pairs = new ArrayList<>();
|
||||
System.out.println("~ All different pairs ~");
|
||||
|
||||
/* Traditional FOR loop */
|
||||
// Call method
|
||||
pairs = DifferentPairs.findPairsWithForLoop(input, sum);
|
||||
// Create a pretty printing
|
||||
final StringBuilder output3 = new StringBuilder();
|
||||
pairs.forEach((pair) -> output3.append("{" + pair + ", " + (sum - pair) + "}, "));
|
||||
// Print result
|
||||
System.out.println("Traditional \"for\" loop: " + output3.toString().substring(0, output3.length() - 2));
|
||||
|
||||
/* Java 8 stream API */
|
||||
// Call method
|
||||
pairs = DifferentPairs.findPairsWithStreamApi(input, sum);
|
||||
// Create a pretty printing
|
||||
final StringBuilder output4 = new StringBuilder();
|
||||
pairs.forEach((pair) -> output4.append("{" + pair + ", " + (sum - pair) + "}, "));
|
||||
// Print result
|
||||
System.out.println("Java 8 streams API: " + output4.toString().substring(0, output4.length() - 2));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.algorithms.pairsaddupnumber;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class DifferentPairsUnitTest {
|
||||
|
||||
/* All different pairs */
|
||||
|
||||
@Test
|
||||
public void whenTraditionalLoop_thenReturnAllDifferentPairs() {
|
||||
/* Data */
|
||||
final int[] input = {2, 4, 3, 3, 8};
|
||||
final int sum = 6;
|
||||
/* Call service */
|
||||
final List<Integer> pairs = DifferentPairs.findPairsWithForLoop(input, sum);
|
||||
/* Check results */
|
||||
assertThat(pairs).hasSize(2).contains(4,3).doesNotContain(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStreamApi_thenReturnAllDifferentPairs() {
|
||||
/* Data */
|
||||
final int[] input = {2, 4, 3, 3, 8};
|
||||
final int sum = 6;
|
||||
/* Call service */
|
||||
final List<Integer> pairs = DifferentPairs.findPairsWithStreamApi(input, sum);
|
||||
/* Check results */
|
||||
assertNotNull(pairs);
|
||||
assertEquals(pairs.size(),2);
|
||||
assertEquals(pairs.get(0), new Integer(4));
|
||||
assertThat(pairs).hasSize(2).contains(4,3).doesNotContain(8);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.algorithms.pairsaddupnumber;
|
||||
|
||||
import org.junit.Test;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class ExistingPairsUnitTest {
|
||||
|
||||
/* All existing pairs */
|
||||
|
||||
@Test
|
||||
public void whenTraditionalLoop_thenReturnAllExistingPairs() {
|
||||
/* Data */
|
||||
final int[] input = {2, 4, 3, 3, 8};
|
||||
final int sum = 6;
|
||||
/* Call service */
|
||||
final List<Integer> pairs = ExistingPairs.findPairsWithForLoop(input, sum);
|
||||
/* Check results */
|
||||
assertThat(pairs).hasSize(4).contains(2,4,3,3).doesNotContain(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStreamApi_thenReturnAllExistingPairs() {
|
||||
/* Data */
|
||||
final int[] input = {2, 4, 3, 3, 8};
|
||||
final int sum = 6;
|
||||
/* Call service */
|
||||
final List<Integer> pairs = ExistingPairs.findPairsWithStreamApi(input, sum);
|
||||
/* Check results */
|
||||
assertThat(pairs).hasSize(4).contains(2,4,3,3).doesNotContain(8);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue