c - Cant get the logic of this code -
how code work input : 20051996
program: delete duplication elements in array
for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if (a[i]==a[j]) { for(k=j;k<n;k++) { a[k]=a[k+1]; } n--; j--; } } }
the logic pretty simple here element not deleted being exchanged.
for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if (a[i]==a[j])
this finding element equal a [i]
. number there loop targeting repeated number , cell fed/replaced next cell value. 1 value has been changed remaining cell value swapped next one.
for(k=j;k<n;k++) { a[k]=a[k+1]; } n--; j--;
and last cell remains , number n , j decreased not refer cell again. removes element makes new element have put counter number of repetition , have slice array or break make work.
Comments
Post a Comment