v2 time O(n) space O(1) Accepted

case 6 when A.length == 0 was not considered in v1