ans = abs(arr[i]-arr[i-1]);
cout << ans << “\n”;
what’s the final thing this loop does? When i is 1, it calculates the absolute difference between a and a. Since a is sorted, these are the two smallest elements in the array.
How does that give the required answer? It doesn’t.