Given a partial daily log of days-since-breakout values with a breakout on day 1, find the minimum and maximum possible breakouts.
Medium4GreedyArrayImplementationSimulationInterviewNo attempts yetTime limit2sMemory limit512 MBEarly in the morning, Farmer John woke up to the sound of splintering wood. The cows were breaking out of the barn again.
Farmer John was sick of the morning breakouts and decided it was time to get tough. He nailed a counter to the barn wall that tracks the number of days since the last breakout. If a breakout happens in the morning, the counter reads 0 that day. If the most recent breakout was 3 days ago, the counter reads 3. Farmer John wrote the counter down every single day.
The end of the year has come and Farmer John is ready to do some accounting. The cows will pay, he says. But some entries of his log are missing.
Farmer John is sure that he started the log on the morning of a breakout. Over all sequences of events that agree with the log entries that remain, find the minimum and the maximum number of breakouts that may have taken place during the logged period.
The first line contains one integer N (1≤N≤100), the number of days Farmer John logged the counter.
The second line contains N space separated integers. The ith integer is either −1, meaning the entry for day i is missing, or a non-negative integer ai (at most 100), meaning the counter read ai on day i.
If no sequence of events agrees with both the remaining log entries and the fact that the cows broke out on the morning of day 1, print the single integer −1. Otherwise print two space separated integers m and M, where m is the smallest number of breakouts over all consistent sequences and M is the largest.
In the sample the counter reads 1 on day 4, so a breakout had to occur on day 3. A breakout also occurred on day 1, so the only uncertainty left is day 2. The total is therefore between 2 and 3.