Please read the new rule regarding the restriction on the use of AI tools. ×

minasedx's blog

By minasedx, 14 months ago, In English

A. Good Pairs


Tutorial
Solution

B. Advantage


Tutorial
Solution

C. Planets


Tutorial
Solution

D. Two Arrays


Tutorial
Solution

E. Gravity Flip


Tutorial
Solution
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
14 months ago, # |
  Vote: I like it 0 Vote: I do not like it

why does the time complexity of sorting in problem E is just $$$O(n)$$$, isn't it $$$O(n log n)$$$?

  • »
    »
    14 months ago, # ^ |
    Rev. 5   Vote: I like it 0 Vote: I do not like it

    Disclaimer: I do not have permission to view the problem statement, but based on the wording of the editorial, I believe that this is what is happening.

    Normal sorting (for example merge sort) is $$$O(n \log n)$$$, but we can sort an array of $$$n$$$ integers, each having a value between $$$0$$$ and $$$n$$$ in $$$O(n)$$$ time using counting sort.

    The idea is to count how many times each element appears in the array using a separate array (usually called the frequency array). After that, we can easily retrieve the sorted array.

    It might be difficult to understand my explanation, but I hope the code should be clear:

    Code