diff --git a/checkstyle.xml b/checkstyle.xml
index 444b366ddee7..ce584392b089 100644
--- a/checkstyle.xml
+++ b/checkstyle.xml
@@ -164,7 +164,7 @@
-
+
diff --git a/pmd-exclude.properties b/pmd-exclude.properties
index f2e35f43887e..8722f126f6d5 100644
--- a/pmd-exclude.properties
+++ b/pmd-exclude.properties
@@ -33,7 +33,6 @@ com.thealgorithms.devutils.nodes.SimpleTreeNode=UselessParentheses
com.thealgorithms.devutils.nodes.TreeNode=UselessParentheses
com.thealgorithms.divideandconquer.ClosestPair=UnnecessaryFullyQualifiedName,UselessParentheses
com.thealgorithms.divideandconquer.Point=UselessParentheses
-com.thealgorithms.dynamicprogramming.KnapsackMemoization=UselessParentheses
com.thealgorithms.dynamicprogramming.MatrixChainMultiplication=UselessParentheses
com.thealgorithms.dynamicprogramming.ShortestSuperSequence=UselessParentheses
com.thealgorithms.dynamicprogramming.UniquePaths=UnnecessarySemicolon
diff --git a/src/main/java/com/thealgorithms/ciphers/ProductCipher.java b/src/main/java/com/thealgorithms/ciphers/ProductCipher.java
index fb49d6cd61db..fb63ed9b6ef9 100644
--- a/src/main/java/com/thealgorithms/ciphers/ProductCipher.java
+++ b/src/main/java/com/thealgorithms/ciphers/ProductCipher.java
@@ -26,8 +26,8 @@ public static void main(String[] args) {
// Transposition encryption
String transpositionInput = substitutionOutput.toString();
- int modulus;
- if ((modulus = transpositionInput.length() % n) != 0) {
+ int modulus = transpositionInput.length() % n;
+ if (modulus != 0) {
modulus = n - modulus;
for (; modulus != 0; modulus--) {
diff --git a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java
index 186f301f622d..cfec2e3b2c37 100644
--- a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java
+++ b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java
@@ -121,7 +121,8 @@ private void fastRemove(final Object[] elements, final int index) {
System.arraycopy(elements, index + 1, elements, index, newSize - index);
}
- elements[this.size = newSize] = null;
+ this.size = newSize;
+ this.elements[this.size] = null;
}
private E getElement(final int index) {
diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java
index 0e4bcc2c8b80..d21f8d6e71dc 100644
--- a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java
+++ b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java
@@ -20,7 +20,8 @@ private final class Node {
// Node constructor setting the data element and left/right pointers to null
private Node(int element) {
this.element = element;
- left = right = null;
+ left = null;
+ right = null;
npl = 0;
}
}
diff --git a/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java b/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java
index 9530c5a69d8c..cd3761bdcf75 100644
--- a/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java
+++ b/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java
@@ -54,7 +54,8 @@ public int deQueue() {
int res = arr[beginningOfQueue];
arr[beginningOfQueue] = Integer.MIN_VALUE;
if (beginningOfQueue == topOfQueue) {
- beginningOfQueue = topOfQueue = -1;
+ beginningOfQueue = -1;
+ topOfQueue = -1;
} else if (beginningOfQueue + 1 == size) {
beginningOfQueue = 0;
} else {
diff --git a/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java b/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java
index b552ac2918a3..8a788317c372 100644
--- a/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java
+++ b/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java
@@ -44,7 +44,9 @@ static class Node {
* Init LinkedQueue
*/
public LinkedQueue() {
- front = rear = new Node<>();
+
+ front = new Node<>();
+ rear = front;
}
/**
@@ -146,7 +148,8 @@ public boolean hasNext() {
@Override
public T next() {
- return (node = node.next).data;
+ node = node.next;
+ return node.data;
}
};
}
diff --git a/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java b/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java
index e30a52929519..0fcf1324a4ce 100644
--- a/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java
+++ b/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java
@@ -15,7 +15,8 @@ class TreeNode {
// Constructor
TreeNode(int key) {
this.key = key;
- left = right = null;
+ left = null;
+ right = null;
}
}
diff --git a/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java b/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java
index ebdcba40ae7c..f2954f28495f 100644
--- a/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java
+++ b/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java
@@ -201,7 +201,8 @@ Node treeMinimum(Node subTreeRoot) {
}
boolean delete(Node z) {
- if ((z = findNode(z, root)) == null) {
+ Node result = findNode(z, root);
+ if (result == null) {
return false;
}
Node x;
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java b/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java
index 290e98caebae..396efb1a7893 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java
@@ -40,8 +40,15 @@ int solveKnapsackRecursive(int capacity, int[] weights, int[] profits, int numOf
dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
return dpTable[numOfItems][capacity];
} else {
- // Return value of table after storing
- return dpTable[numOfItems][capacity] = Math.max((profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable)), solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable));
+ // case 1. include the item, if it is less than the capacity
+ final int includeCurrentItem = profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable);
+
+ // case 2. exclude the item if it is more than the capacity
+ final int excludeCurrentItem = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
+
+ // Store the value of function call stack in table and return
+ dpTable[numOfItems][capacity] = Math.max(includeCurrentItem, excludeCurrentItem);
+ return dpTable[numOfItems][capacity];
}
}
}
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java
index c5b7ea2b9a18..51a78a853e03 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java
@@ -34,7 +34,8 @@ static int AlternatingLength(int[] arr, int n) {
int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence
for (int i = 0; i < n; i++) {
- las[i][0] = las[i][1] = 1;
+ las[i][0] = 1;
+ las[i][1] = 1;
}
int result = 1; // Initialize result
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java
index ff67500b0585..7bc383656581 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java
@@ -15,7 +15,8 @@ private NewManShanksPrime() {
public static boolean nthManShanksPrime(int n, int expected_answer) {
int[] a = new int[n + 1];
// array of n+1 size is initialized
- a[0] = a[1] = 1;
+ a[0] = 1;
+ a[1] = 1;
// The 0th and 1st index position values are fixed. They are initialized as 1
for (int i = 2; i <= n; i++) {
a[i] = 2 * a[i - 1] + a[i - 2];
diff --git a/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java b/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java
index ae887470fa33..228ff0b50b2b 100644
--- a/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java
+++ b/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java
@@ -36,9 +36,11 @@ public static int lcm(int num1, int num2) {
* value selection for the numerator
*/
if (num1 > num2) {
- high = num3 = num1;
+ high = num1;
+ num3 = num1;
} else {
- high = num3 = num2;
+ high = num2;
+ num3 = num2;
}
while (num1 != 0) {
diff --git a/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java b/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java
index 1fd9ae288920..6a3412500d11 100644
--- a/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java
+++ b/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java
@@ -19,7 +19,8 @@ private static Type[] sievePrimesTill(int n) {
checkInput(n);
Type[] isPrimeArray = new Type[n + 1];
Arrays.fill(isPrimeArray, Type.PRIME);
- isPrimeArray[0] = isPrimeArray[1] = Type.NOT_PRIME;
+ isPrimeArray[0] = Type.NOT_PRIME;
+ isPrimeArray[1] = Type.NOT_PRIME;
double cap = Math.sqrt(n);
for (int i = 2; i <= cap; i++) {
diff --git a/src/main/java/com/thealgorithms/searches/UnionFind.java b/src/main/java/com/thealgorithms/searches/UnionFind.java
index fc5dbd801ffa..2effdf37bea5 100644
--- a/src/main/java/com/thealgorithms/searches/UnionFind.java
+++ b/src/main/java/com/thealgorithms/searches/UnionFind.java
@@ -25,7 +25,10 @@ public int find(int i) {
return i;
}
- return p[i] = find(parent);
+ final int result = find(parent);
+ p[i] = result;
+
+ return result;
}
public void union(int x, int y) {