modifying earlier algorithm to handle positive array as well