Counting Sort

QUESTION

You have been given an integer array A of size N. Each element of the array ranges between 1 and 10^5. You need to find the frequency of each distinct element of the array. The elements need to be present in the output in ascending order. You need to print the value and then frequency of each distinct element.\n\nInput Format:\n\nThe first line contains a single integer N denoting the size of the array. The next line contains N space separated integers, denoting the elements of the array.\n\nOutput Format\n\nFor each distinct integer, print its value and then frequency in a new line. The distinct integers should appear in the output in ascending order.\n\nConstraints\n\n1<=N<=100\n1<=A[i]<=100.

“TESTCASE_1”: “5\n5 4 3 2 1\n###—###SEPERATOR—###—\n1 1\n2 1\n3 1\n4 1\n5 1”, “TESTCASE_2”: “10\n5 6 21 75 5 6 2 21 21 21\n###—###SEPERATOR—###—\n2 1\n5 2\n6 2\n21 4\n75 1”, “TESTCASE_3”: “100\n80 42 99 34 54 72 78 53 1 19 28 84 3 56 64 17 86 82 17 16 2 77 83 97 30 8 35 85 40 93 54 53 3 38 19 81 74 19 6 98 5 60 72 10 32 71 40 68 1 39 11 66 68 3 88 88 87 30 34 78 74 98 47 70 13 55 82 19 43 17 96 98 63 27 95 9 13 20 47 16 95 55 29 86 44 42 67 62 100 2 17 32 99 30 75 92 43 77 39 3\n###—###SEPERATOR—###—\n1 2\n2 2\n3 4\n5 1\n6 1\n8 1\n9 1\n10 1\n11 1\n13 2\n16 2\n17 4\n19 4\n20 1\n27 1\n28 1\n29 1\n30 3\n32 2\n34 2\n35 1\n38 1\n39 2\n40 2\n42 2\n43 2\n44 1\n47 2\n53 2\n54 2\n55 2\n56 1\n60 1\n62 1\n63 1\n64 1\n66 1\n67 1\n68 2\n70 1\n71 1\n72 2\n74 2\n75 1\n77 2\n78 2\n80 1\n81 1\n82 2\n83 1\n84 1\n85 1\n86 2\n87 1\n88 2\n92 1\n93 1\n95 2\n96 1\n97 1\n98 3\n99 2\n100 1”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int c[100001]={0};
  int n,i,m;
  cin>>n;
  for(i=0;i<n;i++)
  {
    cin>>m;
    c[m]++;
  }
  for(i=0;i<100001;i++)
  {
    if(c[i]>0)
    {
      cout<<i<<" "<<c[i]<<"\n";
    }
  }
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.