
You are tasked with finding the middle element of an array by implementing a specific traversal algorithm known as Floyd's Cycle-Finding Algorithm, commonly called the "Tortoise and Hare" or two-pointer approach.
The algorithm must be implemented as follows:
Your function should return the value of the element at the slow pointer's final position.
The first line contains a single integer N, the size of the array.
The second line contains N space-separated integers, representing the elements of the array.
Print a single integer, which is the value of the middle element found by the algorithm.
While the middle element of an array can be found in O(1) time with arr[n/2], the purpose of this problem is to practice the specific two-pointer "Tortoise and Hare" traversal technique.
5
1 2 3 4 5
3
Start: slow is at index 0 (value 1), fast is at index 0 (value 1).
Step 1: slow moves to index 1 (value 2), fast moves to index 2 (value 3).
Step 2: slow moves to index 2 (value 3), fast moves to index 4 (value 5).
The fast pointer cannot move two steps further. The loop terminates. The slow pointer is at index 2, so the result is 3.
6
10 20 30 40 50 60
30
Start: slow at index 0, fast at index 0.
Step 1: slow moves to index 1, fast moves to index 2.
Step 2: slow moves to index 2, fast moves to index 4.
The fast pointer cannot move two steps further. The loop terminates. The slow pointer is at index 2, so the result is 30 (the first of the two middle elements).
The expected time complexity is O(N).
1 <= N <= 10^5
-10^9 <= arr[i] <= 10^9
Time limit: 1 sec