AlgoUniversity
  • Go Back
Discussion
Unoccupied Distance :

Author

Lokesh

Difficulty Level : Easy

Submissions : 298

Asked In : Uber

Marks :10

: 2 | : 0

Consider a road segmented in $$$n$$$ parts. The index of the first segment is $$$1$$$. The road has multiple lanes.

There are $$$m$$$ cars on the road, facing towards right, $$$i^{th}$$$ cars front starts at segment $$$start[i]$$$ and rear ends at segment $$$end[i]$$$.

Since there are multiple lanes, there can be two cars that share segments.

Find the length of the longest continuous segments which are unoccupied by the cars (without regard to lanes).

Example: 5 cars in 10 segments:

Segment12345678910
Lane 1.0.......
Lane 22222.....1
Lane 3.333.44...

In this example, we can see that the continuous unoccupied segment sets are $$$\{5\}$$$ and $$$\{8,9\}$$$. The one with the longest length is $$$\{8,9\}$$$, with length $$$2$$$.

Constraints:

$$$1\le n\le 10^9$$$.

$$$1\le m\le 2*10^5$$$

$$$1\le finish[i] \le start[i] \le n$$$

Input

The first line contains two integers $$$n,m$$$.

The second line contains $$$m$$$ integers, $$$i^{th}$$$ value denoting the start of $$$i^{th}$$$ car.

The second line contains $$$m$$$ integers, $$$i^{th}$$$ value denoting the end of $$$i^{th}$$$ car.

Output

Output the length of the longest continuous segments which are not occupied.

Example

Input
10 5
2 10 4 5 7
2 8 1 3 6
Output
0

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.