Iteration is the repetition of a process in a computer program, usually done with the help of loops. There are two types of loop:
- Counter-controlled loop : Executed a predetermined number of times
- Event-controlled loop : Executed as long as specified condition holds true
Correctness of Iterative code : How would we prove that the iterative code is correct? In computer science, you could prove it formally with a loop invariant which is divided into the following three parts:
- Initialization: It is true before the loop runs.
- Maintenance: it's true before an iteration and remains true before the next iteration.
- Termination: It will terminate in a useful way once it is finished.
Two pointer approach: This is an iterative approach of problem solving which involve two pointers. We can solve several interview problems in array, linked list and string using this approach. Here are two varition of this approach :
- Two-pointer from the same side (Sliding Window).
- Two-pointer from opposite sides