Getting error of time limit

#include
using namespace std;

int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int max=0;
for(int i=0;i<n;i++)
{
if(arr[i]>max)
max=arr[i];
}
int freq[max]={0};
for(int i=0;i<n;i++)
{

	freq[arr[i]]=freq[arr[i]]+1;
    
}


 for(int i=0;i<n;i++)
 {
 	for(int j=0;j<=max;j++)
 	{
 		if(freq[j]!=0)
        {
     		arr[i]=j;
             freq[j]--;
     		break;
        }
 	}	
 }

 for(int i=0;i<n;i++)
 {
 	cout<<arr[i]<<" ";
 }

return 0;

}

There are two issues with your code:

  1. you are assigning j to same index i in the inner for loop.
    logic implemented is wrong and causing TLE.
    SOLUTION:
    for(int j=0;j<=max;j++)
    {
    while(freq[j]!=0)
    {
    arr[i++]=j;
    freq[j]–;
    }
    }

2.freq array contains one less index/element, make it freq[max+1]

Hope, this would help.
Give a like, if you are satisfied.

Mark it as resolved if your problem is solved.