fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define nl cout<<endl
  7. #define pb push_back
  8. #define pob pop_back
  9. #define pf push_front
  10. #define pof pop_front
  11. #define mp make_pair
  12. #define ff first
  13. #define ss second
  14. #define PI 3.1415926535897932384626
  15. #define mod 1000000007
  16. #define all(x) x.begin( ),x.end( )
  17. void ss(set<int>st)
  18. {
  19. set<int>::iterator itr = st.begin( );
  20. for(;itr != st.end( );itr++){
  21. cout<<*itr<<" ";
  22. }
  23. nl;
  24. }
  25.  
  26. void sl(list<int>st)
  27. {
  28. list<int>::iterator itr = st.begin( );
  29. for(;itr!=st.end( );itr++){
  30. cout<<*itr<<" ";
  31. }
  32. nl;
  33. }
  34.  
  35. void sv(vector<int>v)
  36. {
  37. vector<int>::iterator itr = v.begin( );
  38. for(;itr != v.end( );itr++){
  39. cout<<*itr<<" ";
  40. }
  41. nl;
  42. }
  43.  
  44. void solve( )
  45. {
  46. int cnt = 0;
  47. int n;
  48. cin >> n;
  49. int a[n];
  50. int i;
  51. for ( i = 0; i < n;i++)
  52. cin >> a[i];
  53. for ( i = n - 2; i >= 0;i--){
  54. if(a[i]>=a[i+1])
  55. continue;
  56. else if(a[i]<a[i+1]){
  57. swap(a[i], a[i + 1]);
  58. break;
  59. }
  60. }
  61. if(i==-1)
  62. cout << "-1" << endl;
  63. else{
  64. sort(a + i + 1, a + n);
  65. for (i = 0; i < n;i++)cout << a[i];
  66. }
  67. cout<<endl;
  68. }
  69.  
  70. int main( )
  71. {
  72. //Shahariar_Amin;
  73. //RUET,CSE-19;
  74.  
  75. ios_base::sync_with_stdio(0);
  76. cin.tie(0);
  77. cout.tie(0);
  78.  
  79. //freopen("input.txt","r",stdin);
  80. int t=1;
  81. cin>>t;
  82. while(t--)
  83. {
  84. solve( );
  85. }
  86. return 0;
  87. }
Success #stdin #stdout 0s 4300KB
stdin
Standard input is empty
stdout