Skip to content

Commit 4b2b2b3

Browse files
authored
Merge branch 'master' into boundary_fill_add_tests
2 parents b512e05 + 48c65e4 commit 4b2b2b3

27 files changed

+1366
-200
lines changed

DIRECTORY.md

+11
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
* divideandconquer
230230
* [BinaryExponentiation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/BinaryExponentiation.java)
231231
* [ClosestPair](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/ClosestPair.java)
232+
* [MedianOfTwoSortedArrays](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.java)
232233
* [SkylineAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java)
233234
* [StrassenMatrixMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java)
234235
* dynamicprogramming
@@ -474,6 +475,7 @@
474475
* scheduling
475476
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
476477
* [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
478+
* [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
477479
* [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
478480
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
479481
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
@@ -731,6 +733,7 @@
731733
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
732734
* lists
733735
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
736+
* [CreateAndDetectLoopTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java)
734737
* [QuickSortLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java)
735738
* [ReverseKGroupTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java)
736739
* [RotateSinglyLinkedListsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java)
@@ -775,15 +778,22 @@
775778
* divideandconquer
776779
* [BinaryExponentiationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/BinaryExponentiationTest.java)
777780
* [ClosestPairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/ClosestPairTest.java)
781+
* [MedianOfTwoSortedArraysTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArraysTest.java)
778782
* [SkylineAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/SkylineAlgorithmTest.java)
779783
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
780784
* dynamicprogramming
781785
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
782786
* [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)
787+
* [BruteForceKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsackTest.java)
783788
* [CatalanNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CatalanNumberTest.java)
784789
* [ClimbStairsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/ClimbStairsTest.java)
790+
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CoinChangeTest.java)
791+
* [CountFriendsPairingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CountFriendsPairingTest.java)
792+
* [DPTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/DPTest.java)
785793
* [EditDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EditDistanceTest.java)
786794
* [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
795+
* [FibonacciTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/FibonacciTest.java)
796+
* [KadaneAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithmTest.java)
787797
* [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)
788798
* [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java)
789799
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
@@ -961,6 +971,7 @@
961971
* scheduling
962972
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
963973
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
974+
* [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
964975
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
965976
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
966977
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* @author - https://github.com/Monk-AbhinayVerma
5+
* @Wikipedia - https://en.wikipedia.org/wiki/Ones%27_complement
6+
* The class OnesComplement computes the complement of binary number
7+
* and returns
8+
* the complemented binary string.
9+
* @return the complimented binary string
10+
*/
11+
public final class OnesComplement {
12+
private OnesComplement() {
13+
}
14+
15+
// Function to get the 1's complement of a binary number
16+
public static String onesComplement(String binary) {
17+
StringBuilder complement = new StringBuilder();
18+
// Invert each bit to get the 1's complement
19+
for (int i = 0; i < binary.length(); i++) {
20+
if (binary.charAt(i) == '0') {
21+
complement.append('1');
22+
} else {
23+
complement.append('0');
24+
}
25+
}
26+
return complement.toString();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* @wikipedia - https://en.wikipedia.org/wiki/Two%27s_complement
5+
* This Algorithm was first suggested by Jon Von Neumann
6+
* @author - https://github.com/Monk-AbhinayVerma
7+
* @return the two's complement of any binary number
8+
*/
9+
public final class TwosComplement {
10+
private TwosComplement() {
11+
}
12+
13+
// Function to get the 2's complement of a binary number
14+
public static String twosComplement(String binary) {
15+
StringBuilder onesComplement = new StringBuilder();
16+
// Step 1: Find the 1's complement (invert the bits)
17+
for (int i = 0; i < binary.length(); i++) {
18+
if (binary.charAt(i) == '0') {
19+
onesComplement.append('1');
20+
} else {
21+
onesComplement.append('0');
22+
}
23+
}
24+
// Step 2: Add 1 to the 1's complement
25+
StringBuilder twosComplement = new StringBuilder(onesComplement);
26+
boolean carry = true;
27+
for (int i = onesComplement.length() - 1; i >= 0; i--) {
28+
if (onesComplement.charAt(i) == '1' && carry) {
29+
twosComplement.setCharAt(i, '0');
30+
} else if (onesComplement.charAt(i) == '0' && carry) {
31+
twosComplement.setCharAt(i, '1');
32+
carry = false;
33+
}
34+
}
35+
// If there is still a carry, append '1' at the beginning
36+
if (carry) {
37+
twosComplement.insert(0, '1');
38+
}
39+
return twosComplement.toString();
40+
}
41+
}

src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java

+14-15
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ private ColumnarTranspositionCipher() {
2727
* @return a String with the word encrypted by the Columnar Transposition
2828
* Cipher Rule
2929
*/
30-
public static String encrpyter(String word, String keyword) {
30+
public static String encrypt(final String word, final String keyword) {
3131
ColumnarTranspositionCipher.keyword = keyword;
32-
abecedariumBuilder(500);
32+
abecedariumBuilder();
3333
table = tableBuilder(word);
3434
Object[][] sortedTable = sortTable(table);
3535
StringBuilder wordEncrypted = new StringBuilder();
36-
for (int i = 0; i < sortedTable[i].length; i++) {
36+
for (int i = 0; i < sortedTable[0].length; i++) {
3737
for (int j = 1; j < sortedTable.length; j++) {
3838
wordEncrypted.append(sortedTable[j][i]);
3939
}
@@ -51,11 +51,12 @@ public static String encrpyter(String word, String keyword) {
5151
* @return a String with the word encrypted by the Columnar Transposition
5252
* Cipher Rule
5353
*/
54-
public static String encrpyter(String word, String keyword, String abecedarium) {
54+
public static String encrypt(String word, String keyword, String abecedarium) {
5555
ColumnarTranspositionCipher.keyword = keyword;
5656
ColumnarTranspositionCipher.abecedarium = Objects.requireNonNullElse(abecedarium, ABECEDARIUM);
5757
table = tableBuilder(word);
5858
Object[][] sortedTable = sortTable(table);
59+
5960
StringBuilder wordEncrypted = new StringBuilder();
6061
for (int i = 0; i < sortedTable[0].length; i++) {
6162
for (int j = 1; j < sortedTable.length; j++) {
@@ -72,7 +73,7 @@ public static String encrpyter(String word, String keyword, String abecedarium)
7273
* @return a String decrypted with the word encrypted by the Columnar
7374
* Transposition Cipher Rule
7475
*/
75-
public static String decrypter() {
76+
public static String decrypt() {
7677
StringBuilder wordDecrypted = new StringBuilder();
7778
for (int i = 1; i < table.length; i++) {
7879
for (Object item : table[i]) {
@@ -91,14 +92,14 @@ public static String decrypter() {
9192
*/
9293
private static Object[][] tableBuilder(String word) {
9394
Object[][] table = new Object[numberOfRows(word) + 1][keyword.length()];
94-
char[] wordInChards = word.toCharArray();
95-
// Fils in the respective numbers
95+
char[] wordInChars = word.toCharArray();
96+
// Fills in the respective numbers for the column
9697
table[0] = findElements();
9798
int charElement = 0;
9899
for (int i = 1; i < table.length; i++) {
99100
for (int j = 0; j < table[i].length; j++) {
100-
if (charElement < wordInChards.length) {
101-
table[i][j] = wordInChards[charElement];
101+
if (charElement < wordInChars.length) {
102+
table[i][j] = wordInChars[charElement];
102103
charElement++;
103104
} else {
104105
table[i][j] = ENCRYPTION_FIELD_CHAR;
@@ -116,7 +117,7 @@ private static Object[][] tableBuilder(String word) {
116117
* order to respect the Columnar Transposition Cipher Rule.
117118
*/
118119
private static int numberOfRows(String word) {
119-
if (word.length() / keyword.length() > word.length() / keyword.length()) {
120+
if (word.length() % keyword.length() != 0) {
120121
return (word.length() / keyword.length()) + 1;
121122
} else {
122123
return word.length() / keyword.length();
@@ -173,13 +174,11 @@ private static void switchColumns(Object[][] table, int firstColumnIndex, int se
173174
}
174175

175176
/**
176-
* Creates an abecedarium with a specified ascii inded
177-
*
178-
* @param value Number of characters being used based on the ASCII Table
177+
* Creates an abecedarium with all available ascii values.
179178
*/
180-
private static void abecedariumBuilder(int value) {
179+
private static void abecedariumBuilder() {
181180
StringBuilder t = new StringBuilder();
182-
for (int i = 0; i < value; i++) {
181+
for (int i = 0; i < 256; i++) {
183182
t.append((char) i);
184183
}
185184
abecedarium = t.toString();
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
11
package com.thealgorithms.datastructures.lists;
22

3-
import java.util.Scanner;
4-
53
public final class CreateAndDetectLoop {
4+
5+
// Node class representing a single node in the linked list
66
private CreateAndDetectLoop() {
7+
throw new UnsupportedOperationException("Utility class");
78
}
9+
static final class Node {
10+
int data;
11+
Node next;
812

9-
/**
10-
* Prints the linked list.
11-
*
12-
* @param head head node of the linked list
13-
*/
14-
static void printList(Node head) {
15-
Node cur = head;
16-
17-
while (cur != null) {
18-
System.out.print(cur.value + " ");
19-
cur = cur.next;
13+
Node(int data) {
14+
this.data = data;
15+
next = null;
2016
}
2117
}
2218

23-
/**
24-
* Creates a loop in the linked list.
25-
*
26-
* @see
27-
* <a href="https://www.geeksforgeeks.org/make-loop-k-th-position-linked-list/">
28-
* GeeksForGeeks: Make a loop at K-th position</a>
29-
* @param head head node of the linked list
30-
* @param k position of node where loop is to be created
19+
// Method to create a loop between two specific positions in the linked list
20+
/*
21+
* Test case that shows the Cycle(loop) in a LinkedList
22+
* Let's take this linked list:
23+
* 1->2->3->4->5->6
24+
* \______/
25+
* In this linked list we can see there is a cycle.
26+
* we can create loop by calling createLoop function in main after creating LL
27+
* createLoop(head,2,5);
28+
* to detect there is loop or not we can call detectloop function in main
29+
* detectloop(head);
3130
*/
32-
static void createLoop(Node head, int k) {
33-
if (head == null) {
31+
32+
static void createLoop(Node head, int position1, int position2) {
33+
if (position1 == 0 || position2 == 0) {
3434
return;
3535
}
36-
Node temp = head;
37-
int count = 1;
38-
while (count < k) { // Traverse the list till the kth node
39-
temp = temp.next;
40-
count++;
41-
}
4236

43-
Node connectedPoint = temp;
37+
Node node1 = head;
38+
Node node2 = head;
4439

45-
while (temp.next != null) { // Traverse remaining nodes
46-
temp = temp.next;
40+
int count1 = 1;
41+
int count2 = 1;
42+
// Traverse to find node at position1
43+
while (count1 < position1 && node1 != null) {
44+
node1 = node1.next;
45+
count1++;
4746
}
4847

49-
temp.next = connectedPoint; // Connect last node to k-th element
50-
}
48+
// Traverse to find node at position2
49+
while (count2 < position2 && node2 != null) {
50+
node2 = node2.next;
51+
count2++;
52+
}
5153

54+
// Create a loop by connecting node2's next to node1
55+
if (node1 != null && node2 != null) {
56+
node2.next = node1;
57+
}
58+
}
59+
// Method to detect a loop in the linked list
5260
/**
5361
* Detects the presence of a loop in the linked list.
5462
*
55-
* @see
56-
* <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">
57-
* Floyd's Cycle Detection Algorithm</a>
58-
*
59-
* @param head the head node of the linked list
60-
*
63+
* @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">Floyd's Cycle Detection Algorithm</a>
6164
* @return true if loop exists else false
6265
*/
6366
static boolean detectLoop(Node head) {
@@ -67,40 +70,10 @@ static boolean detectLoop(Node head) {
6770
while (fptr != null && fptr.next != null) {
6871
sptr = sptr.next;
6972
fptr = fptr.next.next;
70-
if (fptr == sptr) {
73+
if (sptr == fptr) {
7174
return true;
7275
}
7376
}
74-
7577
return false;
7678
}
77-
78-
public static void main(String[] args) {
79-
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
80-
Scanner sc = new Scanner(System.in);
81-
82-
System.out.println("Enter the number of elements to be inserted: ");
83-
int n = sc.nextInt();
84-
System.out.printf("Enter the %d elements: %n", n);
85-
while (n-- > 0) {
86-
singlyLinkedList.insert(sc.nextInt());
87-
}
88-
89-
System.out.print("Given list: ");
90-
printList(singlyLinkedList.getHead());
91-
System.out.println();
92-
93-
System.out.println("Enter the location to generate loop: ");
94-
int k = sc.nextInt();
95-
96-
createLoop(singlyLinkedList.getHead(), k);
97-
98-
if (detectLoop(singlyLinkedList.getHead())) {
99-
System.out.println("Loop found");
100-
} else {
101-
System.out.println("No loop found");
102-
}
103-
104-
sc.close();
105-
}
10679
}

0 commit comments

Comments
 (0)