Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,236 questions

56,139 answers

573 users

How to count the numbers with even number of digits in an array with Kotlin

1 Answer

0 votes
/*
    Architecture notes:
    -------------------
    This program demonstrates a modular design:
    - countDigits: counts digits using integer math.
    - hasEvenDigits: checks if a number has an even digit count.
    - countEvenDigitNumbers: processes an array and returns the count.
    - Multiple test cases in main.

    Performance notes:
    ------------------
    - All operations are O(n) for n array elements.
    - Digit counting uses integer division, avoiding string conversions.
    - Negative numbers handled safely.
    - Uses Kotlin's standard library for clarity and safety.

    Security notes:
    ---------------
    - No unsafe operations.
    - No unchecked indexing.
    - No external dependencies.
*/

// Count the number of digits in an integer.
// Complexity: O(log n) due to repeated division by 10.
// Pitfall: negative numbers must be handled correctly.
fun countDigits(value: Int): Int {
    if (value == 0) return 1 // zero has one digit

    var n = kotlin.math.abs(value)
    var count = 0

    while (n > 0) {
        n /= 10
        count++
    }

    return count
}

// Returns true if the number has an even number of digits.
fun hasEvenDigits(value: Int): Boolean =
    countDigits(value) % 2 == 0

// Count how many numbers in an array have an even number of digits.
// Complexity: O(n)
fun countEvenDigitNumbers(arr: IntArray): Int =
    arr.count { hasEvenDigits(it) }

// Utility: print an array in a readable format.
fun printArray(arr: IntArray) {
    print("[ ")
    arr.forEach { print("$it ") }
    print("]")
}

fun main() {
    println("=== Count Numbers with Even Number of Digits (Kotlin) ===")
    println()

    // Test cases
    val a1 = intArrayOf(12, 345, 2, 6, 7896)
    val a2 = intArrayOf(0, -22, 1000, -7)
    val a3 = intArrayOf(1, 3, 5)          // No even-digit numbers
    val a4 = intArrayOf(10, 99, 1001)     // All even-digit numbers
    val a5 = intArrayOf()                 // Edge case: empty array
    val a6 = intArrayOf(-100000, 500000)  // Large numbers

    // Test 1
    print("Test 1: ")
    printArray(a1)
    println(" -> Count = ${countEvenDigitNumbers(a1)}")

    // Test 2
    print("Test 2: ")
    printArray(a2)
    println(" -> Count = ${countEvenDigitNumbers(a2)}")

    // Test 3
    print("Test 3: ")
    printArray(a3)
    println(" -> Count = ${countEvenDigitNumbers(a3)}")

    // Test 4
    print("Test 4: ")
    printArray(a4)
    println(" -> Count = ${countEvenDigitNumbers(a4)}")

    // Test 5: empty array
    print("Test 5: ")
    printArray(a5)
    println(" -> Count = ${countEvenDigitNumbers(a5)}")

    // Test 6: large numbers
    print("Test 6: ")
    printArray(a6)
    println(" -> Count = ${countEvenDigitNumbers(a6)}")
}


/*
run:

=== Count Numbers with Even Number of Digits (Kotlin) ===

Test 1: [ 12 345 2 6 7896 ] -> Count = 2
Test 2: [ 0 -22 1000 -7 ] -> Count = 2
Test 3: [ 1 3 5 ] -> Count = 0
Test 4: [ 10 99 1001 ] -> Count = 3
Test 5: [ ] -> Count = 0
Test 6: [ -100000 500000 ] -> Count = 2

*/

 



answered 1 hour ago by avibootz

Related questions

...