If there is a random array say arr = [3, 5, 1, 4, 3] and N = 5 which indicates the size of the given array, is there a way to find the first repetitive value in the array (here the answer is 3) within O(N) time complexity but without using any data structure as a dictionary, map, tree, etc.. But you can use a variable.
The idea is to have an optimal space complexity.
I was asked this question in an interview.
Generally, this is solved by using a dictionary and keep the traversed item of the array as a key and value as a count. When we reach the count of more than 2 then we have a solution. But if we are not going to use the data structure, then we have to have loop within a loop to look up to the next items.
I also tried to think of a solution by using just one variable, but a variable will not be enough.
I think it is quite impossible to get the solution in O(N). However, I could be wrong. Please help me find a solution to this.
EDITED
My apologies for not mentioning this before. The numbers with in the array will able be from 1 <= N, i.e., 1 <= arr[i] <= N