Given sorted entry and exit timestamps, find the smallest nonnegative time difference d that maximizes how many entry times t satisfy t + d is an exit time.
Medium5Hash mapArrayBrute forceImplementationInterviewNo attempts yetTime limit7sMemory limit512 MB
Cakey McCakeFace bakes its signature pastry, the Unknowable Cake, every day in a Paris facility. The cake succeeds or fails on one number, the cooking time, and that number is a closely guarded secret. Eve, a well known spy, wants to steal it, and your job is to help her.
Cakes are cooked in a single huge oven that has exactly one front door and one back door. Uncooked cakes go in through the front door. Once the exact secret cooking time has passed, a cake comes out through the back door. Only one cake can pass through the front door or the back door at any given moment.
Eve has secretly installed detectors at the front and the back of the oven. A detector records a signal every time a cake passes through its door. A cake that goes through the front door at time t triggers the entry detector at t, and it triggers the exit detector exactly cooking_time later when it goes through the back door. Every cake here is perfectly cooked.
After a few days Eve receives two sets of timestamps in milliseconds, one from each detector. The detectors are faulty: sometimes one fires when no cake passed, and sometimes one fails to fire when a cake did pass. Eve decided she can guess the secret cooking time well by finding the time difference that maximizes the number of correspondences between entry times and exit times. Compute that difference.
For a time difference d, the number of correspondences is the number of recorded entry times t such that t+d is also recorded as an exit time.
Limits
A single integer: the best guess of the secret cooking time, that is, the time difference d≥0 that maximizes the number of correspondences. If several time differences reach that maximum, print the smallest one. If no time difference produces a single correspondence, print 0.