Getting MLE while every constraint has been taken care of

My logic is correct I tried my own testcases the code is working, still unable to pass testcases due to MLE. And I hope this question is of STL and dynamic programming bcz array needs to be returned .

#include
#include
using namespace std ;

int *next_perm(int num)
{
int *arr = new int [num] ; // array made in heap memory
for (int i = 0; i < num; i++)
{
cin>>arr[i] ;
}
next_permutation(arr, arr+num) ; // permutation done in the array
if(arr[0] > arr[1])
{
sort(arr,arr+num) ; // according to the logic asked in question
return arr ;
}
return arr ; // returning address of array present in heap memory
}

int main ()
{
int test_case, num, b[100000] = {0}, x = 0, y, temp ;
int val ; // to store address of array present in heap memory
cin>>test_case ;
temp = test_case ;
while(test_case > 0)
{
cin>>num ; // total values which should be in array
if(temp==test_case)
{
y = num ;
}
val = next_perm(num) ; // function call
for(int i = x; i < y; i++)
{
b[i] = val[i-x] ; // storing values in temp array
}
delete [] val ; // clearing heap
test_case-- ;
x = y ;
y = y + num ;
}
for (int i = 0; i < num
temp; i++) // actual problem is in displaying
{
if(i % num==0 and i>1)
{
cout<<endl ;
}
cout<<b[i]<<" " ;

}

return 0 ;

}

Link for ide : “https://ide.codingblocks.com/s/673068

hi @kartikey.rajput0071 refer