Swift Coding Test Course, Gift Delivery

Hello, today we will discuss one of the algorithm problems that frequently appears in Swift coding tests, “Gift Delivery.” This problem often comes up in interviews and coding tests, and helps develop algorithmic thinking.

Problem Description

You and your friends have decided to hold a birthday party. All friends want to give and receive gifts from each other. However, it has been decided that a friend cannot give a gift to themselves. Each friend has promised to give a gift to a specific friend. Given that there are a total of N friends and the list of gifts they promised to give, write a program to output the gifts each friend will receive.

Input Format

  • The first line contains the number of friends N (1 ≤ N ≤ 100).
  • The second line contains the indices of the friends to whom each friend promised to give a gift. (Friends are numbered from 1 to N)

Output Format

Output the friend number of the gift each friend will receive, one per line.

Example Input

5
2 3 4 5 1
    

Example Output

1
2
3
4
5
    

Problem Solving Process

Step 1: Understand the Problem

First, we need to understand the problem. From the given input, we can know who each friend will give a gift to. Each friend needs to know the index of the friend from whom they will receive a gift. Hence, the output can be thought of as an array based on the indices each friend has promised to give.

Step 2: Design Data Structure

We will prepare an array to store the input values and an array for each friend’s desired gifts. Since friend numbers start from 1, it is convenient to declare the array with size N + 1.

Step 3: Design Problem Solving Algorithm

The algorithm is as follows:

  1. Receive the input and declare a gift array of size N + 1.
  2. Store the input gift boxes in the gift array.
  3. Print the gifts corresponding to each friend’s index from the app.

Step 4: Swift Implementation

Now let’s implement the algorithm using Swift:

import Foundation

// Input the number of friends
let N = Int(readLine()!)!

// Gift delivery array
var gift = Array(repeating: 0, count: N + 1)

// Input gift distribution
let gifts = readLine()!.split(separator: " ").map { Int($0)! }

// Packing into the array
for i in 1...N {
    gift[gifts[i - 1]] = i
}

for i in 1...N {
    print(gift[i])
}

Step 5: Code Explanation

To briefly explain the code:

  1. First, we receive the number of friends, N.
  2. We declare an array called gift with a size of N + 1. (Since array indices start from 1)
  3. Then, through the second input, we receive the indices of friends to whom gifts will be given as an array called gifts.
  4. Using a loop, we store the gifts each friend will receive in the gift array, saving them at the respective friend’s index using index i.
  5. After completing all processes, we print the gifts that friends will receive.

Conclusion

This problem is a basic array problem given in most coding tests. By understanding the problem and applying appropriate data structures and algorithms, it can be easily solved. Approaching algorithm problems in various ways greatly helps improve your skills. I hope everyone becomes more familiar with Swift through this process!