Get a free weekly practice problem!

Keep that axe sharp.

× No thanks

Just No more free questions left!

Upgrade Now

I have an array of n + 1 numbers. Every number in the range 1..n appears once except for one number that appears twice.

Write a method for finding the number that appears twice.

We can do this with additional memory.

You must log in with one click to view the rest.

Once you're logged in, you'll get free full access to this and 4 other questions.

You must log in with one click to view the rest.

Once you're logged in, you'll get free full access to this and 4 other questions.

time. We can sum all the numbers 1..n in time using the fancy formula, but it still takes time to sum all the numbers in our input array.

additional space. The only additional vars we need to allocate are numbers.

If our array contains huge numbers or is really long, our sum might be so big it causes an integer overflow. What are some ways to protect against this?

What's next?

Powered by qualified.io

. . .