DEV Community

Aleksei Berezkin
Aleksei Berezkin

Posted on • Updated on

Algorithms exercise: “find removed sequence item” and more

You have an array with integers sequence from 1 to 1000:

[1, 2, 3, 4, 5, ..., 1000]
Enter fullscreen mode Exit fullscreen mode

1. Easy

An item with value x was removed, so the array became like:

[1, 2, 4, 5, ..., 1000]
Enter fullscreen mode Exit fullscreen mode

Then the array was shuffled. How to find x in O(n) time and O(1) memory?

2. Harder

An item with value x was replaced with x - 1 so the array became like:

[1, 2, 2, 4, 5, ..., 1000]
Enter fullscreen mode Exit fullscreen mode

Then the array was shuffled. Again, you need to find x in O(n) time and O(1) memory.

3. Hard

And item with value x was replaced with y where y is any integer from 1 to 1000, so the array became like

[1, 2, 9, 4, 5, ..., 1000]
Enter fullscreen mode Exit fullscreen mode

or

[872, 2, 3, 4, 5, ..., 1000]
Enter fullscreen mode Exit fullscreen mode

or even

[1, 2, 3, 4, 5, ..., 999, 214]
Enter fullscreen mode Exit fullscreen mode

As usually, the array was shuffled, and your task is to find x and y in O(n) time and O(1) memory.

Happy coding!

Answers will be published soon.

Top comments (0)