this is a complex invariant, and the loop is incrementing an index. recommend changing this to a forloop with if-statements