I am new to competitive programming, I am doing a question in that i am getting TLE as i am not finding the duplicate values in an array in O(n), with O(1) extra space.
I want to have the worst time complexity as O(n),
I came across an approach given on geeks for geeks:
def printRepeating(arr, size):
print("The repeating elements are: ")
for i in range(0, size):
if arr[abs(arr[i])] >= 0:
arr[abs(arr[i])] = -arr[abs(arr[i])]
else:
print (abs(arr[i]), end = " ")
But what if an element in the array is greater than the size of the array? Something like [10, 20, 10], isn't it an index out of bounds error? How can modify it to get result for all the conditions?