#K85082. Top K Frequent IP Addresses

    ID: 36562 Type: Default 1000ms 256MiB

Top K Frequent IP Addresses

Top K Frequent IP Addresses

You are given a log of n IP addresses. Your task is to identify the top k IP addresses that appear most frequently. In case of ties in frequency, the IP addresses should be sorted in lexicographical order.

Formally, let \( n \) be the number of log entries and let the list of IP addresses be \( \{ip_1, ip_2, \dots, ip_n\} \). You need to determine the top \( k \) IP addresses, sorted by their frequency (in descending order) and then by lexicographical order if the frequencies are equal.

It is guaranteed that \( 1 \leq k \leq n \) and each IP address is a valid IPv4 address.

inputFormat

The input is given from stdin in the following format:

  1. The first line contains an integer \( n \), which is the number of log entries.
  2. The second line contains \( n \) space-separated IP addresses.
  3. The third line contains an integer \( k \), the number of top frequent IP addresses to output.

outputFormat

Output the top \( k \) frequent IP addresses to stdout in one line, separated by a single space.

## sample
7
192.168.0.1 192.168.0.2 192.168.0.1 192.168.0.3 192.168.0.2 192.168.0.3 192.168.0.3
2
192.168.0.3 192.168.0.1