kotlin coding test course, helping the less fortunate

This course explains the process of solving employment-related algorithm problems using the Kotlin language step by step. This problem is based on the theme ‘Helping the Underprivileged,’ and it will proceed by deriving the correct output for the given input.

Problem Description

A charity organization in Korea delivers donations to the underprivileged every month. When data about fundraising is given,
write a program to calculate the total amount of donations and the average donation per applicant.

Input Format


T: Number of test cases (1 <= T <= 1000)
Each test case includes:
N: Number of donors (1 <= N <= 100)
The next line contains N integers representing each donor's donation (0 <= donation <= 1,000,000)

Output Format

For each test case, print the total donations on the first line and the average donation (rounded to the nearest integer) on the second line.

Sample Input

        2
        3
        1000 2000 3000
        4
        500 600 700 800
        

Sample Output

        6000
        2000
        2600
        650
        

Solution Method

To solve the problem, follow these steps:

  1. Getting Input: Read data through standard input.
  2. Calculating Total and Average Donation: For each test case, sum up the donations and calculate the average.
  3. Formatting Output: Print the results according to the requirements.

Code Implementation

        fun main() {
            val reader = System.`in`.bufferedReader()
            val T = reader.readLine().toInt() // Number of test cases
            
            repeat(T) {
                val N = reader.readLine().toInt() // Number of donors
                val donations = reader.readLine().split(" ").map { it.toInt() } // List of donations
                
                val total = donations.sum() // Total donations
                val average = total / N.toDouble() // Average donation (including decimals)
                
                println(total) // Print total donations
                println(average.roundToInt()) // Average donation (rounded to the nearest integer)
            }
        }
        

Code Explanation

Analysis and explanation of the code:

  • Reading Input: Use reader.readLine().toInt() to read the first input (T) and read the number of donors (N) and the list of donations across multiple lines.
  • Calculating Donations: Easily calculate total donations with donations.sum() and derive the average donation with total / N.toDouble().
  • Outputting Results: For each test case, output the results in two lines. Use average.roundToInt() to round the decimals.

Time Complexity

The time complexity of this algorithm is O(N). This is because we traverse the list of donations to calculate the sum for each test case.
Here, N denotes the number of donors in each test case.

Conclusion

This problem is a simple algorithm problem for managing and calculating donations, but
it can be solved more efficiently through the various features of the Kotlin language.
I encourage you to practice solving algorithm problems effectively in preparation for coding tests in a similar manner.