ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java (Java)

Code before:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class KnapsackTest {
    @Test
    public void testKnapSackBasic() {
        int[] weights = {2, 3, 4, 5};
        int[] values = {3, 4, 5, 6};
        int weightCapacity = 5;
        int expected = 7; // Maximum value should be 7 (items 1 and 4).
        int result = Knapsack.knapSack(weightCapacity, weights, values);
        assertEquals(expected, result);
    }

    @Test
    public void testKnapSackEmpty() {
        int[] weights = {};
        int[] values = {};
        int weightCapacity = 10;
        int expected = 0; // With no items, the result should be 0.
        int result = Knapsack.knapSack(weightCapacity, weights, values);
        assertEquals(expected, result);
    }

    @Test

Review comment (suggestion):
```suggestion
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
```

Code after:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

public class KnapsackTest {
    @Test
    public void testKnapSackBasic() {
        int[] weights = {2, 3, 4, 5};
        int[] values = {3, 4, 5, 6};
        int weightCapacity = 5;
        int expected = 7; // Maximum value should be 7 (items 1 and 4).
        int result = Knapsack.knapSack(weightCapacity, weights, values);
        assertEquals(expected, result);
    }

    @Test
    public void testKnapSackEmpty() {
        int[] weights = {};
        int[] values = {};
        int weightCapacity = 10;
        int expected = 0; // With no items, the result should be 0.
        int result = Knapsack.knapSack(weightCapacity, weights, values);
        assertEquals(expected, result);
    }
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java (Java)

Code before:
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 5, 6}; // Different length values array.
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }

    @Test
    public void testKnapSackThrowsForNullInputs() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 6};
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, null, values); });
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, null); });
    }

    @Test
    public void testKnapSackThrowsForNegativeCapacity() {
        int[] weights = {2, 3, 4, 5};
        int[] values = {3, 4, 5, 6};
        int weightCapacity = -5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }

    @Test
    public void testKnapSackThrowsForNegativeWeight() {
        int[] weights = {2, -3, 4};
        int[] values = {3, 4, 6};
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }
}


Review comment (suggestion):
```suggestion
        int[] weights = {2, 0, 4};
```

Code after:
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 5, 6}; // Different length values array.
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }

    @Test
    public void testKnapSackThrowsForNullInputs() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 6};
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, null, values); });
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, null); });
    }

    @Test
    public void testKnapSackThrowsForNegativeCapacity() {
        int[] weights = {2, 3, 4, 5};
        int[] values = {3, 4, 5, 6};
        int weightCapacity = -5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }

    @Test
    public void testKnapSackThrowsForNegativeWeight() {
        int[] weights = {2, 0, 4};
        int[] values = {3, 4, 6};
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java (Java)

Code before:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class UniquePathsTests {

    @Test
    public void testUniquePaths_3x3() {
        int result = UniquePaths.uniquePaths(3, 3);
        assertEquals(6, result);
    }

    @Test
    public void testUniquePaths_1x1() {
        int result = UniquePaths.uniquePaths(1, 1);
        assertEquals(1, result);
    }

    @Test
    public void testUniquePaths_3x7() {
        int result = UniquePaths.uniquePaths(3, 7);
        assertEquals(28, result);
    }

    @Test
    public void testUniquePaths_7x3() {

Review comment (suggestion):
```suggestion
import static org.junit.jupiter.api.Assertions.assertEquals;
```

Code after:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class UniquePathsTests {

    @Test
    public void testUniquePaths_3x3() {
        assertEquals(6, UniquePaths.uniquePaths(3, 3));
    }

    @Test
    public void testUniquePaths_1x1() {
        assertEquals(1, UniquePaths.uniquePaths(1, 1));
    }

    @Test
    public void testUniquePaths_3x7() {
        assertEquals(28, UniquePaths.uniquePaths(3, 7));
    }

    @Test
    public void testUniquePaths_7x3() {
        assertEquals(28, UniquePaths.uniquePaths(7, 3));
    }
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java (Java)

Code before:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class UniquePathsTests {

    @Test
    public void testUniquePaths_3x3() {
        int result = UniquePaths.uniquePaths(3, 3);
        assertEquals(6, result);
    }

    @Test
    public void testUniquePaths_1x1() {
        int result = UniquePaths.uniquePaths(1, 1);
        assertEquals(1, result);
    }

    @Test
    public void testUniquePaths_3x7() {
        int result = UniquePaths.uniquePaths(3, 7);
        assertEquals(28, result);
    }

    @Test
    public void testUniquePaths_7x3() {
        int result = UniquePaths.uniquePaths(7, 3);
        assertEquals(28, result);
    }

    @Test
    public void testUniquePaths2_3x3() {
        int result = UniquePaths.uniquePaths2(3, 3);
        assertEquals(6, result);
    }

Review comment (suggestion):
```suggestion
        assertEquals(6, UniquePaths.uniquePaths(3, 3));
```

I suggest a similar change in other tests.

Code after:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class UniquePathsTests {

    @Test
    public void testUniquePaths_3x3() {
        assertEquals(6, UniquePaths.uniquePaths(3, 3));
    }

    @Test
    public void testUniquePaths_1x1() {
        assertEquals(1, UniquePaths.uniquePaths(1, 1));
    }

    @Test
    public void testUniquePaths_3x7() {
        assertEquals(28, UniquePaths.uniquePaths(3, 7));
    }

    @Test
    public void testUniquePaths_7x3() {
        assertEquals(28, UniquePaths.uniquePaths(7, 3));
    }

    @Test
    public void testUniquePaths_100x100() {
        assertThrows(ArithmeticException.class, () -> UniquePaths.uniquePaths(100, 100));
    }

    @Test
    public void testUniquePaths2_3x3() {
        assertEquals(6, UniquePaths.uniquePaths2(3, 3));
    }
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java (Java)

Code before:
     */
    public void insert(Integer e) {
        if (!minHeap.isEmpty() && e < minHeap.peek()) {
            maxHeap.offer(e);
            if (maxHeap.size() > minHeap.size() + 1) {
                minHeap.offer(maxHeap.poll());
            }
        } else {
            minHeap.offer(e);
            if (minHeap.size() > maxHeap.size() + 1) {
                maxHeap.offer(minHeap.poll());
            }
        }
    }

    /*
      Returns median at any given point
     */
    public Double median() {
        if (maxHeap.size() == minHeap.size()) {
            return (maxHeap.peek() + minHeap.peek()) / 2.0;
        }
        return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
    }

    public static void main(String[] args) {
        /*
        Testing the median function
         */

        MedianOfRunningArray p = new MedianOfRunningArray();
        int[] arr = {30, 20, 10};
        for (int i = 0; i < arr.length; i++) {
            p.insert(arr[i]);
            System.out.print(p.median() + " ");
        }
    }
}


Review comment (suggestion):
Please completely remove the `main` method and add some proper tests. Good examples to look at are [`Factorial`](https://github.com/TheAlgorithms/Java/blob/34cf6dab28ee2271fbf228d345bcf14db85c211c/src/main/java/com/thealgorithms/maths/Factorial.java) and [`FactorialTest`](https://github.com/TheAlgorithms/Java/blob/34cf6dab28ee2271fbf228d345bcf14db85c211c/src/test/java/com/thealgorithms/maths/FactorialTest.java).

I think it is essential here to fully test this class.

Code after:
     */
    public void insert(Integer e) {
        if (!minHeap.isEmpty() && e < minHeap.peek()) {
            maxHeap.offer(e);
            if (maxHeap.size() > minHeap.size() + 1) {
                minHeap.offer(maxHeap.poll());
            }
        } else {
            minHeap.offer(e);
            if (minHeap.size() > maxHeap.size() + 1) {
                maxHeap.offer(minHeap.poll());
            }
        }
    }

    /*
      Returns median at any given point
     */
    public double median() {
        if (maxHeap.isEmpty() && minHeap.isEmpty()) {
            throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
        }

        if (maxHeap.size() == minHeap.size()) {
            return (maxHeap.peek() + minHeap.peek()) / 2.0;
        }
        return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java (Java)

Code before:
        this.maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // Max Heap
        this.minHeap = new PriorityQueue<>(); // Min Heap
    }

    /*
      Inserting lower half of array to max Heap
      and upper half to min heap
     */
    public void insert(Integer e) {
        if (!minHeap.isEmpty() && e < minHeap.peek()) {
            maxHeap.offer(e);
            if (maxHeap.size() > minHeap.size() + 1) {
                minHeap.offer(maxHeap.poll());
            }
        } else {
            minHeap.offer(e);
            if (minHeap.size() > maxHeap.size() + 1) {
                maxHeap.offer(minHeap.poll());
            }
        }
    }

    /*
      Returns median at any given point
     */
    public Double median() {
        if (maxHeap.size() == minHeap.size() && maxHeap.size() > 0) {
            return (maxHeap.peek() + minHeap.peek()) / 2.0;
        } else if (maxHeap.size() == minHeap.size()) {
            throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
        }
        return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
    }
}


Review comment (suggestion):
Is there a reason why `Double` (**not** `double`) is used here?

```suggestion
    public double median() {
```

Code after:
        this.maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // Max Heap
        this.minHeap = new PriorityQueue<>(); // Min Heap
    }

    /*
      Inserting lower half of array to max Heap
      and upper half to min heap
     */
    public void insert(Integer e) {
        if (!minHeap.isEmpty() && e < minHeap.peek()) {
            maxHeap.offer(e);
            if (maxHeap.size() > minHeap.size() + 1) {
                minHeap.offer(maxHeap.poll());
            }
        } else {
            minHeap.offer(e);
            if (minHeap.size() > maxHeap.size() + 1) {
                maxHeap.offer(minHeap.poll());
            }
        }
    }

    /*
      Returns median at any given point
     */
    public double median() {
        if (maxHeap.isEmpty() && minHeap.isEmpty()) {
            throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
        }

        if (maxHeap.size() == minHeap.size()) {
            return (maxHeap.peek() + minHeap.peek()) / 2.0;
        }
        return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java (Java)

Code before:
      and upper half to min heap
     */
    public void insert(Integer e) {
        if (!minHeap.isEmpty() && e < minHeap.peek()) {
            maxHeap.offer(e);
            if (maxHeap.size() > minHeap.size() + 1) {
                minHeap.offer(maxHeap.poll());
            }
        } else {
            minHeap.offer(e);
            if (minHeap.size() > maxHeap.size() + 1) {
                maxHeap.offer(minHeap.poll());
            }
        }
    }

    /*
      Returns median at any given point
     */
    public Double median() {
        if (maxHeap.size() == minHeap.size() && maxHeap.size() > 0) {
            return (maxHeap.peek() + minHeap.peek()) / 2.0;
        } else if (maxHeap.size() == minHeap.size()) {
            throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
        }
        return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
    }
}


Review comment (refactor):
I think one can simplify the logic a bit

```suggestion
        if (maxHeap.isEmpty() && minHeap.isEmpty()) {
            throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
        }

        if (maxHeap.size() == minHeap.size()) {
            return (maxHeap.peek() + minHeap.peek()) / 2.0;
        }
        return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
```

Code after:
      and upper half to min heap
     */
    public void insert(Integer e) {
        if (!minHeap.isEmpty() && e < minHeap.peek()) {
            maxHeap.offer(e);
            if (maxHeap.size() > minHeap.size() + 1) {
                minHeap.offer(maxHeap.poll());
            }
        } else {
            minHeap.offer(e);
            if (minHeap.size() > maxHeap.size() + 1) {
                maxHeap.offer(minHeap.poll());
            }
        }
    }

    /*
      Returns median at any given point
     */
    public double median() {
        if (maxHeap.isEmpty() && minHeap.isEmpty()) {
            throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
        }

        if (maxHeap.size() == minHeap.size()) {
            return (maxHeap.peek() + minHeap.peek()) / 2.0;
        }
        return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java (Java)

Code before:
package com.thealgorithms.dynamicprogramming;

import java.util.Arrays;

/*
Given an array of non-negative integers , partition the array in two subset that
difference in sum of elements for both subset minimum.
Return the minimum difference in sum of these subsets you can achieve.

Input:  array[] = {1, 6, 11, 4}
Output: 0
Explanation:
Subset1 = {1, 4, 6}, sum of Subset1 = 11
Subset2 = {11}, sum of Subset2 = 11

Input:  array[] = {36, 7, 46, 40}
Output: 23
Explanation:
Subset1 = {7, 46} ;  sum of Subset1 = 53
Subset2 = {36, 40} ; sum of Subset2  = 76
 */
public final class MinimumSumPartition {
    private MinimumSumPartition() {
    }

    public static int minimumSumPartition(final int[] array) {
        int sum = Arrays.stream(array).sum();
        boolean[] dp = new boolean[sum / 2 + 1];
        dp[0] = true; // Base case , don't select any element from array

        // Find the closest sum of subset array that we can achieve which is closest to half of sum of full array

Review comment (suggestion):
If it is only for non-negative numbers, please check the input array and throw `IllegalArgumentException` when necessary, cf. https://github.com/TheAlgorithms/Java/pull/4368#issuecomment-1719945280

Code after:
package com.thealgorithms.dynamicprogramming;

import java.util.Arrays;

/*
Given an array of non-negative integers , partition the array in two subset that
difference in sum of elements for both subset minimum.
Return the minimum difference in sum of these subsets you can achieve.

Input:  array[] = {1, 6, 11, 4}
Output: 0
Explanation:
Subset1 = {1, 4, 6}, sum of Subset1 = 11
Subset2 = {11}, sum of Subset2 = 11

Input:  array[] = {36, 7, 46, 40}
Output: 23
Explanation:
Subset1 = {7, 46} ;  sum of Subset1 = 53
Subset2 = {36, 40} ; sum of Subset2  = 76
 */
public final class MinimumSumPartition {
    private MinimumSumPartition() {
    }

    private static void throwIfInvalidInput(final int[] array) {
        if (Arrays.stream(array).anyMatch(a -> a < 0)) {
            throw new IllegalArgumentException("Input array should not contain negative number(s).");
        }
    }
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java (Java)

Code before:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class MinimumSumPartitionTest {
    @Test
    public void testMinimumSumPartitionWithEvenSum() {
        int[] array = {1, 6, 11, 4};
        assertEquals(0, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithOddSum() {
        int[] array = {36, 7, 46, 40};
        assertEquals(23, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithSingleElement() {
        int[] array = {7};
        assertEquals(7, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithLargeNumbers() {
        int[] array = {100, 200, 300, 400, 500};

Review comment (suggestion):
```suggestion
import static org.junit.jupiter.api.Assertions.assertEquals;
```

Code after:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class MinimumSumPartitionTest {
    @Test
    public void testMinimumSumPartitionWithEvenSum() {
        int[] array = {1, 6, 11, 4};
        assertEquals(0, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithOddSum() {
        int[] array = {36, 7, 46, 40};
        assertEquals(23, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithSingleElement() {
        int[] array = {7};
        assertEquals(7, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithLargeNumbers() {
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java (Java)

Code before:
    }

    @Test
    public void testMinimumSumPartitionWithSingleElement() {
        int[] array = {7};
        assertEquals(7, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithLargeNumbers() {
        int[] array = {100, 200, 300, 400, 500};
        assertEquals(100, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithEmptyArray() {
        int[] array = {};
        assertEquals(0, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionThrowsForNegativeArray() {
        int[] array = {4, 1, -6, 7};
        assertThrows(IllegalArgumentException.class, () -> {
            MinimumSumPartition.minimumSumPartition(array);
        });
    }
}


Review comment (suggestion):
```suggestion
        assertThrows(IllegalArgumentException.class, () -> { MinimumSumPartition.minimumSumPartition(array); });
```

Code after:
    }

    @Test
    public void testMinimumSumPartitionWithSingleElement() {
        int[] array = {7};
        assertEquals(7, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithLargeNumbers() {
        int[] array = {100, 200, 300, 400, 500};
        assertEquals(100, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionWithEmptyArray() {
        int[] array = {};
        assertEquals(0, MinimumSumPartition.minimumSumPartition(array));
    }

    @Test
    public void testMinimumSumPartitionThrowsForNegativeArray() {
        int[] array = {4, 1, -6, 7};
        assertThrows(IllegalArgumentException.class, () -> { MinimumSumPartition.minimumSumPartition(array); });
    }
}