Wrong test case1

For input: 4 701 319 695 52
Expected output coming up is: 701 695 52 319
But the correct answer should be 701 52 319 695, because of this my solution is giving wrong answer.

Had the same problem then later found that the hint in the solution is wrong. You don’t have to swap the smaller element in the suffix.
You have to swap the smaller element that is greater than the pivot value.

The original next_permutation function in c++ standard library will also give the same output.
there is nothing wrong with test cases.
smaller element must be greater than the pivot value.
try this:
int main() {
vector < int> v;
v.push_back(701); v.push_back(319); v.push_back(695); v.push_back(52);
next_permutation(v.begin(),v.end());
for(auto i:v)cout<<i<<" ";
}
output:701 695 52 319

Click here to start solving coding interview questions