Hey, I’m gettting WA in https://www.spoj.com/problems/JNEXT/ problem.
Here is my code:
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
long long n,flag=0;
cin>>n;
vector <long long> a(n);
for(long long i=0;i<n;i++)
cin>>a[i];
for(long long i=n-1;i>0;i--)
{
if(a[i]>a[i-1])
{
flag=1;
long long temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
sort(a.begin()+i,a.end());
break;
}
}
if(flag)
{
long long num=0;
for(int i=0;i<n;i++)
num=num*10+a[i];
cout<<num;
}
else
cout<<"-1";
cout<<"\n";
}
return 0;
}