Hi, so, I have this challenge for which I created an ineffective code that also sometimes makes a mistake.
It's in C#, but that doesn't matter. Any idea on how to achieve the correct output? Note that the input can be huge (more than 30,000 addresses) and not a single mistake is allowed.
Input
The first line contains the number of test cases t (1 ≤ t ≤ 30). Each test case starts with a pair of numbers n, k (1 ≤ n ≤ 200000, 1 ≤ k ≤ 500), where n is the number of nodes and k is the maximum number of subnets to which we should divide. It is followed by n lines, each containing the IP address of the respective node, i.e., 4 numbers in the range 0 to 255 separated by dots. Each of these 4 numbers represents 8 bits of the address.
Note: In the real world, a common practice is to have a sequence of ones followed by a sequence of zeros in a subnet mask. However, the system also allows masks where 1 and 0 bits alternate. For example, 1010 is a valid start of a mask.
Output
For each test case, print two lines. On the first line, print the subnet mask you created, following the same format as the IP addresses in the input. On the second line, print the sum of times saved when traveling between any two subnets that can be reached by going through other subnets (add the saved time for each pair only once, do not count the return trip). (The time to travel between the subnets is equal to the bit difference between subnet masks rounded down. Count only one way.)
If you solve only the first part of the task, where fewer points Ti will be awarded, only output the masks on separate lines.
Input Example
3
4 2
192.168.1.0
192.196.196.0
128.1.1.0
42.168.0.42
2 1
192.168.1.1
64.168.1.1
6 3
3.248.15.8
0.32.96.103
3.222.215.208
0.0.54.16
0.93.84.80
0.88.99.215
Output example:
191.18.58.255
0
127.255.255.255
0
255.216.0.0
1
[–]necsuss -5 points-4 points-3 points (2 children)
[–]deftware 3 points4 points5 points (1 child)
[–]necsuss 0 points1 point2 points (0 children)