You’re writing a function in C++ that accepts an array of distinct integers from 0, 1, 2, 3...up to N. However, the array will be missing one integer, and your function is to return the missing one. For example, this array has all the integers from 0 to 6, but is missing the 4: [2, 3, 0, 6, 1, 5] Therefore, the function should return 4. The next example has all the integers from 0 to 9, but is missing the 1: [8, 2, 3, 9, 4, 7, 5, 0, 6] In this case, the function should return 1. Using a nested-loops approach would take up to O(N^2). Your job is to optimize the code so that it has a runtime of O(N).

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 44SA
icon
Related questions
Question

You’re writing a function in C++ that accepts an array of distinct integers from 0, 1, 2, 3...up to N. However, the array will be missing one integer, and your function is to return the missing one.

For example, this array has all the integers from 0 to 6, but is missing the 4:

[2, 3, 0, 6, 1, 5]

Therefore, the function should return 4.

The next example has all the integers from 0 to 9, but is missing the 1:

[8, 2, 3, 9, 4, 7, 5, 0, 6]

In this case, the function should return 1.

Using a nested-loops approach would take up to O(N^2).

Your job is to optimize the code so that it has a runtime of O(N).

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning