update with O(n) method. to be continued...