1 / 7
Mar 2021

#include <bits/stdc++.h>
using namespace std;
int main(){
int tc;
scanf("%d",&tc);
while(tc–){
int a;
scanf("%d",&a);
int arr[a];
for(int i=0;i<a;i++){
scanf("%d",&arr[i]);
}
sort(arr,arr+a);
int ans = 0;
int dp[a+5];
for(int i=a;i>0;i–){
ans = abs(arr[i]-arr[i-1]);
}
cout << ans << “\n”;
}
return 0;
}

How does this code solve the problem? It always writes the difference between the two smallest numbers.

TECHLN53

Can u explain it to me? how come its the difference about the two smallest numbers?.thank you.

    for(int i=a;i>0;i–){
      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[1] and a[0]. Since a is sorted, these are the two smallest elements in the array.

How does that give the required answer? It doesn’t.

oh i dont know that,but on the first testcae i got the answer by doing that,i really have no clue how to do it.