Only 1 testcase passed, what is wrong in this code

#include<bits/stdc++.h>
using namespace std;
#define MAX 1000000000

int* hash_array(int A[], int n)
{
int* hash = new int[MAX]; //MAX is the maximum possible value of A[i]
for(int i=0;i<=MAX;i++) hash[i]=-1; //initialize hash to -1.
int count = 0;
for(int i=0;i<n;i++) // Loop through elements of array
{
if(hash[A[i]] == -1) // A[i] was not assigned any hash before
{
hash[A[i]] = count; // Assigning hash to A[i]
count++;
continue;
}
for(int j = 0;j<i;j++)
{
if(hash[A[j]] > hash[A[i]]) // All the hashes greater than previous hash of A[i] are decremented.
hash[A[j]]–;
}
hash[A[i]] = count - 1; // Assigning a new hash to A[i]
}
return hash;
}
int main() {
int n;
map <int, int> mp;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int *hash = hash_array(a,n);
for(int i=0;i<=MAX;i++)
{
if(hash[i] != -1)
{
int x = hash[i];
//cout<<x;
//mp.insert(pair<int,int>(x,i));
mp[x] = i;
}
}
for(auto i=mp.begin(); i!= mp.end(); i++)
{
cout<second<<endl;
}
return 0;
}

This is causing TLE for few testcases.

You can build a map or array inside the function to store values at there respective indexes while building the hash rather doing it later.

Let me know if it doesn’t work

I am marking this doubt as resolved as you have not replied to this thread for 5 days.
In case, you still have some doubts regarding the same problem, you can reopen this doubt.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.