Add uniform number counting algorithm (#6052)

This commit is contained in:
Muhammad Junaid Khalid
2024-10-29 22:46:29 +05:00
committed by GitHub
parent 5e9d1dcdcd
commit d1c1e6b4d2
3 changed files with 110 additions and 0 deletions

View File

@@ -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;
}
}
}

View File

@@ -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));
}
}