Skip to content

Commit 9a95623

Browse files
reformatted code
1 parent e7839d4 commit 9a95623

File tree

734 files changed

+7201
-6615
lines changed

Some content is hidden

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

734 files changed

+7201
-6615
lines changed

src/main/java/com/thealgorithms/audiofilters/IIRFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* N-Order IIR Filter Assumes inputs are normalized to [-1, 1]
5-
*
5+
* <p>
66
* Based on the difference equation from
77
* <a href="https://en.wikipedia.org/wiki/Infinite_impulse_response">Wikipedia link</a>
88
*/
@@ -43,7 +43,7 @@ public IIRFilter(int order) throws IllegalArgumentException {
4343
* @param aCoeffs Denominator coefficients
4444
* @param bCoeffs Numerator coefficients
4545
* @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is
46-
* not of size {@code order}, or if {@code aCoeffs[0]} is 0.0
46+
* not of size {@code order}, or if {@code aCoeffs[0]} is 0.0
4747
*/
4848
public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {
4949
if (aCoeffs.length != order) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public static List<List<Integer>> combination(int n, int k) {
3333
* A helper method that uses backtracking to find combinations.
3434
*
3535
* @param combinations The list to store all valid combinations found.
36-
* @param current The current combination being built.
37-
* @param start The starting index for the current recursion.
38-
* @param n The total number of elements (0 to n-1).
39-
* @param k The desired length of each combination.
36+
* @param current The current combination being built.
37+
* @param start The starting index for the current recursion.
38+
* @param n The total number of elements (0 to n-1).
39+
* @param k The desired length of each combination.
4040
*/
4141
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
4242
// Base case: combination found

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* Finds all permutations of given array
11+
*
1112
* @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>)
1213
*/
1314
public final class Combination {
@@ -16,8 +17,9 @@ private Combination() {
1617

1718
/**
1819
* Find all combinations of given array using backtracking
20+
*
1921
* @param arr the array.
20-
* @param n length of combination
22+
* @param n length of combination
2123
* @param <T> the type of elements in the array.
2224
* @return a list of all combinations of length n. If n == 0, return null.
2325
*/
@@ -39,12 +41,13 @@ public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
3941

4042
/**
4143
* Backtrack all possible combinations of a given array
42-
* @param arr the array.
43-
* @param n length of the combination
44-
* @param index the starting index.
44+
*
45+
* @param arr the array.
46+
* @param n length of the combination
47+
* @param index the starting index.
4548
* @param currSet set that tracks current combination
46-
* @param result the list contains all combination.
47-
* @param <T> the type of elements in the array.
49+
* @param result the list contains all combination.
50+
* @param <T> the type of elements in the array.
4851
*/
4952
private static <T> void backtracking(T[] arr, int n, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
5053
if (index + n - currSet.size() > arr.length) {

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
* A class to solve a crossword puzzle using backtracking.
99
* Example:
1010
* Input:
11-
* puzzle = {
12-
* {' ', ' ', ' '},
13-
* {' ', ' ', ' '},
14-
* {' ', ' ', ' '}
15-
* }
16-
* words = List.of("cat", "dog")
17-
*
11+
* puzzle = {
12+
* {' ', ' ', ' '},
13+
* {' ', ' ', ' '},
14+
* {' ', ' ', ' '}
15+
* }
16+
* words = List.of("cat", "dog")
17+
* <p>
1818
* Output:
19-
* {
20-
* {'c', 'a', 't'},
21-
* {' ', ' ', ' '},
22-
* {'d', 'o', 'g'}
23-
* }
19+
* {
20+
* {'c', 'a', 't'},
21+
* {' ', ' ', ' '},
22+
* {'d', 'o', 'g'}
23+
* }
2424
*/
2525
public final class CrosswordSolver {
2626
private CrosswordSolver() {
@@ -104,7 +104,7 @@ public static boolean solveCrossword(char[][] puzzle, Collection<String> words)
104104
for (int col = 0; col < puzzle[0].length; col++) {
105105
if (puzzle[row][col] == ' ') {
106106
for (String word : new ArrayList<>(remainingWords)) {
107-
for (boolean vertical : new boolean[] {true, false}) {
107+
for (boolean vertical : new boolean[]{true, false}) {
108108
if (isValid(puzzle, word, row, col, vertical)) {
109109
placeWord(puzzle, word, row, col, vertical);
110110
remainingWords.remove(word);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* Java program for Flood fill algorithm.
5+
*
56
* @author Akshay Dubey (<a href="https://github.com/itsAkshayDubey">Git-Akshay Dubey</a>)
67
*/
78
public final class FloodFill {
@@ -12,8 +13,8 @@ private FloodFill() {
1213
* Get the color at the given coordinates of a 2D image
1314
*
1415
* @param image The image to be filled
15-
* @param x The x co-ordinate of which color is to be obtained
16-
* @param y The y co-ordinate of which color is to be obtained
16+
* @param x The x co-ordinate of which color is to be obtained
17+
* @param y The y co-ordinate of which color is to be obtained
1718
*/
1819

1920
public static int getPixel(final int[][] image, final int x, final int y) {
@@ -24,8 +25,8 @@ public static int getPixel(final int[][] image, final int x, final int y) {
2425
* Put the color at the given coordinates of a 2D image
2526
*
2627
* @param image The image to be filled
27-
* @param x The x co-ordinate at which color is to be filled
28-
* @param y The y co-ordinate at which color is to be filled
28+
* @param x The x co-ordinate at which color is to be filled
29+
* @param y The y co-ordinate at which color is to be filled
2930
*/
3031
public static void putPixel(final int[][] image, final int x, final int y, final int newColor) {
3132
image[x][y] = newColor;
@@ -34,9 +35,9 @@ public static void putPixel(final int[][] image, final int x, final int y, final
3435
/**
3536
* Fill the 2D image with new color
3637
*
37-
* @param image The image to be filled
38-
* @param x The x co-ordinate at which color is to be filled
39-
* @param y The y co-ordinate at which color is to be filled
38+
* @param image The image to be filled
39+
* @param x The x co-ordinate at which color is to be filled
40+
* @param y The y co-ordinate at which color is to be filled
4041
* @param newColor The new color which to be filled in the image
4142
* @param oldColor The old color which is to be replaced in the image
4243
*/

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
/**
88
* The KnightsTour class solves the Knight's Tour problem using backtracking.
9-
*
9+
* <p>
1010
* Problem Statement:
1111
* Given an N*N board with a knight placed on the first block, the knight must
1212
* move according to chess rules and visit each square on the board exactly once.
1313
* The class outputs the sequence of moves for the knight.
14-
*
14+
* <p>
1515
* Example:
1616
* Input: N = 8 (8x8 chess board)
1717
* Output: The sequence of numbers representing the order in which the knight visits each square.
@@ -25,14 +25,14 @@ private KnightsTour() {
2525

2626
// Possible moves for a knight in chess
2727
private static final int[][] MOVES = {
28-
{1, -2},
29-
{2, -1},
30-
{2, 1},
31-
{1, 2},
32-
{-1, 2},
33-
{-2, 1},
34-
{-2, -1},
35-
{-1, -2},
28+
{1, -2},
29+
{2, -1},
30+
{2, 1},
31+
{1, 2},
32+
{-1, 2},
33+
{-2, 1},
34+
{-2, -1},
35+
{-1, -2},
3636
};
3737

3838
// Chess grid representing the board
@@ -61,7 +61,7 @@ public static void resetBoard() {
6161
/**
6262
* Recursive method to solve the Knight's Tour problem.
6363
*
64-
* @param row The current row of the knight
64+
* @param row The current row of the knight
6565
* @param column The current column of the knight
6666
* @param count The current move number
6767
* @return True if a solution is found, False otherwise
@@ -96,10 +96,10 @@ static boolean solve(int row, int column, int count) {
9696
/**
9797
* Returns a list of valid neighboring cells where the knight can move.
9898
*
99-
* @param row The current row of the knight
99+
* @param row The current row of the knight
100100
* @param column The current column of the knight
101101
* @return A list of arrays representing valid moves, where each array contains:
102-
* {nextRow, nextCol, numberOfPossibleNextMoves}
102+
* {nextRow, nextCol, numberOfPossibleNextMoves}
103103
*/
104104
static List<int[]> neighbors(int row, int column) {
105105
List<int[]> neighbour = new ArrayList<>();
@@ -109,7 +109,7 @@ static List<int[]> neighbors(int row, int column) {
109109
int y = m[1];
110110
if (row + y >= 0 && row + y < BASE && column + x >= 0 && column + x < BASE && grid[row + y][column + x] == 0) {
111111
int num = countNeighbors(row + y, column + x);
112-
neighbour.add(new int[] {row + y, column + x, num});
112+
neighbour.add(new int[]{row + y, column + x, num});
113113
}
114114
}
115115
return neighbour;
@@ -137,9 +137,9 @@ static int countNeighbors(int row, int column) {
137137
/**
138138
* Detects if moving to a given position will create an orphan (a position with no further valid moves).
139139
*
140-
* @param count The current move number
141-
* @param row The row of the current position
142-
* @param column The column of the current position
140+
* @param count The current move number
141+
* @param row The row of the current position
142+
* @param column The column of the current position
143143
* @return True if an orphan is detected, False otherwise
144144
*/
145145
static boolean orphanDetected(int count, int row, int column) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This class contains methods to solve a maze using recursive backtracking.
55
* The maze is represented as a 2D array where walls, paths, and visited/dead
66
* ends are marked with different integers.
7-
*
7+
* <p>
88
* The goal is to find a path from a starting position to the target position
99
* (map[6][5]) while navigating through the maze.
1010
*/

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
* which N queens can be placed on the board such no two queens attack each
99
* other. Ex. N = 6 Solution= There are 4 possible ways Arrangement: 1 ".Q....",
1010
* "...Q..", ".....Q", "Q.....", "..Q...", "....Q."
11-
*
11+
* <p>
1212
* Arrangement: 2 "..Q...", ".....Q", ".Q....", "....Q.", "Q.....", "...Q.."
13-
*
13+
* <p>
1414
* Arrangement: 3 "...Q..", "Q.....", "....Q.", ".Q....", ".....Q", "..Q..."
15-
*
15+
* <p>
1616
* Arrangement: 4 "....Q.", "..Q...", "Q.....", ".....Q", "...Q..", ".Q...."
17-
*
17+
* <p>
1818
* Solution: Brute Force approach:
19-
*
19+
* <p>
2020
* Generate all possible arrangement to place N queens on N*N board. Check each
2121
* board if queens are placed safely. If it is safe, include arrangement in
2222
* solution set. Otherwise, ignore it
23-
*
23+
* <p>
2424
* Optimized solution: This can be solved using backtracking in below steps
25-
*
25+
* <p>
2626
* Start with first column and place queen on first row Try placing queen in a
2727
* row on second column If placing second queen in second column attacks any of
2828
* the previous queens, change the row in second column otherwise move to next
@@ -59,9 +59,9 @@ public static void placeQueens(final int queens) {
5959
/**
6060
* This is backtracking function which tries to place queen recursively
6161
*
62-
* @param boardSize: size of chess board
63-
* @param solutions: this holds all possible arrangements
64-
* @param columns: columns[i] = rowId where queen is placed in ith column.
62+
* @param boardSize: size of chess board
63+
* @param solutions: this holds all possible arrangements
64+
* @param columns: columns[i] = rowId where queen is placed in ith column.
6565
* @param columnIndex: This is the column in which queen is being placed
6666
*/
6767
private static void getSolution(int boardSize, List<List<String>> solutions, int[] columns, int columnIndex) {
@@ -94,8 +94,8 @@ private static void getSolution(int boardSize, List<List<String>> solutions, int
9494
* This function checks if queen can be placed at row = rowIndex in column =
9595
* columnIndex safely
9696
*
97-
* @param columns: columns[i] = rowId where queen is placed in ith column.
98-
* @param rowIndex: row in which queen has to be placed
97+
* @param columns: columns[i] = rowId where queen is placed in ith column.
98+
* @param rowIndex: row in which queen has to be placed
9999
* @param columnIndex: column in which queen is being placed
100100
* @return true: if queen can be placed safely false: otherwise
101101
*/

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
/**
77
* Finds all permutations of given array
8+
*
89
* @author Alan Piao (<a href="https://github.com/cpiao3">Git-Alan Piao</a>)
910
*/
1011
public final class Permutation {
@@ -13,6 +14,7 @@ private Permutation() {
1314

1415
/**
1516
* Find all permutations of given array using backtracking
17+
*
1618
* @param arr the array.
1719
* @param <T> the type of elements in the array.
1820
* @return a list of all permutations.
@@ -26,10 +28,11 @@ public static <T> List<T[]> permutation(T[] arr) {
2628

2729
/**
2830
* Backtrack all possible orders of a given array
29-
* @param arr the array.
30-
* @param index the starting index.
31+
*
32+
* @param arr the array.
33+
* @param index the starting index.
3134
* @param result the list contains all permutations.
32-
* @param <T> the type of elements in the array.
35+
* @param <T> the type of elements in the array.
3336
*/
3437
private static <T> void backtracking(T[] arr, int index, List<T[]> result) {
3538
if (index == arr.length) {
@@ -44,8 +47,9 @@ private static <T> void backtracking(T[] arr, int index, List<T[]> result) {
4447

4548
/**
4649
* Swap two element for a given array
47-
* @param a first index
48-
* @param b second index
50+
*
51+
* @param a first index
52+
* @param b second index
4953
* @param arr the array.
5054
* @param <T> the type of elements in the array.
5155
*/

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* of unique, natural numbers.
77
* For example, if N=100 and X=3, we have to find all combinations of unique cubes adding up to 100.
88
* The only solution is 1^3 + 2^3 + 3^3 + 4^3. Therefore, the output will be 1.
9-
*
9+
* <p>
1010
* N is represented by the parameter 'targetSum' in the code.
1111
* X is represented by the parameter 'power' in the code.
1212
*/
@@ -16,7 +16,7 @@ public class PowerSum {
1616
* Calculates the number of ways to express the target sum as a sum of Xth powers of unique natural numbers.
1717
*
1818
* @param targetSum The target sum to achieve (N in the problem statement)
19-
* @param power The power to raise natural numbers to (X in the problem statement)
19+
* @param power The power to raise natural numbers to (X in the problem statement)
2020
* @return The number of ways to express the target sum
2121
*/
2222
public int powSum(int targetSum, int power) {
@@ -30,10 +30,10 @@ public int powSum(int targetSum, int power) {
3030
/**
3131
* Recursively calculates the number of ways to express the remaining sum as a sum of Xth powers.
3232
*
33-
* @param remainingSum The remaining sum to achieve
34-
* @param power The power to raise natural numbers to (X in the problem statement)
33+
* @param remainingSum The remaining sum to achieve
34+
* @param power The power to raise natural numbers to (X in the problem statement)
3535
* @param currentNumber The current natural number being considered
36-
* @param currentSum The current sum of powered numbers
36+
* @param currentSum The current sum of powered numbers
3737
* @return The number of valid combinations
3838
*/
3939
private int sumRecursive(int remainingSum, int power, int currentNumber, int currentSum) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private SubsequenceFinder() {
1414
* Find all subsequences of given list using backtracking
1515
*
1616
* @param sequence a list of items on the basis of which we need to generate all subsequences
17-
* @param <T> the type of elements in the array
17+
* @param <T> the type of elements in the array
1818
* @return a list of all subsequences
1919
*/
2020
public static <T> List<List<T>> generateAll(List<T> sequence) {
@@ -33,11 +33,11 @@ public static <T> List<List<T>> generateAll(List<T> sequence) {
3333
* We know that each state has exactly two branching
3434
* It terminates when it reaches the end of the given sequence
3535
*
36-
* @param sequence all elements
36+
* @param sequence all elements
3737
* @param currentSubsequence current subsequence
38-
* @param index current index
39-
* @param allSubSequences contains all sequences
40-
* @param <T> the type of elements which we generate
38+
* @param index current index
39+
* @param allSubSequences contains all sequences
40+
* @param <T> the type of elements which we generate
4141
*/
4242
private static <T> void backtrack(List<T> sequence, List<T> currentSubsequence, final int index, List<List<T>> allSubSequences) {
4343
assert index <= sequence.size();

0 commit comments

Comments
 (0)