Skip to content

Commit

Permalink
Adding half-square runner solution
Browse files Browse the repository at this point in the history
  • Loading branch information
josdem committed Apr 19, 2024
1 parent d87b47b commit 9c5d2a5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
21 changes: 20 additions & 1 deletion big-o/src/main/java/com/josdem/algorithms/HalfSquareRunner.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package com.josdem.algorithms;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/* Type: Square Time Algorithms – O(n^2/2)
Description: Half-square increment amount of time based on collection size
*/

public class HalfSquareRunner {
private final Logger log = Logger.getLogger(this.getClass().getName());
public List<Integer> getTargetNumbers(List<Integer> numbers, int target) {
return numbers;
List<Integer> result = new ArrayList<>();
for(int i=0; i < numbers.size(); i++){
for(int j = i + 1; j < numbers.size(); j++){
int a = numbers.get(i);
int b = numbers.get(j);
if (a + b == target){
result.add(a);
result.add(b);
log.info(a + "," + b);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/* Type: Square Time Algorithms – O(n^2)
Description: Square increment amount of time based on collection size
*/

public class SquareTimeRunner {

private final Logger log = Logger.getLogger(this.getClass().getName());

public List<Integer> getTargetNumbers(List<Integer> numbers, int target) {
List<Integer> result = new ArrayList<>();
numbers.forEach(a -> {
numbers.forEach(b -> {
if (a + b == target) {
result.add(a);
result.add(b);
log.info(a + "," + b);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HalfSquareRunnerTest {
@DisplayName("show half-square algorithm")
void shouldTestHalfSquare(){
List<Integer> numbers = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> expectedNumbers = List.of(0, 5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0);
List<Integer> expectedNumbers = List.of(0, 5, 1, 4, 2, 3);
int target = 5;
assertEquals(expectedNumbers, halfSquareRunner.getTargetNumbers(numbers, target), "should get numbers from the array");
}
Expand Down

0 comments on commit 9c5d2a5

Please sign in to comment.