Repository: TheAlgorithms/Java
Pull request #6052 (2024-10-28 12:54:36)

feat: Adding uniform number counting algorithm
<!--
Thank you for your contribution!
To reduce the number of notifications sent to the maintainers, please:
- create your PR as a draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my work; I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [X] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6052?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `91.66667%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 72.40%. Comparing base [(`bca8d0e`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/bca8d0efe0097423110276a3d540a2be97de5154?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`fc44a87`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/fc44a87ef01be0423ebdb0c60b51d0513074bb8e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6052?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...n/java/com/thealgorithms/maths/UniformNumbers.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6052?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fmaths%2FUniformNumbers.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9tYXRocy9Vbmlmb3JtTnVtYmVycy5qYXZh) | 91.66% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6052?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##             master    #6052   +/-   ##
=========================================
  Coverage     72.40%   72.40%           
- Complexity     4983     4989    +6     
=========================================
  Files           653      654    +1     
  Lines         17574    17586   +12     
  Branches       3386     3388    +2     
=========================================
+ Hits          12724    12733    +9     
- Misses         4371     4372    +1     
- Partials        479      481    +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6052?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 0c9aeca4..e34b9eba 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -466,6 +466,7 @@
             * [SumWithoutArithmeticOperators](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SumWithoutArithmeticOperators.java)
             * [TrinomialTriangle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/TrinomialTriangle.java)
             * [TwinPrime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/TwinPrime.java)
+            * [UniformNumbers](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/UniformNumbers.java)
             * [VampireNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/VampireNumber.java)
             * [VectorCrossProduct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/VectorCrossProduct.java)
             * [Volume](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Volume.java)
@@ -597,6 +598,7 @@
             * [UnionFind](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UnionFind.java)
             * [UpperBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UpperBound.java)
           * slidingwindow
+            * [LongestSubarrayWithSumLessOrEqualToK](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java)
             * [LongestSubstringWithoutRepeatingCharacters](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharacters.java)
             * [MaxSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarray.java)
             * [MinSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java)
@@ -1119,6 +1121,7 @@
             * [SumWithoutArithmeticOperatorsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumWithoutArithmeticOperatorsTest.java)
             * [TestArmstrong](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TestArmstrong.java)
             * [TwinPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java)
+            * [UniformNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/UniformNumbersTest.java)
             * [VolumeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/VolumeTest.java)
           * misc
             * [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
@@ -1228,6 +1231,7 @@
             * [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
             * [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
           * slidingwindow
+            * [LongestSubarrayWithSumLessOrEqualToKTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java)
             * [LongestSubstringWithoutRepeatingCharactersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharactersTest.java)
             * [MaxSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarrayTest.java)
             * [MinSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarrayTest.java)
diff --git a/src/main/java/com/thealgorithms/maths/UniformNumbers.java b/src/main/java/com/thealgorithms/maths/UniformNumbers.java
new file mode 100644
index 00000000..c83783aa
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/UniformNumbers.java
@@ -0,0 +1,50 @@
+package com.thealgorithms.maths;
+
+/**
+ * A positive integer is considered uniform if all
+ * of its digits are equal. For example, 222 is uniform,
+ * while 223 is not.
+ * Given two positive integers a and b, determine the
+ * number of uniform integers between a and b.
+ */
+public final class UniformNumbers {
+    // Private constructor to prevent instantiation of the utility class
+    private UniformNumbers() {
+        // Prevent instantiation
+    }
+    /**
+     * This function will find the number of uniform numbers
+     * from 1 to num
+     * @param num upper limit to find the uniform numbers
+     * @return the count of uniform numbers between 1 and num
+     */
+    public static int uniformNumbers(int num) {
+        String numStr = Integer.toString(num);
+        int uniformCount = (numStr.length() - 1) * 9;
+        int finalUniform = Integer.parseInt(String.valueOf(numStr.charAt(0)).repeat(numStr.length()));
+
+        if (finalUniform <= num) {
+            uniformCount += Integer.parseInt(String.valueOf(numStr.charAt(0)));
+        } else {
+            uniformCount += Integer.parseInt(String.valueOf(numStr.charAt(0))) - 1;
+        }
+
+        return uniformCount;
+    }
+    /**
+     * This function will calculate the number of uniform numbers
+     * between a and b
+     * @param a lower bound of range
+     * @param b upper bound of range
+     * @return  the count of uniform numbers between a and b
+     */
+    public static int countUniformIntegers(int a, int b) {
+        if (b > a && b > 0 && a > 0) {
+            return uniformNumbers(b) - uniformNumbers(a - 1);
+        } else if (b == a) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/UniformNumbersTest.java b/src/test/java/com/thealgorithms/maths/UniformNumbersTest.java
new file mode 100644
index 00000000..ac46c000
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/UniformNumbersTest.java
@@ -0,0 +1,56 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+class UniformNumbersTest {
+
+    @Test
+    void testSingleUniformDigitRange() {
+        assertEquals(1, UniformNumbers.countUniformIntegers(1, 1));
+        assertEquals(9, UniformNumbers.countUniformIntegers(1, 9));
+    }
+
+    @Test
+    void testSmallRange() {
+        assertEquals(1, UniformNumbers.countUniformIntegers(10, 11));
+        assertEquals(2, UniformNumbers.countUniformIntegers(22, 33));
+    }
+
+    @Test
+    void testRangeWithNoUniformNumbers() {
+        assertEquals(0, UniformNumbers.countUniformIntegers(12, 21));
+        assertEquals(0, UniformNumbers.countUniformIntegers(123, 128));
+    }
+
+    @Test
+    void testRangeWithAllUniformNumbers() {
+        assertEquals(9, UniformNumbers.countUniformIntegers(1, 9));
+        assertEquals(18, UniformNumbers.countUniformIntegers(1, 99));
+    }
+
+    @Test
+    void testMultiDigitRangeWithUniformNumbers() {
+        assertEquals(1, UniformNumbers.countUniformIntegers(100, 111));
+        assertEquals(2, UniformNumbers.countUniformIntegers(111, 222));
+    }
+
+    @Test
+    void testExactUniformBoundary() {
+        assertEquals(1, UniformNumbers.countUniformIntegers(111, 111));
+        assertEquals(2, UniformNumbers.countUniformIntegers(111, 222));
+    }
+
+    @Test
+    void testLargeRange() {
+        assertEquals(27, UniformNumbers.countUniformIntegers(1, 999));
+        assertEquals(36, UniformNumbers.countUniformIntegers(1, 9999));
+    }
+
+    @Test
+    void testInvalidRange() {
+        assertEquals(0, UniformNumbers.countUniformIntegers(500, 100));
+        assertEquals(0, UniformNumbers.countUniformIntegers(-100, -1));
+    }
+}