Skip to content

Commit cbea63e

Browse files
authored
Merge branch 'master' into iterative_ternary_add_tests
2 parents 203a058 + 0a7065d commit cbea63e

File tree

60 files changed

+2998
-494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2998
-494
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
uses: actions/setup-java@v4
1111
with:
1212
java-version: 21
13-
distribution: 'adopt'
13+
distribution: 'temurin'
1414
- name: Build with Maven
1515
run: mvn --batch-mode --update-snapshots verify
1616
- name: Upload coverage to codecov (tokenless)

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
uses: actions/setup-java@v4
3131
with:
3232
java-version: 21
33-
distribution: 'adopt'
33+
distribution: 'temurin'
3434

3535
- name: Initialize CodeQL
3636
uses: github/codeql-action/init@v3

.github/workflows/infer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/setup-java@v4
1919
with:
2020
java-version: 21
21-
distribution: 'adopt'
21+
distribution: 'temurin'
2222

2323
- name: Set up OCaml
2424
uses: ocaml/setup-ocaml@v3

DIRECTORY.md

Lines changed: 35 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Utility class to find the single non-duplicate element from an array
5+
* where all other elements appear twice.
6+
* <p>
7+
* The algorithm runs in O(n) time complexity and O(1) space complexity
8+
* using bitwise XOR.
9+
* </p>
10+
*
11+
* @author <a href="http://github.com/tuhinm2002">Tuhin M</a>
12+
*/
13+
public final class SingleElement {
14+
15+
/**
16+
* Private constructor to prevent instantiation of this utility class.
17+
* Throws an UnsupportedOperationException if attempted.
18+
*/
19+
private SingleElement() {
20+
throw new UnsupportedOperationException("Utility Class");
21+
}
22+
23+
/**
24+
* Finds the single non-duplicate element in an array where every other
25+
* element appears exactly twice. Uses bitwise XOR to achieve O(n) time
26+
* complexity and O(1) space complexity.
27+
*
28+
* @param arr the input array containing integers where every element
29+
* except one appears exactly twice
30+
* @return the single non-duplicate element
31+
*/
32+
public static int findSingleElement(int[] arr) {
33+
int ele = 0;
34+
for (int i = 0; i < arr.length; i++) {
35+
ele ^= arr[i];
36+
}
37+
return ele;
38+
}
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Swap every pair of adjacent bits of a given number.
5+
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
6+
*/
7+
8+
public final class SwapAdjacentBits {
9+
private SwapAdjacentBits() {
10+
}
11+
12+
public static int swapAdjacentBits(int num) {
13+
// mask the even bits (0xAAAAAAAA => 10101010...)
14+
int evenBits = num & 0xAAAAAAAA;
15+
16+
// mask the odd bits (0x55555555 => 01010101...)
17+
int oddBits = num & 0x55555555;
18+
19+
// right shift even bits and left shift odd bits
20+
evenBits >>= 1;
21+
oddBits <<= 1;
22+
23+
// combine shifted bits
24+
return evenBits | oddBits;
25+
}
26+
}
Lines changed: 41 additions & 0 deletions
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

Lines changed: 14 additions & 15 deletions
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();
Lines changed: 44 additions & 71 deletions
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)