A platform team rolls a new build out to a fleet of n servers, numbered 1 through n in rollout order. Once a server exhibits the regression, every server rolled out after it exhibits it too — the fault only ever gets worse as the rollout proceeds, never better — so the fleet's health record (one flag per server, 0 for healthy and 1 for regressed) is guaranteed to look like a block of 0s followed by a block of 1s. At least one server in the fleet is regressed. Given the full health record, find the number of the very first server that is regressed.
n — the number of servers.n integers, each 0 or 1 — the health flags for servers 1 through n, in order. The sequence is guaranteed non-decreasing, and the last flag is guaranteed to be 1.Print a single integer: the smallest 1-indexed server number whose flag is 1.
Example 1
Input
5 0 0 1 1 1
Expected
3
Explanation
The flags are 0,0,1,1,1 for servers 1 through 5. Servers 1 and 2 are healthy, and server 3 is the first with flag 1, so the answer is 3.
Example 2
Input
1 1
Expected
1
Explanation
There is only one server, server 1, and it already carries the regression, so the answer is 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →