Given n intervals with distinct endpoint positions, find the maximum number of intervals fully contained in some segment of fixed length d.
Medium6SortingSliding windowTwo pointersIntervalsInterviewNo attempts yetTime limit1sMemory limit512 MBThere are n people who commute between home and office. Each person's home and office sit at two different points on a horizontal line. For any two people A and B, the position of A's home or office may coincide with the position of B's home or office.
To help the commuters, a railroad is laid between two points on that line and a train runs along it. The budget is limited, so the length of the railroad is fixed at d. Place the railroad so that the number of people whose home and office both lie on it is as large as possible.
You are given a positive integer d and n integer pairs (hi,oi) for 1≤i≤n, where hi is the home position of person i and oi is the office position. Over all segments L of length d, find the largest number of people whose home and office are both contained in L. A segment contains its two endpoints.

Figure 1. Home and office positions of eight people
Figure 1 shows the home and office positions of eight people. With d=30, the red segment from position 10 to position 40 is one of the segments that contain the homes and offices of the most people, and that count is 4.
Input is read from standard input. The first line has a positive integer n (1≤n≤100,000), the number of people. Each of the next n lines has an integer pair hi and oi separated by a space. Both hi and oi lie between −100,000,000 and 100,000,000, and they differ from each other. The last line has an integer d (1≤d≤200,000,000), the length of the railroad.
Output is written to standard output. Print on one line the largest number of people whose home and office both lie inside a segment of length d.