Skip to content

Commit

Permalink
Merge pull request AASF-IIITM#36 from source-aasf/master
Browse files Browse the repository at this point in the history
merge from source-aasf
  • Loading branch information
haritha1313 authored Oct 5, 2018
2 parents f2e8186 + b8b0e8e commit c87e618
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions c++/JNEXT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
int a[n];

for(long long int i=0;i<n;i++)
{
cin>>a[i];
}
long long int k=-1;
vector<int> q;
q.push_back(a[n-1]);
for(long long int i=n-2;i>-1;i--)
{

if(a[i]<a[i+1])
{
q.push_back(a[i]);
k=i;
break;
}
else
{
q.push_back(a[i]);
}
}

if(k==-1)
{
cout<<"-1\n";
}
else
{
for(long long int i=0;i<k;i++)
{
if(i!=k)
{
cout<<a[i];
}
}
sort(q.begin(),q.end());
long long int blah=-1;
for(long long int i=0;i<q.size();i++)
{
if(q[i]>a[k])
{
blah=i;
break;
}
}
cout<<q[blah];
for(long long int i=0;i<q.size();i++)
{
if(i!=blah)
{
cout<<q[i];
}
}
cout<<"\n";
}
}
return 0;
}

0 comments on commit c87e618

Please sign in to comment.