Implemented O(n) algorithm also