Skip to content

Instantly share code, notes, and snippets.

@mwshubham
Last active June 8, 2020 13:57
Show Gist options
  • Save mwshubham/aa7ee6cf5a1f79ecafaf733129ccd537 to your computer and use it in GitHub Desktop.
Save mwshubham/aa7ee6cf5a1f79ecafaf733129ccd537 to your computer and use it in GitHub Desktop.
result = []
n = int(input())
arr = list(map(int, input().split()))
for i in range(0,2*n, 2):
result.append([arr[i],arr[i+1]])
# sort a array based on first element
result.sort(key=lambda x: x[0])
j = 0
while j < len(result)-1:
if result[j][1] >= result[j+1][0]:
if result[j][1] <= result[j+1][1]:
result[j][1] = result[j+1][1]
del result[j+1]
else:
j = j + 1
for i in range(len(result)):
print(result[i][0], result[i][1], end =" ")
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment