51 Check power of 2 5s

Powers of 2 have exactly one set bit. This bit trick is O(1) and appears in many bitmask problems.

n > 0 and (n & (n-1)) == 0
52 Get ith bit 5s

Bit extraction isolates a single bit. Building block for bitmask enumeration.

(n >> i) & 1
53 Set ith bit 5s

Setting a bit turns on a flag. Used in bitmask DP and subset representation.

n | (1 << i)
54 Toggle ith bit 5s

XOR toggle flips a single bit. Core operation for Hamming distance and bit manipulation.

n ^ (1 << i)
55 Clear lowest set bit 5s

Clearing the lowest set bit is Brian Kernighan's trick for efficient bit counting.

n & (n - 1)
56 Count set bits (Brian Kernighan) 10s

Counting set bits via n &= n-1 runs in O(k) where k is the number of set bits.

count = 0
while n:
    n &= n - 1
    count += 1
73 Sort with key function 5s

Custom key sorting is Python's primary sort mechanism. Foundation for interval and custom-order problems.

a.sort(key=lambda x: x[1])
74 Sort by multiple keys 10s

Multi-key sorting handles tiebreakers. Used in leaderboard and event scheduling.

a.sort(key=lambda x: (x[0], -x[1]))
75 Counting sort 15s

Counting sort is O(n+k) for bounded integers. Optimal when the range is small.

count = [0] * (max(a) + 1)
for x in a:
    count[x] += 1
result = []
for val, cnt in enumerate(count):
    result.extend([val] * cnt)
76 Merge two sorted lists 15s

Merging sorted lists is the core of merge sort and merge-k-lists.

i = j = 0
merged = []
while i < len(a) and j < len(b):
    if a[i] <= b[j]:
        merged.append(a[i]); i += 1
    else:
        merged.append(b[j]); j += 1
merged.extend(a[i:])
merged.extend(b[j:])
77 Partition step (quicksort) 15s

Partition is the heart of quicksort. Same as Lomuto partition — drill until automatic.

def partition(a, lo, hi):
    pivot = a[hi]
    i = lo
    for j in range(lo, hi):
        if a[j] <= pivot:
            a[i], a[j] = a[j], a[i]
            i += 1
    a[i], a[hi] = a[hi], a[i]
    return i
78 GCD (Euclidean) 10s

Euclidean GCD runs in O(log min(a,b)). Foundation for fraction simplification and LCM.

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
79 Check prime 10s

Trial division up to sqrt(n) is the basic primality test. Used in number theory problems.

def is_prime(n):
    if n < 2: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False
    return True
80 Fibonacci iterative 10s

Iterative Fibonacci is O(n) time, O(1) space. Classic DP introduction pattern.

a, b = 0, 1
for _ in range(n):
    a, b = b, a + b
# a is fib(n)
81 Power (fast exponentiation) 15s

Binary exponentiation computes x^n in O(log n). Essential for modular arithmetic problems.

res = 1
while exp:
    if exp & 1:
        res *= base
    base *= base
    exp >>= 1
82 Factorial iterative 10s

Iterative factorial avoids recursion overhead. Used in combinatorics and permutation counting.

res = 1
for i in range(2, n + 1):
    res *= i
83 Modular arithmetic 10s

Modular arithmetic prevents overflow in combinatorics. pow(a,b,m) is built-in fast mod-exp.

(a * b) % m
pow(a, b, m)  # fast modular exponentiation
142 Quick select kth largest 10s

Quickselect partitions then recurses into one side. O(n) average for kth element.

def qs(l, r, k):
    p = partition(l, r)
    if p == k: return a[p]
    return qs(p+1, r, k) if p < k else qs(l, p-1, k)
143 Task scheduler greedy 10s

Greedy formula: max count drives idle slots. Total time = max(n_tasks, (max_freq-1)*(n+1) + max_count).

freq = Counter(tasks)
mx = max(freq.values())
mx_cnt = sum(1 for v in freq.values() if v == mx)
return max(len(tasks), (mx - 1) * (n + 1) + mx_cnt)