Code not wokring

The question says not to use sets or maps to avoid repetition so I perform the find function which takes O(n) which might be the reason for TLE. Please check my code and help me with the solution.
code-https://ide.codingblocks.com/s/324680

@amanb25
You have to modify your code in such a way, that you don’t end producing any duplicates.
This trick helps in achieving that

if (a[i] != a[j] or i == j) {
		swap(a[i], a[j]);
		permute(a, i + 1, n, s);
	} 

your modified code:

Please mark your code as resolved and rate as well :slight_smile:

I used that trick but the test cases are not being passed. Also why are we not using backtracking in this trick to revert the changes made to the number?
this is my code-https://ide.codingblocks.com/s/325112

i have shared your modified code. it passes all the test cases.Please go through it once


I tried this code but its not accepting it though it is giving the right answer when i try it on my ide.

please check the code :slight_smile:

@amanb25
Consider a case:
4
1 2 2 1

Your output contains duplicates and also it is not in lexicographic order. try to dry run for this

For this test case:
10
100008 100004 100004 100009 100000 100000 100009 100007 100004 100004

Your code is giving TLE.

Please try to go through the code .I corrected the mistakes and marked them with comments

can you please provide the link to the code, I cannot see any edits in my code

here

1 Like

please mark your doubt as resolved and rate as well :slight_smile:

I understood the code and got what was wrong. But I’ve one question when we did this for a string cab suppose in the video it showed we should do backtracking so that the main string remains unchanged, but in this that causes error.