Skip to content
    Algoria

    ARRAYS & STRINGS · 9 MIN

    Arrays & Two Pointers

    Arrays store items side by side, so reading any index is instant; two pointers walk them efficiently.

    BYTE explains

    • Access a[i] is O(1); inserting in the middle is O(n) because items shift.
    • Two pointers start at both ends and move inward — great for sorted arrays and palindromes.
    • A sliding window keeps a running range instead of re-adding everything each step.

    Real world: Checking if a name reads the same backwards, like 'level' or 'madam'.

    Code

    def is_pal(s):
        i, j = 0, len(s) - 1
        while i < j:
            if s[i] != s[j]: return False
            i, j = i + 1, j - 1
        return True
    print(is_pal("level"), is_pal("cubix"))
    Output ›
    True False
    Try it in the online compiler →

    Checkpoint quiz

    Reading a[500] in a 1000-item array is…