Patterns for Subarray
Pattern 1
Given a sequence with N elements and an integer K,
find (total number of) pairs (i,j) such that f(i,j)=K,
where i,j are the indices of elements in the sequence and
0≤i<j≤N−1.
f(i,j)=Seq[i]+Seq[j]=K
, where Seq[x] is the element at index x in the sequence.
⇒f(i,j)=Σx=ijSeq[x]=KΣx=0jSeq[x]−Σx=0i−1Seq[x]=K