Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9045297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:15:29+00:00 2026-06-16T11:15:29+00:00

From the book Programming Pearls 2nd Edition, quoting the question A from Column 2,

  • 0

From the book Programming Pearls 2nd Edition, quoting the question A from Column 2, section 2.1

Given a sequential file that contains at most four billion integers in
random order, find a 32-bit integer that isn’t in the file (and there
must be at least one missing – why ?). How would you solve this
problem with ample quantities of main memory ? How would you solve it
if you could use several external “scratch” files but only a few
hundred bytes of main memory ?

The problem statement says “at most” four billion integers. So one valid input could be in the range 100 – 299 with one missing number. If this understanding of the problem statement is correct then the required inputs for this problem are the file containing the numbers and also the range of the numbers in the file, ie: i to n.

For this problem isn’t the following O(n) solution more intuitive than the one given in the book (from Ed Reingold) ? Or am I missing something ?

Assume the given range is i…n

using the forumla (n * (n + 1) / 2) 
  x = the sum of numbers from 1 to i-1 
  y = the sum of numbers from 1 to n 
walk through the input and get a sum of the numbers (value z)

missing number = (y - x - z)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:15:30+00:00Added an answer on June 16, 2026 at 11:15 am

    You are missing something:

    1. The numbers don’t said to be unique, the summation approach
      assumes unique elements and only one missing in the range
    2. You don’t know the range of the numbers, in 32 bit integer can have
      much more then 4B elements (to be exact, there are 294967296 more numbers that can be represented by 32 bits then 4B)

    Look at the simplified counter example with range = [1,5], array = [5,5,5,4,1].

    In this case, you will get x=1, y = 15, z = 20.

    However, 20-15-1 = 4, and it is not missing.

    You can use radix sort, which runs in O(n) in this case (because 32 bits is constant), and then scan the sorted array to find the first missing element.

    EDIT: A more efficient way to do it is yet with a variation of radix sort and selection algorithm:

    1. Let your current number of bits be k. Look at the first bit, and partition the array into two parts, the first with this bit unset, and the second with this bit set.
    2. In at least one of the parts – there must be less then (2^k) / 2 = 2^(k-1) elements.
    3. Reduce the problem to this sub array only and use k' = k-1 (reduce the current number of bits) and return to 1.

    Keep doing it until you exhausted your bits, and you will get a number that is not in the original list.

    Note that assuming the list is random enough, the complexity of the algorithm is O(n) – for any number of bits (and not O(n*k))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question from Programming Pearls, 2nd edition: Given a sequential file containing 4,300,000,000 32-bit integers,
I am reading book on programming pearls. Question: Given a sequential file that contains
Guys that is code copied from a book (Programming Windows 5th edition): #include <windows.h>
This is code from the book - Programming Interviews Exposed 2nd Edition - Pages
I have a question about a programming problem from the book Cracking The Code
I am following the tutorial from the book Android Programming Tutorials, 3rd Edition. In
Warning: I'm cross-posting from #scala The book Programming in Scala states that path-dependent types
In reading in Chapter 14 of Jon Bentley's Programming Pearls, 2nd Edition, I understand
The following example is from the book 'Programming in Scala'. Given a class 'Rational'
Working through the book Cocoa Programming for Mac OS X (third edition) from Aaron

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.