Tricks for DSAlgo problems under 5 mins.

January 23, 2024

For newcomers to competitive programming, recalling trivial details can consume valuable seconds. To optimize your C++ programming experience, I've compiled a set of tips and tricks for occasional reference:

  1. Use S.push() for characters and S.append() for strings.
  2. Always pass variables by reference to enhance efficiency.
  3. When utilizing log2(), ensure you place float/double data types inside it.
  4. Keep in mind the power of binary searching for answers.
  5. Avoid relying solely on memory; think logically about problem-solving.
  6. Prioritize time complexity; seek the most efficient solution, especially when it significantly impacts the outcome.
  7. For uncertain approaches, delve deeper into problem-solving or explore alternative methods. Complex solutions for C/D often appear straightforward once discovered.
  8. Exercise caution with logical expressions, especially when applying them to numbers.
  9. Consider using a set as an ideal data structure for fast addition, removal, and finding the next greatest element.
  10. Remember the XOR and sum trick: a+b=ab+2×(a&b).
  11. Utilize n=n&(n−1) to convert the rightmost 1 to 0, particularly useful for counting the number of 1s.
  12. n>>1 shifts all bits to the right.
  13. n<<1 shifts all bits to the left.

These guidelines aim to streamline your coding experience, helping you save time and enhance your problem-solving skills.