Swift Coding Test Course, Jumong’s Command

Hello! Today, as part of the Swift coding test course, we will cover a topic titled ‘The Command of Jumong’. Through this problem, we can learn various techniques necessary for solving algorithmic problems.

Problem Description

In an ancient kingdom, Jumong decided to give commands to his subordinates. The command is in the following format:

“Everyone, X go out from your places!”

According to this command, the subordinates move. However, the distances they move may vary, and some subordinates may ignore Jumong’s command and not go out. The problem is to find the number of subordinates who follow Jumong’s command.

Input

  • The number of subordinates N (1 ≤ N ≤ 105)
  • The distance K that the subordinates follow Jumong’s command (1 ≤ K ≤ 106)
  • An array A of distances that N subordinates follow (0 ≤ A[i] ≤ 106)

Output

Output the number of subordinates who follow Jumong’s command.

Example Input

   5
   10
   8 9 10 11 12
   

Example Output

   3
   

Problem Solving Process

The first step to solve this problem is to accurately understand the input. Here, we need to receive the number of subordinates, the command distance, and the distances each subordinate follows.

Step 1: Get Input

We can use Swift’s basic input functions to receive the data.

   let n = Int(readLine()!)!
   let k = Int(readLine()!)!
   let distances = readLine()!.split(separator: " ").map { Int(String($0))! }
   

Step 2: Count Subordinates That Meet the Condition

To count the number of subordinates who follow Jumong’s command, we will iterate through the given distance array and check if each subordinate’s distance is greater than or equal to the command distance K. We will increase the count of subordinates that meet the condition through this process.

   var count = 0
   for distance in distances {
       if distance >= k {
           count += 1
       }
   }
   

Step 3: Output Result

Finally, we will output the number of subordinates who follow Jumong’s command.

   print(count)
   

Final Code

   import Foundation
   
   let n = Int(readLine()!)!
   let k = Int(readLine()!)!
   let distances = readLine()!.split(separator: " ").map { Int(String($0))! }
   
   var count = 0
   for distance in distances {
       if distance >= k {
           count += 1
       }
   }
   
   print(count)
   

Summary

In this lesson, we learned how to solve a simple algorithmic problem using the Swift language through the problem ‘The Command of Jumong’. We practiced how to approach the problem and write the necessary code for each step.

This problem-solving process is very similar to the thought process required in actual coding interviews, so I hope you improve your skills through practice!

Additional Practice Problems

If you want more practice, try solving the following additional problems.

  • Output the number of subordinates whose distances are less than Jumong’s command distance K.
  • Calculate and output the difference between the maximum and minimum distances of subordinates.
  • Randomly generate Jumong’s command distance K, and calculate the ratio of N subordinates who follow that command.

Thank you!