Skip to content
JZLeetCode
Go back

Cheatsheet for Math Knowledge Needed for LeetCode Algorithm Questions (Geometric Series and Arithmetic Progression Sum)

Table of contents

Open Table of contents

Number Series

Geometric Series

A geometric series is a series of terms where each term is a constant multiple (called the common ratio, rr) of the previous one.

General Form

The general form of a geometric series is:

Sn=a+ar+ar2+ar3++arn1S_n = a + ar + ar^2 + ar^3 + ⋯ + ar^{n−1}

Where:

Sum of a Geometric Series

The sum of the first n terms of a geometric series can be calculated using the formula:

Sn=a(1rn)1rS_{n}=\dfrac{a(1-r^{n})}{1-r}

Special Cases:

  1. If r=1:
    • The series is just a repetition of the same term a, so the sum of n terms is: Sn=naS_n = na
  2. For an infinite geometric series:
    • If r<1∣r∣ < 1, the series converges to: Sn=a1rS_{n}=\dfrac{a}{1-r}
    • If r>1∣r∣ > 1, the series does not converge.

Example

  1. Finite Geometric Series: S=1+2+22+23+24=a(125)12=31S = 1 + 2 + 2^2 + 2^3 + 2^4 = \dfrac{a(1-2^5)}{1-2} = 31
  2. Infinite Geometric Series: S=1+12+14+...=1112=2S = 1 + \dfrac{1}{2} + \dfrac{1}{4} + ... = \dfrac{1}{1-\dfrac{1}{2}} = 2

References

Arithmetic Series

The term “等差数列” in Chinese translates to “Arithmetic Sequence” or “Arithmetic Progression” in English.

An Arithmetic Sequence is a sequence of numbers in which the difference between consecutive terms is constant. This difference is referred to as the common difference (d).

General Form

The nthn^{th} term of an arithmetic sequence can be expressed as:

an=a1+(n1)da_n = a_1 + (n-1)\cdot d

Where:

Sum of the First n Terms 等差数列求和

The sum SnS_n of the first n terms of an arithmetic sequence is given by:

Sn=n2(a1+an)S_n = \dfrac{n}{2}\cdot (a_1 + a_n)

Alternatively, using the general formula for the nthn^{th} term, the sum can also be written as:

Sn=n2(2a1+(n1)d)S_n = \dfrac{n}{2}\cdot (2a_1 + (n-1)\cdot d)

Example

For an arithmetic sequence where:

The sequence will be: 2,5,8,11,14.

The sum of the first 5 terms is:

S5=52(2+14)=40S_5 = \dfrac{5}{2}\cdot (2 + 14) = 40

References

Share this post on:

Previous Post
LeetCode 91 Decode Ways and Hacker Rank ASCII Encoded Strings (VMWare, Goldman Sachs, and Salesforce Interview Questions)
Next Post
HackerRank Bitwise Xor Operations