Data Structures & Algorithms · Revision notes

Greedy Algorithms & Bit Manipulation

Greedy algorithms make the locally best choice at each step and are correct when the greedy-choice property and optimal substructure hold (activity selection, intervals, Huffman). Bit manipulation uses bitwise operators for O(1) tricks such as XOR to find unique elements and masks to represent subsets.

Quick revision

  • Greedy needs greedy-choice + optimal substructure; prove by exchange or find counter-example
  • Activity selection: sort by finish, take start >= last finish; merge intervals: sort by start
  • Weighted intervals, 0/1 knapsack, arbitrary coins: DP not greedy
  • x&(x-1) clears lowest set bit; x&-x isolates it; power of two: x>0 and x&(x-1)==0
  • XOR: a^a=0, a^0=a; single number, missing number; subsets via masks 0..2^n-1
Read the full Greedy Algorithms & Bit Manipulation notes on Doubtclip →