this has to be done in complexity less than O(n²) we have an array a[4]={1,2,3,4}. we need to find the absolute differences of all the individual elements with the rest of the elements of the array i.e,
for a[0]->|1-2|+|1-3|+|1-4|=6
for a[1]->|2-1|+|2-3|+|2-4|=4
for a[2]->|3-1|+|3-2|+|3-4|=4
for a[3]->|4-1|+|4-2|+|4-3|=6
and the output we get will be {6,4,4,6}
I did this piece using 2 for loops, but the time complexity becomes O(n²).
Aucun commentaire:
Enregistrer un commentaire