PROBLEM LINK:
Author: Kunal Demla
Editorialist: Shivansh Kapoor
DIFFICULTY:
EASY
PREREQUISITES:
Arrays
PROBLEM:
Given an array Arr with N meals, namely, Breakfast, Lunch and Dinner. Sort them such that meals of the same time are adjacent.
QUICK EXPLANATION:
Use two arrays, one taking input and other storing the index count and then print indexes.
EXPLANATION:
We use two arrays a and b, a takes input from the user and we use array b to increase the count of the numbers that are being taken by the array a, using a for loop we then print out the value of index the number of time it has been stored in the array b.
SOLUTIONS:
Setter's Solution
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
void solve()
{
ll i,j,n;
cin>>n;
vector<int> a(n);
ll b[5]={0};
for(i=0;i<n;i++){
cin>>a[i];
b[a[i]]++;
}
for(i=0;i<3;i++)
{
for(j=0;j<b[i];j++)
cout<<i<<" ";
}
cout<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
Editorialist's Solution
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t;
t=sc.nextInt();
while(t-->0)
{
int n;
n=sc.nextInt();
int a[]=new int[n];
int b[]=new int[5];
int i;
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
b[a[i]]++;
}
for(i=0;i<4;i++)
{
for(int j=0;j<b[i];j++)
System.out.print(i+" ");
}
System.out.println();
}
}
}