Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f43c5de
added shuffle algorithms, it's implementation and problem related to it
Darshan-Baligeri Oct 27, 2024
ddcac94
clang-formatted the code
Darshan-Baligeri Oct 27, 2024
dc82551
checked with coding style issues
Darshan-Baligeri Oct 27, 2024
0357b9a
refined code style issues and checked with clang format
Darshan-Baligeri Oct 27, 2024
ead8be5
resolved code style violations
Darshan-Baligeri Oct 27, 2024
0160f11
done required changes
Darshan-Baligeri Oct 27, 2024
545aeae
resolved issues
Darshan-Baligeri Oct 27, 2024
9f38af6
resolved errors
Darshan-Baligeri Oct 27, 2024
1493800
resolves problems
Darshan-Baligeri Oct 27, 2024
e518ce8
resolved issues
Darshan-Baligeri Oct 27, 2024
07276fc
resolved errors
Darshan-Baligeri Oct 27, 2024
8a18e1a
resolved errors
Darshan-Baligeri Oct 27, 2024
6f2c907
resolved errors
Darshan-Baligeri Oct 27, 2024
d404af1
resolved all issues
Darshan-Baligeri Oct 27, 2024
f4169c4
resolved spot bugs
Darshan-Baligeri Oct 27, 2024
213ec92
resolved more spotbugs
Darshan-Baligeri Oct 27, 2024
0cce10d
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
c1775b4
resolved spotbug
Darshan-Baligeri Oct 27, 2024
3e2564a
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
781ec96
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
7eeb225
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
6f3fb36
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
26dd671
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
21d5a3c
resolved build errors
Darshan-Baligeri Oct 27, 2024
4bdab42
resolved bugs
Darshan-Baligeri Oct 27, 2024
a85fd14
resolved error in binary tree
Darshan-Baligeri Oct 27, 2024
293341e
resolved errorin binary tree remove method
Darshan-Baligeri Oct 27, 2024
30f0573
rewrote test cases for binary tree and resolved bugs
Darshan-Baligeri Oct 27, 2024
02945d6
rewrote test cases for binary tree and resolved bugs
Darshan-Baligeri Oct 27, 2024
31601ba
resolved bugs
Darshan-Baligeri Oct 27, 2024
48c1fbc
resolved bugs
Darshan-Baligeri Oct 27, 2024
e7839d4
formatted according to clang-format
Darshan-Baligeri Oct 27, 2024
9a95623
reformatted code
Darshan-Baligeri Oct 27, 2024
cb03bc9
reformatted code
Darshan-Baligeri Oct 27, 2024
c2d5827
applied clang format
Darshan-Baligeri Oct 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/audiofilters/IIRFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* N-Order IIR Filter Assumes inputs are normalized to [-1, 1]
*
* <p>
* Based on the difference equation from
* <a href="https://en.wikipedia.org/wiki/Infinite_impulse_response">Wikipedia link</a>
*/
Expand Down Expand Up @@ -43,7 +43,7 @@ public IIRFilter(int order) throws IllegalArgumentException {
* @param aCoeffs Denominator coefficients
* @param bCoeffs Numerator coefficients
* @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is
* not of size {@code order}, or if {@code aCoeffs[0]} is 0.0
* not of size {@code order}, or if {@code aCoeffs[0]} is 0.0
*/
public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {
if (aCoeffs.length != order) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public static List<List<Integer>> combination(int n, int k) {
* A helper method that uses backtracking to find combinations.
*
* @param combinations The list to store all valid combinations found.
* @param current The current combination being built.
* @param start The starting index for the current recursion.
* @param n The total number of elements (0 to n-1).
* @param k The desired length of each combination.
* @param current The current combination being built.
* @param start The starting index for the current recursion.
* @param n The total number of elements (0 to n-1).
* @param k The desired length of each combination.
*/
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
// Base case: combination found
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/thealgorithms/backtracking/Combination.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

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

/**
* Backtrack all possible combinations of a given array
* @param arr the array.
* @param n length of the combination
* @param index the starting index.
*
* @param arr the array.
* @param n length of the combination
* @param index the starting index.
* @param currSet set that tracks current combination
* @param result the list contains all combination.
* @param <T> the type of elements in the array.
* @param result the list contains all combination.
* @param <T> the type of elements in the array.
*/
private static <T> void backtracking(T[] arr, int n, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
if (index + n - currSet.size() > arr.length) {
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
* A class to solve a crossword puzzle using backtracking.
* Example:
* Input:
* puzzle = {
* {' ', ' ', ' '},
* {' ', ' ', ' '},
* {' ', ' ', ' '}
* }
* words = List.of("cat", "dog")
*
* puzzle = {
* {' ', ' ', ' '},
* {' ', ' ', ' '},
* {' ', ' ', ' '}
* }
* words = List.of("cat", "dog")
* <p>
* Output:
* {
* {'c', 'a', 't'},
* {' ', ' ', ' '},
* {'d', 'o', 'g'}
* }
* {
* {'c', 'a', 't'},
* {' ', ' ', ' '},
* {'d', 'o', 'g'}
* }
*/
public final class CrosswordSolver {
private CrosswordSolver() {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/thealgorithms/backtracking/FloodFill.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* Java program for Flood fill algorithm.
*
* @author Akshay Dubey (<a href="https://github.com/itsAkshayDubey">Git-Akshay Dubey</a>)
*/
public final class FloodFill {
Expand All @@ -12,8 +13,8 @@ private FloodFill() {
* Get the color at the given coordinates of a 2D image
*
* @param image The image to be filled
* @param x The x co-ordinate of which color is to be obtained
* @param y The y co-ordinate of which color is to be obtained
* @param x The x co-ordinate of which color is to be obtained
* @param y The y co-ordinate of which color is to be obtained
*/

public static int getPixel(final int[][] image, final int x, final int y) {
Expand All @@ -24,8 +25,8 @@ public static int getPixel(final int[][] image, final int x, final int y) {
* Put the color at the given coordinates of a 2D image
*
* @param image The image to be filled
* @param x The x co-ordinate at which color is to be filled
* @param y The y co-ordinate at which color is to be filled
* @param x The x co-ordinate at which color is to be filled
* @param y The y co-ordinate at which color is to be filled
*/
public static void putPixel(final int[][] image, final int x, final int y, final int newColor) {
image[x][y] = newColor;
Expand All @@ -34,9 +35,9 @@ public static void putPixel(final int[][] image, final int x, final int y, final
/**
* Fill the 2D image with new color
*
* @param image The image to be filled
* @param x The x co-ordinate at which color is to be filled
* @param y The y co-ordinate at which color is to be filled
* @param image The image to be filled
* @param x The x co-ordinate at which color is to be filled
* @param y The y co-ordinate at which color is to be filled
* @param newColor The new color which to be filled in the image
* @param oldColor The old color which is to be replaced in the image
*/
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/thealgorithms/backtracking/KnightsTour.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

/**
* The KnightsTour class solves the Knight's Tour problem using backtracking.
*
* <p>
* Problem Statement:
* Given an N*N board with a knight placed on the first block, the knight must
* move according to chess rules and visit each square on the board exactly once.
* The class outputs the sequence of moves for the knight.
*
* <p>
* Example:
* Input: N = 8 (8x8 chess board)
* Output: The sequence of numbers representing the order in which the knight visits each square.
Expand Down Expand Up @@ -61,7 +61,7 @@ public static void resetBoard() {
/**
* Recursive method to solve the Knight's Tour problem.
*
* @param row The current row of the knight
* @param row The current row of the knight
* @param column The current column of the knight
* @param count The current move number
* @return True if a solution is found, False otherwise
Expand Down Expand Up @@ -96,10 +96,10 @@ static boolean solve(int row, int column, int count) {
/**
* Returns a list of valid neighboring cells where the knight can move.
*
* @param row The current row of the knight
* @param row The current row of the knight
* @param column The current column of the knight
* @return A list of arrays representing valid moves, where each array contains:
* {nextRow, nextCol, numberOfPossibleNextMoves}
* {nextRow, nextCol, numberOfPossibleNextMoves}
*/
static List<int[]> neighbors(int row, int column) {
List<int[]> neighbour = new ArrayList<>();
Expand Down Expand Up @@ -137,9 +137,9 @@ static int countNeighbors(int row, int column) {
/**
* Detects if moving to a given position will create an orphan (a position with no further valid moves).
*
* @param count The current move number
* @param row The row of the current position
* @param column The column of the current position
* @param count The current move number
* @param row The row of the current position
* @param column The column of the current position
* @return True if an orphan is detected, False otherwise
*/
static boolean orphanDetected(int count, int row, int column) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This class contains methods to solve a maze using recursive backtracking.
* The maze is represented as a 2D array where walls, paths, and visited/dead
* ends are marked with different integers.
*
* <p>
* The goal is to find a path from a starting position to the target position
* (map[6][5]) while navigating through the maze.
*/
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/thealgorithms/backtracking/NQueens.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
* which N queens can be placed on the board such no two queens attack each
* other. Ex. N = 6 Solution= There are 4 possible ways Arrangement: 1 ".Q....",
* "...Q..", ".....Q", "Q.....", "..Q...", "....Q."
*
* <p>
* Arrangement: 2 "..Q...", ".....Q", ".Q....", "....Q.", "Q.....", "...Q.."
*
* <p>
* Arrangement: 3 "...Q..", "Q.....", "....Q.", ".Q....", ".....Q", "..Q..."
*
* <p>
* Arrangement: 4 "....Q.", "..Q...", "Q.....", ".....Q", "...Q..", ".Q...."
*
* <p>
* Solution: Brute Force approach:
*
* <p>
* Generate all possible arrangement to place N queens on N*N board. Check each
* board if queens are placed safely. If it is safe, include arrangement in
* solution set. Otherwise, ignore it
*
* <p>
* Optimized solution: This can be solved using backtracking in below steps
*
* <p>
* Start with first column and place queen on first row Try placing queen in a
* row on second column If placing second queen in second column attacks any of
* the previous queens, change the row in second column otherwise move to next
Expand Down Expand Up @@ -59,9 +59,9 @@ public static void placeQueens(final int queens) {
/**
* This is backtracking function which tries to place queen recursively
*
* @param boardSize: size of chess board
* @param solutions: this holds all possible arrangements
* @param columns: columns[i] = rowId where queen is placed in ith column.
* @param boardSize: size of chess board
* @param solutions: this holds all possible arrangements
* @param columns: columns[i] = rowId where queen is placed in ith column.
* @param columnIndex: This is the column in which queen is being placed
*/
private static void getSolution(int boardSize, List<List<String>> solutions, int[] columns, int columnIndex) {
Expand Down Expand Up @@ -94,8 +94,8 @@ private static void getSolution(int boardSize, List<List<String>> solutions, int
* This function checks if queen can be placed at row = rowIndex in column =
* columnIndex safely
*
* @param columns: columns[i] = rowId where queen is placed in ith column.
* @param rowIndex: row in which queen has to be placed
* @param columns: columns[i] = rowId where queen is placed in ith column.
* @param rowIndex: row in which queen has to be placed
* @param columnIndex: column in which queen is being placed
* @return true: if queen can be placed safely false: otherwise
*/
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/thealgorithms/backtracking/Permutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

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

/**
* Backtrack all possible orders of a given array
* @param arr the array.
* @param index the starting index.
*
* @param arr the array.
* @param index the starting index.
* @param result the list contains all permutations.
* @param <T> the type of elements in the array.
* @param <T> the type of elements in the array.
*/
private static <T> void backtracking(T[] arr, int index, List<T[]> result) {
if (index == arr.length) {
Expand All @@ -44,8 +47,9 @@ private static <T> void backtracking(T[] arr, int index, List<T[]> result) {

/**
* Swap two element for a given array
* @param a first index
* @param b second index
*
* @param a first index
* @param b second index
* @param arr the array.
* @param <T> the type of elements in the array.
*/
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/thealgorithms/backtracking/PowerSum.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* of unique, natural numbers.
* For example, if N=100 and X=3, we have to find all combinations of unique cubes adding up to 100.
* The only solution is 1^3 + 2^3 + 3^3 + 4^3. Therefore, the output will be 1.
*
* <p>
* N is represented by the parameter 'targetSum' in the code.
* X is represented by the parameter 'power' in the code.
*/
Expand All @@ -16,7 +16,7 @@ public class PowerSum {
* Calculates the number of ways to express the target sum as a sum of Xth powers of unique natural numbers.
*
* @param targetSum The target sum to achieve (N in the problem statement)
* @param power The power to raise natural numbers to (X in the problem statement)
* @param power The power to raise natural numbers to (X in the problem statement)
* @return The number of ways to express the target sum
*/
public int powSum(int targetSum, int power) {
Expand All @@ -30,10 +30,10 @@ public int powSum(int targetSum, int power) {
/**
* Recursively calculates the number of ways to express the remaining sum as a sum of Xth powers.
*
* @param remainingSum The remaining sum to achieve
* @param power The power to raise natural numbers to (X in the problem statement)
* @param remainingSum The remaining sum to achieve
* @param power The power to raise natural numbers to (X in the problem statement)
* @param currentNumber The current natural number being considered
* @param currentSum The current sum of powered numbers
* @param currentSum The current sum of powered numbers
* @return The number of valid combinations
*/
private int sumRecursive(int remainingSum, int power, int currentNumber, int currentSum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private SubsequenceFinder() {
* Find all subsequences of given list using backtracking
*
* @param sequence a list of items on the basis of which we need to generate all subsequences
* @param <T> the type of elements in the array
* @param <T> the type of elements in the array
* @return a list of all subsequences
*/
public static <T> List<List<T>> generateAll(List<T> sequence) {
Expand All @@ -33,11 +33,11 @@ public static <T> List<List<T>> generateAll(List<T> sequence) {
* We know that each state has exactly two branching
* It terminates when it reaches the end of the given sequence
*
* @param sequence all elements
* @param sequence all elements
* @param currentSubsequence current subsequence
* @param index current index
* @param allSubSequences contains all sequences
* @param <T> the type of elements which we generate
* @param index current index
* @param allSubSequences contains all sequences
* @param <T> the type of elements which we generate
*/
private static <T> void backtrack(List<T> sequence, List<T> currentSubsequence, final int index, List<List<T>> allSubSequences) {
assert index <= sequence.size();
Expand Down
Loading