any tip on dealing with indexing

Hi LC community,

I have trouble with figuring out the correct indexing. For example, I want to sum the last K numbers starting from index i or I want to count the number of elements between index i and j, or count the number of sliding windows of width K. Without a careful thought, my index might be off by 1.

I have found that I usually spend like 3-10 minutes just to figure out the correct indexing. This struggle has occurred often when I worked on some DP problems.

Is there any tip or suggestion on how to deal with the indexing?

My bread-and-butter formula is:

  • the number elements between index i and j inclusively is j - i + 1.
  • the number of sliding window of width K in an array of length L is L - K + 1

Is there anything else you can share?
Thank you!

Comments (1)