Common Mistakes

Common errors to avoid

  • Off-by-one in range queries: prefix[j] - prefix[i] gives sum from index i+1 to j inclusive.

Draw out indices carefully.

  • Forgetting prefix[0] = 0 for hash map approach: Initialize hash map with {0: -1} or {0: 1} to handle subarrays starting from index 0.
  • Integer overflow in prefix sums: For large arrays with large values, use long integers to avoid overflow.
  • Not handling zero correctly: In product variants, zeros require special handling since they zero out products.