Reduced the runtime from O(n log(n)) to O(n)
Merge pull request #1 from tarasbob/master

Reduced the runtime from O(n log(n)) to O(n)