Skip to content

Commit 96b67b9

Browse files
committed
Merge remote-tracking branch 'origin/median_sorted_arrays_new_algo' into median_sorted_arrays_new_algo
2 parents 100d592 + a15a53a commit 96b67b9

File tree

72 files changed

+4240
-558
lines changed

Some content is hidden

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

72 files changed

+4240
-558
lines changed

DIRECTORY.md

Lines changed: 59 additions & 13 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.11.1</version>
23+
<version>5.11.2</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter</artifactId>
34-
<version>5.11.1</version>
34+
<version>5.11.2</version>
3535
<scope>test</scope>
3636
</dependency>
3737
<dependency>
@@ -44,7 +44,7 @@
4444
<dependency>
4545
<groupId>org.junit.jupiter</groupId>
4646
<artifactId>junit-jupiter-api</artifactId>
47-
<version>5.11.1</version>
47+
<version>5.11.2</version>
4848
<scope>test</scope>
4949
</dependency>
5050
<dependency>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.Recursion;
2+
3+
// program to find power set of a string
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public final class GenerateSubsets {
9+
10+
private GenerateSubsets() {
11+
throw new UnsupportedOperationException("Utility class");
12+
}
13+
14+
public static List<String> subsetRecursion(String str) {
15+
return doRecursion("", str);
16+
}
17+
18+
private static List<String> doRecursion(String p, String up) {
19+
if (up.isEmpty()) {
20+
List<String> list = new ArrayList<>();
21+
list.add(p);
22+
return list;
23+
}
24+
25+
// Taking the character
26+
char ch = up.charAt(0);
27+
// Adding the character in the recursion
28+
List<String> left = doRecursion(p + ch, up.substring(1));
29+
// Not adding the character in the recursion
30+
List<String> right = doRecursion(p, up.substring(1));
31+
32+
left.addAll(right);
33+
34+
return left;
35+
}
36+
}

src/main/java/com/thealgorithms/backtracking/WordSearch.java

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,72 @@
11
package com.thealgorithms.backtracking;
22

3-
/*
4-
Word Search Problem (https://en.wikipedia.org/wiki/Word_search)
5-
6-
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
7-
8-
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are
9-
those horizontally or vertically neighboring. The same letter cell may not be used more than once.
10-
11-
For example,
12-
Given board =
13-
14-
[
15-
['A','B','C','E'],
16-
['S','F','C','S'],
17-
['A','D','E','E']
18-
]
19-
word = "ABCCED", -> returns true,
20-
word = "SEE", -> returns true,
21-
word = "ABCB", -> returns false.
22-
*/
23-
24-
/*
25-
Solution
26-
Depth First Search in matrix (as multiple sources possible) with backtracking
27-
like finding cycle in a directed graph. Maintain a record of path
28-
29-
Tx = O(m * n * 3^L): for each cell, we look at 3 options (not 4 as that one will be visited), we
30-
do it L times Sx = O(L) : stack size is max L
31-
*/
32-
3+
/**
4+
* Word Search Problem
5+
*
6+
* This class solves the word search problem where given an m x n grid of characters (board)
7+
* and a target word, the task is to check if the word exists in the grid.
8+
* The word can be constructed from sequentially adjacent cells (horizontally or vertically),
9+
* and the same cell may not be used more than once in constructing the word.
10+
*
11+
* Example:
12+
* - For board =
13+
* [
14+
* ['A','B','C','E'],
15+
* ['S','F','C','S'],
16+
* ['A','D','E','E']
17+
* ]
18+
* and word = "ABCCED", -> returns true
19+
* and word = "SEE", -> returns true
20+
* and word = "ABCB", -> returns false
21+
*
22+
* Solution:
23+
* - Depth First Search (DFS) with backtracking is used to explore possible paths from any cell
24+
* matching the first letter of the word. DFS ensures that we search all valid paths, while
25+
* backtracking helps in reverting decisions when a path fails to lead to a solution.
26+
*
27+
* Time Complexity: O(m * n * 3^L)
28+
* - m = number of rows in the board
29+
* - n = number of columns in the board
30+
* - L = length of the word
31+
* - For each cell, we look at 3 possible directions (since we exclude the previously visited direction),
32+
* and we do this for L letters.
33+
*
34+
* Space Complexity: O(L)
35+
* - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word).
36+
*/
3337
public class WordSearch {
3438
private final int[] dx = {0, 0, 1, -1};
3539
private final int[] dy = {1, -1, 0, 0};
3640
private boolean[][] visited;
3741
private char[][] board;
3842
private String word;
3943

44+
/**
45+
* Checks if the given (x, y) coordinates are valid positions in the board.
46+
*
47+
* @param x The row index.
48+
* @param y The column index.
49+
* @return True if the coordinates are within the bounds of the board; false otherwise.
50+
*/
4051
private boolean isValid(int x, int y) {
4152
return x >= 0 && x < board.length && y >= 0 && y < board[0].length;
4253
}
4354

55+
/**
56+
* Performs Depth First Search (DFS) from the cell (x, y)
57+
* to search for the next character in the word.
58+
*
59+
* @param x The current row index.
60+
* @param y The current column index.
61+
* @param nextIdx The index of the next character in the word to be matched.
62+
* @return True if a valid path is found to match the remaining characters of the word; false otherwise.
63+
*/
4464
private boolean doDFS(int x, int y, int nextIdx) {
4565
visited[x][y] = true;
4666
if (nextIdx == word.length()) {
4767
return true;
4868
}
69+
4970
for (int i = 0; i < 4; ++i) {
5071
int xi = x + dx[i];
5172
int yi = y + dy[i];
@@ -56,10 +77,19 @@ private boolean doDFS(int x, int y, int nextIdx) {
5677
}
5778
}
5879
}
59-
visited[x][y] = false;
80+
81+
visited[x][y] = false; // Backtrack
6082
return false;
6183
}
6284

85+
/**
86+
* Main function to check if the word exists in the board. It initiates DFS from any
87+
* cell that matches the first character of the word.
88+
*
89+
* @param board The 2D grid of characters (the board).
90+
* @param word The target word to search for in the board.
91+
* @return True if the word exists in the board; false otherwise.
92+
*/
6393
public boolean exist(char[][] board, String word) {
6494
this.board = board;
6595
this.word = word;

src/main/java/com/thealgorithms/others/CountSetBits.java renamed to src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.others;
1+
package com.thealgorithms.bitmanipulation;
22

33
public class CountSetBits {
44

@@ -48,4 +48,32 @@ public long countSetBits(long num) {
4848
}
4949
return cnt;
5050
}
51+
52+
/**
53+
* This approach takes O(1) running time to count the set bits, but requires a pre-processing.
54+
*
55+
* So, we divide our 32-bit input into 8-bit chunks, with four chunks. We have 8 bits in each chunk.
56+
*
57+
* Then the range is from 0-255 (0 to 2^7).
58+
* So, we may need to count set bits from 0 to 255 in individual chunks.
59+
*
60+
* @param num takes a long number
61+
* @return the count of set bits in the binary equivalent
62+
*/
63+
public int lookupApproach(int num) {
64+
int[] table = new int[256];
65+
table[0] = 0;
66+
67+
for (int i = 1; i < 256; i++) {
68+
table[i] = (i & 1) + table[i >> 1]; // i >> 1 equals to i/2
69+
}
70+
71+
int res = 0;
72+
for (int i = 0; i < 4; i++) {
73+
res += table[num & 0xff];
74+
num >>= 8;
75+
}
76+
77+
return res;
78+
}
5179
}

src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
package com.thealgorithms.bitmanipulation;
2+
23
import java.util.Optional;
34

45
/**
56
* Find Highest Set Bit
6-
* This class provides a function calculating the position (or index)
7-
* of the most significant bit being set to 1 in a given integer.
8-
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
7+
*
8+
* This class provides a utility method to calculate the position of the highest
9+
* (most significant) bit that is set to 1 in a given non-negative integer.
10+
* It is often used in bit manipulation tasks to find the left-most set bit in binary
11+
* representation of a number.
12+
*
13+
* Example:
14+
* - For input 18 (binary 10010), the highest set bit is at position 4 (zero-based index).
15+
*
16+
* @author Bama Charan Chhandogi
17+
* @version 1.0
18+
* @since 2021-06-23
919
*/
10-
1120
public final class HighestSetBit {
21+
1222
private HighestSetBit() {
1323
}
1424

25+
/**
26+
* Finds the highest (most significant) set bit in the given integer.
27+
* The method returns the position (index) of the highest set bit as an {@link Optional}.
28+
*
29+
* - If the number is 0, no bits are set, and the method returns {@link Optional#empty()}.
30+
* - If the number is negative, the method throws {@link IllegalArgumentException}.
31+
*
32+
* @param num The input integer for which the highest set bit is to be found. It must be non-negative.
33+
* @return An {@link Optional} containing the index of the highest set bit (zero-based).
34+
* Returns {@link Optional#empty()} if the number is 0.
35+
* @throws IllegalArgumentException if the input number is negative.
36+
*/
1537
public static Optional<Integer> findHighestSetBit(int num) {
1638
if (num < 0) {
1739
throw new IllegalArgumentException("Input cannot be negative");
@@ -27,6 +49,6 @@ public static Optional<Integer> findHighestSetBit(int num) {
2749
position++;
2850
}
2951

30-
return Optional.of(position - 1);
52+
return Optional.of(position - 1); // Subtract 1 to convert to zero-based index
3153
}
3254
}

src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find The Index Of Right Most SetBit
5-
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
4+
* Utility class for bit manipulation operations.
5+
* This class provides methods to work with bitwise operations.
6+
* Specifically, it includes a method to find the index of the rightmost set bit
7+
* in an integer.
8+
* This class is not meant to be instantiated.
9+
*
10+
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
611
*/
7-
812
public final class IndexOfRightMostSetBit {
13+
914
private IndexOfRightMostSetBit() {
1015
}
16+
17+
/**
18+
* Finds the index of the rightmost set bit in the given integer.
19+
* The index is zero-based, meaning the rightmost bit has an index of 0.
20+
*
21+
* @param n the integer to check for the rightmost set bit
22+
* @return the index of the rightmost set bit; -1 if there are no set bits
23+
* (i.e., the input integer is 0)
24+
*/
1125
public static int indexOfRightMostSetBit(int n) {
1226
if (n == 0) {
1327
return -1; // No set bits
@@ -16,7 +30,7 @@ public static int indexOfRightMostSetBit(int n) {
1630
// Handle negative numbers by finding the two's complement
1731
if (n < 0) {
1832
n = -n;
19-
n = n & (~n + 1); // Get the rightmost set bit in positive form
33+
n = n & (~n + 1); // Isolate the rightmost set bit
2034
}
2135

2236
int index = 0;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Lowest Set Bit
5+
* @author Prayas Kumar (https://github.com/prayas7102)
6+
*/
7+
8+
public final class LowestSetBit {
9+
// Private constructor to hide the default public one
10+
private LowestSetBit() {
11+
}
12+
/**
13+
* Isolates the lowest set bit of the given number. For example, if n = 18
14+
* (binary: 10010), the result will be 2 (binary: 00010).
15+
*
16+
* @param n the number whose lowest set bit will be isolated
17+
* @return the isolated lowest set bit of n
18+
*/
19+
public static int isolateLowestSetBit(int n) {
20+
// Isolate the lowest set bit using n & -n
21+
return n & -n;
22+
}
23+
/**
24+
* Clears the lowest set bit of the given number.
25+
* For example, if n = 18 (binary: 10010), the result will be 16 (binary: 10000).
26+
*
27+
* @param n the number whose lowest set bit will be cleared
28+
* @return the number after clearing its lowest set bit
29+
*/
30+
public static int clearLowestSetBit(int n) {
31+
// Clear the lowest set bit using n & (n - 1)
32+
return n & (n - 1);
33+
}
34+
}

0 commit comments

Comments
 (0)