So, yesterday my teacher gave us a question to try. The link for the question is
Rearrange an array such that arr[i] = i - GeeksforGeeks[
^]
He just told the problem and the input and told us to try. I came up with a solution and showed it to him.
But, he said that my code is bad and not optimised. After some time, he showed the code (first one) that is given in the website in a projector and said it is the correct solution.
Basically, my code and the code in the website works.
But I need to know why he said that my code is not optimised and bad. How can I improve myself to write good code.
What I have tried:
#include<stdio.h>
int main()
{
int a[] = {-1, -1, 6, 1, 9, 3, 2, -1, 4, -1},n,i,x,y;
n= sizeof(a)/sizeof(a[0]);
for(i=0;i<n;i++){
if(a[i] == i || a[i] == -1)
continue;
else if(a[i] != -1){
x=a[i];
y=a[x];
a[i] = y;
a[x] = x;
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
~
~
~
~