Packets are sent to different ports on a computer system based on the has of their packet ID. The value of the has is as given below:
hash = mod(packet_id, numberOfPorts)
where mod is the modulus operator and takes the mod of first operand by second operand.
The ports are numbered from 0
to (number of ports) - 1
and a packet is initially sent to the port that has the port number equal to the has of its packet ID. Each port requires a time t to send a packet. If a port is currently sending a packet, this packet is then sent to the next port number, and so on. Given a list IDs of n
packets that arrive 1
per second, find the port to which each packet is finally sent. The first packet is sent at time t = 1
.
Function Description
Complete the sentTimes
which has the following parameters:
int numberOfPorts
: the number of ports in the systemint transmissionTime
: the time for a port to send a packet.int packetIds[n]
: the IDs of the packet in the order in which they arriveExample 1:
Input: numberOfPorts = 3, transmissionTime = 2, packetIds = [4, 7, 10, 6]
Output: [1, 2, 1, 0]
Explanation:The destination ports, assuming no time conflicts are all calculated as packetIds[i] modulo numberOfPorts, so [1, 1, 1, 0] in this case. These arrive at times 1, 2, 3, 4. The first packet is sent to port 1 with no conflicts. Port 1 will be occupied at times 1 and 2 due to the transmission time, so the second packet has a conflict and is sent to port 1 + 1 = 2. The third packet wants to go to port 1 and arrives at time 3. Since port 1 is no longer transmitting packet 1, it receives the third packet. The fourth packet goes to port 0 without conflicts. The return array is [1, 2, 1, 0].
N/A (If you know about it, feel free to contact us. TYVM!
input:
output: