![]() |
VOOZH | about |
Given a sorted array of n elements containing elements in range from 1 to n-1 i.e. one element occurs twice, the task is to find the repeating element in an array.
Examples :
Input : arr[] = { 1, 2 , 3 , 4 , 4}
Output : 4
Input : arr[] = { 1 , 1 , 2 , 3 , 4}
Output : 1
Brute Force:
Below is the implementation of the above approach:
4
Time Complexity: O(N^2)
Auxiliary Space: O(1)
An efficient method is to use Floyd’s Tortoise and Hare algorithm.
3
Time Complexity : O(log n)
Space Complexity: O(1)