Years (Codeforces 1424G)
Tags: Others, sortings

During one of the space missions, humans have found an evidence of previous life at one of the planets. They were lucky enough to find a book with birth and death years of each individual that had been living at this planet. What's interesting is that these years are in the range $$$(1, 10^9)$$$! Therefore, the planet was named Longlifer.In order to learn more about Longlifer's previous population, scientists need to determine the year with maximum number of individuals that were alive, as well as the number of alive individuals in that year. Your task is to help scientists solve this problem!

Input: The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of people.Each of the following $$$n$$$ lines contain two integers $$$b$$$ and $$$d$$$ ($$$1 \le b \lt d \le 10^9$$$) representing birth and death year (respectively) of each individual.

Output: Print two integer numbers separated by blank character, $$$y$$$  — the year with a maximum number of people alive and $$$k$$$  — the number of people alive in year $$$y$$$.In the case of multiple possible solutions, print the solution with minimum year.

Note: You can assume that an individual living from $$$b$$$ to $$$d$$$ has been born at the beginning of $$$b$$$ and died at the beginning of $$$d$$$, and therefore living for $$$d$$$ - $$$b$$$ years.

Sample Inputs:
3
1 5
2 4
5 6

4
3 4
4 5
4 6
8 10

Sample Outputs:
2 2

4 2

Solution (Python):
def years():
    n = int(input())
    birth = []
    death = []
    for _ in range(n):
        line = input().split()
        birth.append(int(line[0]))
        death.append(int(line[1]))
    birth.sort()
    death.sort()
    cont = 0
    max = 0
    ano_max = 0
    i_death = 0
    for i in range(len(birth)):
        cont += 1
        while death[i_death] <= birth[i]:
            cont -= 1
            i_death += 1
        if cont > max:
            max = cont
            ano_max = birth[i]
    print(ano_max, max)
years()

Solution (Python):
n = int(input())
aux = {}
for _ in range(n):
    (x, y) = map(int, input().split())
    aux[x] = aux.get(x, 0) + 1
    aux[y] = aux.get(y, 0) - 1
_max = 0
curr = 0
people = 0
for key in sorted(aux.keys()):
    curr += aux[key]
    if curr > people:
        people = curr
        _max = key
print(_max, people)

Solution (Python):
import math
from collections import *
import math

def solve():
    n = int(input())
    a = []
    b = []
    for i in range(n):
        (x, y) = map(int, input().split())
        a.append(x)
        b.append(y)
    a.sort()
    b.sort()
    (i, j, maxi, y, c) = (0, 0, float('-inf'), -1, 0)
    while i < n and j < n:
        if a[i] < b[j]:
            c += 1
            if c > maxi:
                y = a[i]
                maxi = c
            i += 1
        else:
            c -= 1
            j += 1
    print(y, maxi)
solve()