Patterns for Subarray

Pattern 1

Given a sequence with NN elements and an integer KK, find (total number of) pairs (i,j)(i, j) such that f(i,j)=Kf(i, j) = K, where i,ji, j are the indices of elements in the sequence and 0i<jN10 \leq i < j \leq N-1.

Two Sum

f(i,j)=Seq[i]+Seq[j]=K f(i, j) = Seq[i] + Seq[j] = K

, where Seq[x]Seq[x] is the element at index xx in the sequence.

Subarray Sum Equals K

f(i,j)=Σx=ijSeq[x]=KΣx=0jSeq[x]Σx=0i1Seq[x]=K \begin{aligned} & f(i, j) = \Sigma_{x = i} ^ {j} Seq[x] = K \\ \Rightarrow & \Sigma_{x = 0} ^ {j} Seq[x] - \Sigma_{x = 0} ^ {i-1} Seq[x] = K \end{aligned}

results matching ""

    No results matching ""