#K85082. Top K Frequent IP Addresses
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:
- The first line contains an integer \( n \), which is the number of log entries.
- The second line contains \( n \) space-separated IP addresses.
- 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.
## sample7
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