in reply to add missing elements and remove duplicates....and some more questions.

check each number with the preceding one
This seems to imply a sort-based approach was expected. But sort has the time complexity O(n log n) whereas your linear scan is O(n).

It's not uncommon to have TA-s generate teaching problems from random solutions, inadvertently ending up with a few non-solvable ones in the mix. And then, some teachers take it poorly when a student outsmarts them.

  • Comment on Re: add missing elements and remove duplicates....and some more questions.

Replies are listed 'Best First'.
Re^2: add missing elements and remove duplicates....and some more questions.
by pritesh_ugrankar (Monk) on Apr 23, 2017 at 12:12 UTC

    Hi,

    Thanks for the encouraging comments. However. I was not thinking in those very high level complex terms. I just tried to keep it simple. I have not studied algorithms. May be in future I will be able to write this using a cool complex algorithm. Here's how I thought about it.

    Before writing this script I thought its better to keep it simple so that if I get a large array, then checking an element, say x with the next and previous could be computationally intensive. I will Google about the log n stuff and try to understand it.

    Thinkpad T430 with Ubuntu 16.04.2 running perl 5.24.1 thanks to plenv!!

      This is the "big O notation". You can read about it on wikipedia: analysis of algorithms. But don't get sidetracked too much with the "dry stuff". Maybe ask for the next problem, or another variant, because you appear to have conquered this one already.