// may be it not working perfectly :(, but it very fast func (ts *threesum) TriplesCountFast() int { is := sort.Insertion{ts.nums} is.Sort() cnt := 0 for i, vi := range is.A { for j, vj := range is.A[i+1:] { bs := search.Binary{A: is.A[j+1:], X: -(vi + vj)} k := bs.Iterative(0, len(bs.A)-1) if k > j { cnt += 1 } } } return cnt }
func (ts *twosum) CountFast() int { is := sort.Insertion{ts.ids} is.Sort() ts.ids = is.A cnt := 0 for i, v := range ts.ids { bs := search.Binary{A: ts.ids, X: -v} index := bs.Recursive(0, len(bs.A)-1) if index > i { cnt += 1 } } return cnt }