1

Given an array of integers I have to return true if all the elements in the array are different or all the elements are the same. Otherwise, I have to return false.

E.g.

[1, 1, 1, 1] - all the elements are the same, so it is true

[12, 20, 1, 23] - all the elements are different, so it is also true

[12, 20, 1, 1] - we have just two 1, so it is false

The easy thing I was able to come up with is to use a hashmap to map an integer to a number of occurrences of that integer in the array. And then to iterate over the hashmap and check if there is at least one entry whose value is greater than 1 and is not equal to the length of the array, then I return false. Otherwise, I return true after the iteration.

The problem with my solution is that it has a linear space complexity and I feel that it should be possible to somehow solve the problem using a constant space complexity and linear time complexity.

So, could someone help me come up with an O(1) space complexity and O(n) linear complexity with respect to the array size solution, please?

I am working on this problem just for fun.

D.W.
  • 159,275
  • 20
  • 227
  • 470
some1 here
  • 113
  • 5
  • 1
    Related: https://cs.stackexchange.com/q/112166/755, https://cs.stackexchange.com/q/44320/755, https://cs.stackexchange.com/q/108465/755, https://cs.stackexchange.com/q/83346/755 – D.W. May 12 '21 at 22:47

2 Answers2

3

The problem does not have any $O(n)$ time algorithm. Note that it is easy to check in linear time if all the elements in the array are the same or not. However, to check if all the elements are district, the problem is known as Element distinctness problem. And, this problem has an $\Omega(n \log n)$ lower bound complexity (for comparison based models). More technically, your problem can be reduced to element distinctness problem in linear time.

Therefore, the best possible solution here is $O(n \log n)$ time algorithm. For that, you can employ merge sort algorithm using a linked list. It also has an $O(1)$ space complexity.


Note: Since the input is a set of integers, you can employ Radix sort to solve the problem in linear time.

Inuyasha Yagami
  • 6,157
  • 1
  • 11
  • 23
  • 1
    This isn't quite right as stated. The $\Omega(n \log n)$ time lower bound only applies in the algebraic decision tree model, but other algorithms might well be faster. For instance, it is known how to sort integers faster than $\Theta(n \log n)$, by going beyond the comparison-based model, and you can use that for element distinctness. The poster gives a randomized linear-time algorithm and they seem happy with randomized linear-time algorithms; their question seems to be primarily about space complexity. – D.W. May 12 '21 at 21:55
  • @D.W. The hash map's worst-case complexity is $O(n)$. So the posted answer is not actually linear time. – Inuyasha Yagami May 12 '21 at 22:01
  • @D.W. And I agree that the lower bound of $\Theta(n \log n)$ is for comparison-based model. This is generally the assumption in such cases. – Inuyasha Yagami May 12 '21 at 22:02
  • @D.W. If we use a hash map with $O(t)$ space, where $t$ is the value of the maximum integer. In that case, hash map gives $O(n)$ time complexity. – Inuyasha Yagami May 12 '21 at 22:04
  • @D.W. But that was not stated in the algorithm. :/ – Inuyasha Yagami May 12 '21 at 22:07
  • A hashtable has $O(n^2)$ worst-case time to solve the problem, but if you use 2-universal hashing, you can prove that the expected running time is $O(n)$, i.e., average-case $O(n)$ time complexity. – D.W. May 12 '21 at 22:07
  • @D.W. I did not know that. I will check that out. Thanks! – Inuyasha Yagami May 12 '21 at 22:08
  • @D.W. But that is an average over all the hash tables in the family right? – Inuyasha Yagami May 12 '21 at 22:09
  • It is an average over the random coins used by the randomized algorithm, just as in any other randomized algorithm. – D.W. May 12 '21 at 22:19
  • @D.W. I will spend some time understanding what you have explained to me. In that case, I should perhaps delete my answer since I do not know any algorithm with $O(n)$ time complexity and $O(1)$ space complexity, even when it is allowed randomness. Previously, I thought that the OP wants worst-case scenarios. – Inuyasha Yagami May 12 '21 at 22:30
  • I don't think you should delete your answer -- I think it is valuable. I'd just add some caveats to the statements about $\Omega(n \log n)$ running time. – D.W. May 12 '21 at 22:31
  • Thanks,@D.W. for this and in general for doing so much for the community. – Inuyasha Yagami May 12 '21 at 22:35
  • "your problem can be reduced to element distinctness problem" - For your argument, you need the other direction: "the element distinctness problem can be reduced to your problem". – Kelly Bundy Jan 03 '24 at 14:58
-1

I have just started to look into time and space complexity and it is all a mess for me atm, but this particular problem can be solved with converting the list of integers into a set, then comparing lenghts. Would that be linear in time? Sample function:

def same_diff(a_list):
    a_list.sort()
    temp_set = set(a_list)
    if len(temp_set) <= 1 or len(temp_set) == len(a_list):
        return True
    return False

Sorting is not required, but neater I guess?

Thanks :)

  • What do you think is the running time to convert to a set? How do you imagine that is done? Python has to implement it in some way. It is not free. – D.W. Jan 30 '23 at 18:58
  • not required, but neater Sir William would oppose. – greybeard Jan 31 '23 at 15:29
  • well all the mentions I find of converting a list to a set talks in O(N) time complexity - iterating over the list is O(N) and and adding each element to a hash set is O(1) - but as I said I'm really at the start of my studies regarding time/space complexity, so really here to learn from you :) – Zsolt Pal Jan 31 '23 at 19:38