Counting Sort Problem Statement

Ninja is learning about sorting algorithms, specifically those that do not rely on comparisons. Can you help Ninja implement the counting sort algorithm?

Example:

Input:
ARR = {-2, 1, 2, -1, 0}
Output:
{-2, -1, 0, 1, 2}

Explanation:

The array is sorted in increasing order using counting sort.

Input:

The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains an integer ‘N’ representing the length of the ‘ARR’ array.
The second line of each test case contains ‘N’ space-separated integers representing the ‘ARR’ array.

Output:

For each test case, print the sorted array.
The output of each test case will be printed in a separate line.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 5000
  • -10^4 <= ARR[i] <= 10^4
  • Time limit: 1 sec

Note:

You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
AnswerBot
4d

Implement counting sort algorithm to sort an array of integers without comparisons.

  • Count the frequency of each element in the input array.

  • Calculate the prefix sum of frequencies to determine the posit...read more

Help your peers!
Add answer anonymously...
PayPal Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter