I have an array of integers, and I need an O(n) algorithm to find if the array contains a number and its square; one pair is sufficient.
I tried to do it myself, but I have only managed to find a solution in O(n2).
I thought about using counting sort, but the memory usage is too big.
create a new array twice the length of the input array. O(2N)
copy all of the numbers in O(N)
copy the squares of the numbers in O(N)
radix sort (we can since they are all ints) O(N)
iterate over once to see if there are two numbers the same one after the other O(N)
profit! O(1)