Skip to content

Commit 1c86778

Browse files
committed
hackerrank: Resolved failing test cases and added a check for mixed input skills
1 parent 3463a5c commit 1c86778

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/main/java/com/shortthirdman/quickstart/hackerrank/TeamEfficiencyCalculator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
public class TeamEfficiencyCalculator {
77

88
public long getTotalEfficiency(List<Integer> skill) {
9-
if (skill == null || skill.size() % 2 != 0) {
9+
if (skill == null || skill.isEmpty() || skill.size() % 2 != 0) {
1010
return -1; // Can't form teams if the list is null or the number of participants isn't even
1111
}
1212

1313
Collections.sort(skill); // Sort the skills to easily form pairs
1414
int n = skill.size();
15+
16+
// Additional check for mixed positive and negative skills
17+
// This ensures that if there are both negative and positive values, pairing is invalid.
18+
if (skill.getFirst() < 0 && skill.get(n - 1) > 0) {
19+
return -1;
20+
}
21+
1522
long totalEfficiency = 0;
1623
int targetSum = skill.getFirst() + skill.get(n - 1); // Calculate the target sum of skills for all pairs
1724

src/test/java/com/shortthirdman/quickstart/hackerrank/TeamEfficiencyCalculatorTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void tearDown() {
2626
public void testValidInput() {
2727
// Test with a valid input where pairs satisfy the condition
2828
List<Integer> skill = Arrays.asList(1, 2, 3, 4);
29-
long expected = 14; // (1 * 4) + (2 * 3) = 14
29+
long expected = 10; // (1 * 4) + (2 * 3) = 10
3030
assertEquals(expected, app.getTotalEfficiency(skill));
3131
}
3232

@@ -52,7 +52,7 @@ public void testEmptyInput() {
5252
@Test
5353
public void testInvalidPairing() {
5454
// Test where pairs cannot form the same sum
55-
List<Integer> skill = Arrays.asList(1, 2, 5, 6);
55+
List<Integer> skill = Arrays.asList(1, 2, 3, 6);
5656
assertEquals(-1, app.getTotalEfficiency(skill));
5757
}
5858

@@ -68,7 +68,7 @@ public void testAllZeroes() {
6868
public void testNegativeSkills() {
6969
// Test with negative skill values
7070
List<Integer> skill = Arrays.asList(-1, -2, -3, -4);
71-
long expected = 14; // (-4 * -1) + (-3 * -2) = 14
71+
long expected = 10; // (-4 * -1) + (-3 * -2) = 10
7272
assertEquals(expected, app.getTotalEfficiency(skill));
7373
}
7474

@@ -84,6 +84,7 @@ public void testLargeInput() {
8484
// Test with a large input size
8585
List<Integer> skill = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80);
8686
long expected = 2200; // For sorted list, pairing and summing the products satisfies
87-
assertEquals(expected, app.getTotalEfficiency(skill));
87+
assertNotEquals(expected, app.getTotalEfficiency(skill));
88+
assertEquals(6000, app.getTotalEfficiency(skill));
8889
}
8990
}

0 commit comments

Comments
 (0)