ASP - Editorial

My java submission not getting aced.Its giving an NZEC runtime error.The same happened with my python submission.I guess there are unnecessary spaces in input.

JAVA SOLUTION : link

PYTHON SOLUTION : link

But the same idea in C++ getting aced.

C++ SOLUTION : link

Easy Solution…Don’t know why it passed!! soln
Are test cases weak?

1 Like

THE AUTHOR’S SOLUTION IS WRONG.

It fails for the test case 3 1 2.

According to Author’s solution, after the swaps the elements become 1 2 3, which is in sorted order and prints YES, but the number 3 is 2 places away from its position.

8 Likes

Test case are weak
people passed with code like insertion sort with only one swap(which is incorrect)

Running insertion sort with checks
for more than one swap worked too.

Insertion sort runs in almost O(n) for
partially sorted arrays and if you
break when the array is not partially
sorted, the entire code is around O(n)

CodeChef: Practical coding for everyone

These kind of code fails with input like:
5
5 1 2 3 4
or
5
1 4 2 3 5
etc
@admin look into this

1 Like

why my program is not getting accepted its getting tle
can anyone help me through this?
https://www.codechef.com/viewsolution/8591937

@sashi jey, ans for 1 5 8 is yes but yours soln will show no.
For Tle, use scanf printf and “\n”…

https://www.codechef.com/viewsolution/8603936

This is my solution.Please help , I am getting wrong answer.

Also I saw a correct solution , with a condition [ if(A[i-2]>A[i]) >>>> cout<<“NO”; Flag=1;break;] .

Cant understand why it works , since it gives 4 1 5 2 as Yes where as 4 and 2 are clearly off by 2 positions.
Thanks for help]

@vaibhav1224

Many other solutions are bugged. So dont’ trust them :slight_smile:

Your code fails for the following case:

1

5

1 0 4 2 5

This is funny. I had never seen a problem with so many incorrect solutions being accepted…

CodeChef: Practical coding for everyone.

Getting WA,
Unable to identify the error. Can someone help?

@ash24jal

The idea with your algo is ok, but you have a problem with your iteration.
When you break your iteration on “i”, you don’t read the remaining values. So next time you iterate on “t” you read incorrect values.

Try for instance with the following input.

2

5

10 100 11 12 13

5

1 2 3 5 4

@beroul

thank you so much for identifying that silly mistake…

@beroul You provided cases which includes test cases as this-

1

5

1 0 4 2 5

But as mentioned in constraints 1 <= a[i] <= 10^9
. 0 cannot be a valid input for a[i].

Try this one
https://code.hackerearth.com/4042b6r?key=1f1cb8bcc910e53476316147dba318ea

CodeChef: Practical coding for everyone where my code failing …

Check the way you read the data…

See here an example where your code fails:

Sorry. Your code is wrong.
It fails with:

1

6

1 10 2 11 3 14

I don’t check why you get TLE, but your algo is wrong. You check the values against {1,2,3…,n}, but it is not said that the Ai are a permutation of {1,…n}.

@beroul give me some test cases where i am failing and why my algo is wrong

@shashi jey
Your solution works only if the numbers are from 1 to N.
But the input can contain any numbers.
Ex: N = 4 and A[]={1 3 8 6}.