Monk and Sorting Algorithm[HackerEarth]

Hello guys can you help me reduce the time complexity of my code for the problem Monk and Sorting Algorithm: https://www.hackerearth.com/practice/codemonk/

I tried to optimize my code as much as possible and now I have no idea to further reduce the time complexity.

**#include<bits/stdc++.h>**

** #define ll long long int**

** using namespace std;**

** int counter;**

** bool anyNonZero = false;**

** ll powerOf10[19];**

** bool compare(const int& a, const int& b){**

** ll wa = a;**

** wa/=powerOf10[min(18, 5*(counter-1))];**

** wa%=powerOf10[min(18, 5counter - 5(counter-1))];**

** ll wb = b;**

** wb/=powerOf10[min(18, 5*(counter-1))];**

** wb%=powerOf10[min(18, 5counter - 5(counter-1))];**

** if(wa!=0 || wb!=0) anyNonZero=true;**

** return wa<wb;**

** }**

** void print(vector& num){**

** for(int i=0;i<num.size();i++){**

** cout << num[i] << " ";**

** }**

** cout << endl;**

** }**

** int main(){**

** int n;**

** cin >> n;**

** powerOf10[0]=1;**

** for(int i=1;i<=18;i++) powerOf10[i]=powerOf10[i-1]10;*

** vector num(n);**

** for(int i=0;i<n;i++) cin >> num[i];**

** counter = 1;**

** while(true){**

** anyNonZero = false;**

** stable_sort(num.begin(), num.end(), compare);**

** if(!anyNonZero) break;**

** print(num);**

** counter++;**

** }**

** return 0;**

** }**

I did nothing fancy, I kept finding the weight and then sorting the vector using the stable_sort function and then printing my result.

My time complexity as I think should be O(5*(time taken by stable sort)) which is when I googled to be O(5nlog^2n).

What can I do? Please help me.