Skip to content

i / j / k [terminology]

Terminology

Usage

  • Use for short-lived indices with no more specific role.
  • i is the primary index.
  • j is a secondary or nested index.
  • k is a third index or a write position when that role is obvious.
  • Prefer a semantic index such as w, r, mid, or idx when it carries meaningful algorithmic behavior.
i = m - 1     # last valid element in nums1
j = n - 1     # last element in nums2
k = m + n - 1 # write position at the end of nums1
while i >= 0 and j >= 0:
    if nums1[i] > nums2[j]:
        nums1[k] = nums1[i]
        i -= 1
    else:
        nums1[k] = nums2[j]
        j -= 1
    k -= 1

See 88. Merge Sorted Array.