Commit Graph

1056 Commits

Author SHA1 Message Date
kvadrik
d8672882bf Added surface area of a cuboid (#7293)
* Added surface area of a cuboid

Added surface area of a cuboid according to the formula:
S = 2 * (ab + ac + bc)

* Removed extra white space

* Added test for cuboid surface area

* fixed syntax error

* Removed extra space

* Added tests for cuboid surface area that should fail

I have added tests for cuboid surface area where one of the parameters is invalid. These should fail.
2026-02-28 19:13:19 +00:00
kvadrik
0d2a98e9f8 Added volume of a torus (#7294)
* Added volume of a torus

Added function calculation the volume of a torus according to the formula:
V = 2 * pi^2 * R * r^2
where R is the major radius and r is the minor radius of the torus.

* Added test for torus volume
2026-02-28 20:08:50 +01:00
kvadrik
705eb52833 Added volume of a pyramid frustum (#7291)
* Added volume of a pyramid frustum

Added a function calculating volume of a pyramid frustum, V=(S1+S2+sqrt(S1*S2))*h/3

* compiler error fixed

* Added pyramid frustum test case

* extra space removed
2026-02-27 22:35:42 +01:00
Chahat Sandhu
7e5d9d469d feat: add HuffmanCoding with fail-fast validation and immutable design (#7289) 2026-02-26 12:27:11 +01:00
Vasundhara117
10114b79ae Add ReverseQueueRecursion.java: Reverse a Queue using recursion (#7281)
* Create ReverseQueueRecursion

Add ReverseQueueRecursion.java:
- Reverses a Queue using recursion (generic <T>)
- Includes unit tests in ReverseQueueRecursionTest.java
- Follows repo style (final class, private constructor, Javadoc)

* Create ReverseQueueRecursionTest.java

Add ReverseQueueRecursionTest as required by CONTRIBUTING.md

* Rename ReverseQueueRecursion to ReverseQueueRecursion.java

* Update ReverseQueueRecursion.java

* Update ReverseQueueRecursion.java

* Update ReverseQueueRecursion.java

* Update ReverseQueueRecursion.java

* Update ReverseQueueRecursionTest.java

* Update ReverseQueueRecursion.java

* Update ReverseQueueRecursionTest.java

* Update ReverseQueueRecursionTest.java

* Update ReverseQueueRecursion.java

* Update ReverseQueueRecursionTest.java

* Update ReverseQueueRecursionTest.java

* Update ReverseQueueRecursionTest.java

* Update ReverseQueueRecursionTest.java

* Update ReverseQueueRecursion.java
2026-02-23 20:06:51 +01:00
Çağlar Eker
12935c291d Add Longest Repeated Substring algorithm (#7286)
* Add Longest Repeated Substring algorithm

Implement LongestRepeatedSubstring in the strings package using the
existing SuffixArray and Kasai's algorithm for LCP array construction.
Includes parameterized unit tests covering typical and edge cases.

* style: reformat test file per clang-format and add coverage test

---------

Co-authored-by: Çağlar Eker <eker.caglar@hepsiburada.com>
2026-02-22 19:15:22 +01:00
Piotr Idzik
109df1f398 style: include SUA_SUSPICIOUS_UNINITIALIZED_ARRAY (#7285) 2026-02-21 16:27:13 +01:00
Piotr Idzik
7c38e5acd2 style: include DM_NEXTINT_VIA_NEXTDOUBLE (#7282) 2026-02-20 20:48:12 +01:00
Ruturaj Jadhav
29ce2ef666 Add RangeSumQuery algorithm in prefix folder (#7280)
* add Subarray Sum Equals K using prefix sum

* Add RangeSumQuery algorithm in prefix folder

* chore: apply clang-format to RangeSumQueryTest file

* Add import statement for JUnit test class

---------

Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2026-02-19 22:11:56 +01:00
Neha-2005-VCE
2ae1bdfd9a feat: add contains() method to DynamicArray (#7270)
* feat: add contains() method to DynamicArray

* style: fix clang formatting

* style: fix clang format issues

* style: apply remaining clang formatting

---------

Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2026-02-18 18:34:41 +00:00
Syed Mohammad Saad
c9bda3dad7 Add string algorithms: RemoveStars and ComplexNumberMultiply (#7275)
* Add RemoveStars and ComplexNumberMultiply string algorithms

* Add RemoveStars and ComplexNumberMultiply string algorithms

* Add unit tests for RemoveStars and ComplexNumber Multiply

* Fix checkstyle

* Remove redundant main method

* Move ComplexNumberMultiply to maths package and add input validation with tests

* Apply spotless formatting

---------

Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2026-02-18 18:24:34 +00:00
Mohammed Vijahath
0c79d33eb5 Add Tower of Hanoi recursive algorithm (#7235)
* Add Tower of Hanoi recursive algorithm with tests

* Fix SpotBugs issues and format TowerOfHanoi tests

* Enhance existing TowerOfHanoi and remove duplicate recursion version

* Fix clang-format issue

---------

Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2026-02-11 16:00:15 +01:00
Chahat Sandhu
a14b2345f0 feat: add ElGamalCipher with Safe Prime generation and stateless design (#7257) 2026-02-04 16:49:15 +01:00
Ahmed Allam
8e30bcbb02 refactor: clean up duplicate algorithm implementations to reduce maintenance overhead (#7256)
refactor: remove duplicate algorithm implementations

Removed the following duplicate implementations:
- searches/PerfectBinarySearch.java (duplicate of IterativeBinarySearch)
- searches/SortOrderAgnosticBinarySearch.java (duplicate of OrderAgnosticBinarySearch)
- strings/LongestPalindromicSubstring.java (duplicate of dynamicprogramming version)
- strings/ValidParentheses.java (duplicate of stacks version)
- others/cn/HammingDistance.java (duplicate - strings version handles text)
- others/NewManShanksPrimeTest.java (orphan test in wrong package)

Updated DIRECTORY.md to reflect the changes.

Fixes #7253

Co-authored-by: Ahmed Allam <60698204+AllamF5J@users.noreply.github.com>
2026-02-03 21:32:37 +01:00
Ruturaj Jadhav
3e36833bf4 add Subarray Sum Equals K using prefix sum (#7252)
Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2026-02-01 16:26:57 +01:00
Divyansh Saxena
dfaa495639 Refactor KMP and RabinKarp: Improve Reusability and Test Coverage (#7250)
* first commit

* Running KMPTest and RabinKarpTest with fixed formatting

* now build failed error resolved

* now build failed error resolved 2

---------

Co-authored-by: Divyansh Saxena <divyanshsaxena@gmail.com>
Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2026-02-01 14:51:13 +00:00
Chahat Sandhu
dc3d64f51d feat: add Difference Array algorithm implementation and tests (#7244) 2026-01-27 22:29:05 +01:00
Piotr Idzik
6fdf2db298 style: resolve some of the UTAO_JUNIT_ASSERTION_ODDITIES_USE_ASSERT_EQUALS warnings (#7240) 2026-01-25 12:44:41 +01:00
Piotr Idzik
0b0cefe9f9 style: include UTAO_JUNIT_ASSERTION_ODDITIES_ACTUAL_CONSTANT (#7239) 2026-01-24 14:16:48 +01:00
Piotr Idzik
1a7f8fe79e style: include UTAO_JUNIT_ASSERTION_ODDITIES_IMPOSSIBLE_NULL (#7238) 2026-01-23 09:39:22 +01:00
Piotr Idzik
0f9139dc42 style: include UTAO_JUNIT_ASSERTION_ODDITIES_USE_ASSERT_NOT_EQUALS (#7229) 2026-01-22 09:24:45 +01:00
Piotr Idzik
0e8291e669 style: include UTAO_JUNIT_ASSERTION_ODDITIES_USE_ASSERT_NOT_NULL (#7226) 2026-01-21 10:55:09 +01:00
Piotr Idzik
1b014a2ea4 style: include UTAO_JUNIT_ASSERTION_ODDITIES_USE_ASSERT_NULL (#7225) 2026-01-20 22:02:10 +01:00
Gopesh Pandey
7339b9dfe9 Add distance between two points algorithm (#7218)
* Add distance between two points algorithm

* Create DistanceBetweenTwoPointsTest.java

* DistanceBetweenTwoPoints.java

* Fix test file package and project structure

* Delete src/test/java/com/thealgorithms/DistanceBetweenTwoPointsTest.java

* Apply clang-format compliant formatting

* Apply clang-format

---------

Co-authored-by: a <19151554+alxkm@users.noreply.github.com>
2026-01-19 21:36:03 +01:00
Chahat Sandhu
109ed2e497 feat: Add Prefix Sum category with 1D and 2D implementations (#7220)
Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2026-01-19 10:15:59 +00:00
Chahat Sandhu
1b9373e71a feat: add Bell Numbers algorithm using Aitken's Array (#7219)
* feat: added Bell Numbers algorithm using Aitken's Array

* style: applied clang-format fixes
2026-01-19 09:59:58 +00:00
Ahmed Allam
fd0bcb79e6 Add Middle of Linked List (Slow/Fast Pointers) (#7212)
* feat: implement Smooth Sort algorithm with detailed JavaDoc and test class

* style: format LEONARDO array for improved readability with clang-format

* feat: add MiddleOfLinkedList class and corresponding test cases

* docs: update documentation for MiddleOfLinkedList class

* test: refactor MiddleOfLinkedListTest to improve readability and assertions

* test: refactor MiddleOfLinkedListTest for improved null safety and readability

---------

Co-authored-by: Ahmed Allam <60698204+AllamF5J@users.noreply.github.com>
2026-01-16 12:33:29 +01:00
Ahmed Allam
66f76eb3d9 Add RotatedBinarySearch (search in rotated sorted array) (#7202)
* feat: implement Smooth Sort algorithm with detailed JavaDoc and test class

* style: format LEONARDO array for improved readability with clang-format

* feat(sorts): add TournamentSort (winner-tree)

* test: add unit test for null array handling in TournamentSort

* feat(search): add rotated binary search

* test: add unit test for handling middle element in right sorted half of rotated array

---------

Co-authored-by: Ahmed Allam <60698204+AllamF5J@users.noreply.github.com>
2026-01-14 10:59:25 +01:00
Ahmed Allam
7148661e44 Feat/tournament sort (#7201)
* feat: implement Smooth Sort algorithm with detailed JavaDoc and test class

* style: format LEONARDO array for improved readability with clang-format

* feat(sorts): add TournamentSort (winner-tree)

* test: add unit test for null array handling in TournamentSort

---------

Co-authored-by: Ahmed Allam <60698204+AllamF5J@users.noreply.github.com>
Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
Co-authored-by: Oleksandr Klymenko <19151554+alxkm@users.noreply.github.com>
2026-01-13 23:04:14 +01:00
Tarunj Gupta
1644db2a34 Add Count Nice Subarrays sliding using window algorithm (#7206)
* Add Count Nice Subarrays sliding window algorithm

* Add detailed comments and reference to CountNiceSubarrays

* Fix clang-format issues in CountNiceSubarrays

* Added extra edge cases

* changes made
2026-01-10 16:32:19 +01:00
Ahmed Allam
fe6066b332 Add SmoothSort (Dijkstra’s adaptive in-place heapsort variant) (#7200)
* feat: implement Smooth Sort algorithm with detailed JavaDoc and test class

* style: format LEONARDO array for improved readability with clang-format

---------

Co-authored-by: Ahmed Allam <60698204+AllamF5J@users.noreply.github.com>
2026-01-09 12:36:46 +01:00
Daniel Sánchez Núñez
d382e656f2 Adding method to check wether a matrix is stochastic (#7184)
* Add StochasticMatrix utility class

This class provides methods to check if a matrix is row-stochastic or column-stochastic, ensuring all elements are non-negative and the sums of rows or columns equal 1.

* Change package from maths to matrix

Updated package declaration for StochasticMatrix class.

* Change package declaration in StochasticMatrixTest

* Delete src/test/java/com/thealgorithms/StochasticMatrixTest.java

* Refactor matrix initialization for test cases

* Fix formatting of matrix initialization in tests

* Remove unnecessary newline in StochasticMatrix

* Add import statement for JUnit in StochasticMatrixTest

* Add additional assertions to StochasticMatrixTest

* Clean up import statements in StochasticMatrixTest

Removed unused import statements for cleaner code.

* Reorder import statements in StochasticMatrixTest

* Remove unused import assertThrows from StochasticMatrixTest

Removed unused import for assertThrows.
2025-12-28 10:53:12 +01:00
NYAMATHABAD KOUSHIK SAI
51335cc18a Add Immutable HashMap implementation (#7183)
* Add immutable HashMap implementation

* Add immutable HashMap implementation

* Applied clang-format

---------

Co-authored-by: Koushik Sai <nyamathabadkoushik@gmail.com>
2025-12-27 16:55:23 +00:00
NYAMATHABAD KOUSHIK SAI
2707da25e3 Add recursive factorial implementation with tests (#7178)
* Add recursive factorial implementation with tests

* [STYLE] Apply clang-format

* Move recursive factorial to recursion package and remove duplicate

* Move recursive factorial to recursion package and remove duplicate

* Apply clang-format to factorial recursion tests

* Apply clang-format to factorial recursion tests

---------

Co-authored-by: Koushik Sai <nyamathabadkoushik@gmail.com>
Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2025-12-22 20:15:50 +00:00
Gopal Gupta
bb334881e9 Implement LengthOfLastWord algorithm and add JUnit tests (#7057)
* Implement LengthOfLastWord algorithm in strings package

* Add JUnit tests for LengthOfLastWord algorithm

* style: fix import order and spacing for clang-format
2025-12-20 13:42:34 +00:00
Saniya Mane
7d51c7f320 Add additional edge cases to GCD unit tests (#7174)
* Add additional edge cases to GCD unit tests

* Rename GCD test methods and add descriptive names

* Fix formatting in GCD tests

---------

Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2025-12-18 07:55:03 +00:00
Rajuri Likhitha
2911a7b52c ExtendedEuclideanAlgorithm (#7172)
* commit1

* Fix clang-format spacing in array declarations
2025-12-17 22:14:12 +01:00
NYAMATHABAD KOUSHIK SAI
4f293b33e5 [FEAT] Add Thin Lens formula for ray optics (#7170)
* [FEAT] Add Thin Lens formula for ray optics

* [CI] Re-run clang-format checks

* [CI] Re-run checks after rebase

* [STYLE] Apply clang-format to ThinLensTest

---------

Co-authored-by: Koushik Sai <nyamathabadkoushik@gmail.com>
2025-12-17 16:48:00 +01:00
Taranjeet Singh Kalsi
d9f2ac8340 Added program to check Lucky number (#6962)
added program for lucky number
2025-12-14 10:29:14 +00:00
Muhammad Junaid Khalid
aa91451203 feat: Binary Tree to String algorithm added (Leetcode 606) (#6727)
* feat: BinaryTreeToString.java Added with tests

* fix: javadoc added to BinaryTreeToString

* fix: link to leetcode added
2025-12-14 10:14:00 +00:00
utkarsh patel
bccaf970ba Add Z-Algorithm for Linear-Time String Pattern Matching (#7124)
* Add Z-Algorithm (string pattern matching) with tests

* Add Z-Algorithm (string pattern matching) with tests

* Add Z-Algorithm (string pattern matching) with tests

* Fix checkstyle errors for ZAlgorithm

* Fix: clang-format and checkstyle compliance for ZAlgorithm
2025-12-14 10:08:18 +00:00
Lucas Horn
4f40206684 Add unit tests for graph algorithms (#7133) (#7156)
* Add unit tests for graph algorithms (BFS, DFS, Dijkstra, Bellman-Ford) #7133

- Created BellmanFordTest.java with 10 test methods covering various graph scenarios

- Created ConnectedComponentTest.java with 15 test methods for DFS-based component counting

- Enhanced MatrixGraphsTest.java with additional BFS/DFS test cases

- Enhanced DijkstraAlgorithmTest.java with comprehensive shortest path tests

Fixes #7133

* fix: format files using clang

* fix: removing my changes on pull_request_template

---------

Co-authored-by: prashantdubeypng <vinoddubey6059@gmail.com>
2025-12-10 09:04:32 +00:00
Daniel Sánchez Núñez
316def4ea6 Add Snell's Law refraction algorithm (#7150)
* Add Snell's Law implementation for refraction angle

* Add tests for Snell's Law calculations

* Update documentation with reference link to Snell's Law

Added a reference link to Snell's Law in the documentation.

* Prevent instantiation of SnellLaw class

Make SnellLaw class non-instantiable by adding a private constructor.

* Rename SnellsLawTest to SnellLawTest

* Refactor SnellLawTest for clarity and accuracy

* Rename SnellsLaw.java to SnellLaw.java

* Refactor SnellLawTest with additional assertions

* Refactor SnellLaw class constructor and error handling

Refactor SnellLaw constructor and error message formatting.

* Fix missing newline at end of SnellLawTest.java

Ensure that the SnellLawTest class has a newline at the end of the file.

* Simplify assertions in SnellLawTest

* Simplify exception throwing for total internal reflection
2025-12-07 12:18:55 +01:00
duvvuvenkataramana
8d2cdb27db feat: implement One-Time Pad cipher (#6941) (#7096)
* feat: implement One-Time Pad cipher (#6941)

* style: format OneTimePadCipher with clang-format
2025-12-01 20:17:32 +00:00
Shewale Gopal Sanjay
14c0b0844e Added threaded binary tree (#6995)
* Add One-Time Pad Cipher implementation in Java

* Add One-Time Pad Cipher implementation (fixed)

* Add ThreadedBinaryTree with in-order traversal and tests

* feat: add threaded binary tree implementation and tests

* fix: remove redundant null check for SpotBugs compliance
2025-12-01 20:13:14 +00:00
Taranjeet Singh Kalsi
ac6fef19dc Added program to check either number is Evil or Odious (#6958)
added program for evil number
2025-11-30 22:37:41 +00:00
Taranjeet Singh Kalsi
a9ba87c4b6 Added program to check Abundant number (#6961)
added program for abundant number
2025-11-28 08:51:28 +00:00
Sameer Prajapati
4c5525a2e0 feat: Added Gomory–Hu Tree (all-pairs min-cuts via n1 max-flows) (#6818)
* Add Gomory–Hu Tree (all-pairs min-cuts via n1 max-flows)

* Stabilize Monte Carlo integration with antithetic variates to reduce variance

* Fix Checkstyle in GomoryHuTreeTest: remove inner assignments, add braces, split declarations

* SpotBugs: use RandomGenerator interface in test helper

---------

Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
2025-11-28 08:47:53 +00:00
Gokul45-45
d1ea306920 feat: add Valid Parentheses algorithm using Stack (#7117)
* feat: add Valid Parentheses algorithm using Stack

* fix: add missing ValidParentheses.java implementation

* fix: remove trailing spaces and add newline at EOF

* fix: remove misplaced ValidParentheses.java from root
2025-11-27 11:29:48 +00:00
KANAKALA SAI KIRAN
fa8ea8e614 Feature/centroid decomposition (#7086)
* feat: Add Centroid Decomposition for trees (#7054)

- Implement CentroidDecomposition with O(N log N) construction
- Add CentroidTree class with parent tracking and query methods
- Include buildFromEdges helper for easy tree construction
- Add comprehensive test suite with 20+ test cases
- Cover edge cases, validation, and various tree structures

Closes #7054

* feat: Add Centroid Decomposition for trees (#7054)

- Implement CentroidDecomposition with O(N log N) construction
- Add CentroidTree class with parent tracking and query methods
- Include buildFromEdges helper for easy tree construction
- Add comprehensive test suite with 20+ test cases
- Cover edge cases, validation, and various tree structures

Closes #7054

* fix: Remove trailing whitespace from CentroidDecompositionTest

* fix: Remove trailing whitespace and add newlines at end of files

* fix: Format code to comply with clang-format and checkstyle requirements

* style: Fix lambda formatting in test assertions

- Change single-line lambdas to multi-line format
- Align with repository code style guidelines

* style: Apply clang-format to match repository style guide

- Format code according to .clang-format configuration
- Use single-line lambdas as allowed by AllowShortLambdasOnASingleLine: All
- Apply 4-space indentation
- Ensure proper line endings
2025-11-22 09:44:34 +00:00