Skip to the code
+25 XP

#71 Missing Page Numbers

medium

A paginated API should have returned pages 1 to n, where n is the number of responses received. Some pages came back twice, so others never arrived. The page numbers received are in pages.

Write missingPages(pages) that returns every number from 1 to n that is not in pages, in increasing order. Use O(1) extra memory besides the answer; you may reorder pages.

Example 1

Input: pages = [3,5,2,3,1,5]
Output: [4,6]

3 and 5 came twice, so pages 4 and 6 never arrived.

Example 2

Input: pages = [1,2,3]
Output: []

Every page arrived.

Constraints

  • 1 <= pages.length <= 5 * 10^5
  • 1 <= pages[i] <= pages.length
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).