Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
<module name="EqualsHashCode"/>
<!-- TODO <module name="HiddenField"/> -->
<module name="IllegalInstantiation"/>
<!-- TODO <module name="InnerAssignment"/> -->
<module name="InnerAssignment"/>
<!-- TODO <module name="MagicNumber"/> -->
<!-- TODO <module name="MissingSwitchDefault"/> -->
<!-- TODO <module name="MultipleVariableDeclarations"/> -->
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/ciphers/ProductCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ static class Node<T> {
* Init LinkedQueue
*/
public LinkedQueue() {
front = rear = new Node<>();

front = new Node<>();
rear = front;
}

/**
Expand Down Expand Up @@ -146,7 +148,8 @@ public boolean hasNext() {

@Override
public T next() {
return (node = node.next).data;
node = node.next;
return node.data;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class TreeNode {
// Constructor
TreeNode(int key) {
this.key = key;
left = right = null;
left = null;
right = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ 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
int includeCurrentItem = profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable);
Comment thread
vil02 marked this conversation as resolved.
Outdated


Comment thread
vil02 marked this conversation as resolved.
// case 2. exclude the item if it is more than the capacity
int excludeCurrentItem = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
Comment thread
vil02 marked this conversation as resolved.
Outdated

// Store the value of function call stack in table and return
dpTable[numOfItems][capacity] = Math.max(includeCurrentItem, excludeCurrentItem);
return dpTable[numOfItems][capacity];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/thealgorithms/searches/UnionFind.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public int find(int i) {
return i;
}

return p[i] = find(parent);
int result = find(parent);
Comment thread
vil02 marked this conversation as resolved.
Outdated
p[i] = result;

return result;
}

public void union(int x, int y) {
Expand Down