The following code returns the start and end indices, as well as the sum:
import scala.math.Numeric.Implicits.infixNumericOps import scala.math.Ordering.Implicits.infixOrderingOps case class Sub[T: Numeric](start: Index, end: Index, sum: T) def maxSubSeq[T](arr: collection.IndexedSeq[T])(implicit n: Numeric[T]) = arr .view .zipWithIndex .scanLeft(Sub(-1, -1, n.zero)) { case (p, (x, i)) if p.sum > n.zero => Sub(p.start, i, p.sum + x) case (_, (x, i)) => Sub(i, i, x) } .drop(1) .maxByOption(_.sum)
Nader ghanbari
source share