Sorting in Linear Time

Code -> https://ide.codingblocks.com/s/34864
Question ->https://hack.codingblocks.com/contests/c/509/423
working fine in my ide and cb online ide but showing wrong output while submitting for one case !!.

1 Like

In your code
line 4,5,6 are :

int n;
int a[n];
cin>>n;

Instead they should be :

int n;
cin>>n;
int a[n];

Actually you were creating array even before n was entered by the user, hence any random n was taken

Also in your code, line 9 is

if(a[i] > 2 && a[i] < 0){return 0;}

This is completely illogical, as a number can’t be both greater than 2 and less than 0 at the same time.:joy:
And this was not even needed, as when the question says that numbers will be only 1,2and 0, then you don’t need to check it

https://ide.codingblocks.com/s/34952
I have made the required changes in your code.

1 Like

Thanks, bhaiya, actually I was coding from last night 4 hours and so while solving the assignment problems . . . my mind went crazy and I made those silly mistakes.:sweat_smile: