We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

//Same approch in C++ using vector
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
vector vec(n);
generate(vec.begin(), vec.end(), []{int x; cin>>x; return x;});
sort(vec.begin(), vec.end());

int diff = abs(vec[0] - vec[1]);
int sub;
for(int i=0; i<(n-1); i++){
sub = abs(vec[i] - vec[i+1]);
if(sub < diff)
diff = sub;
}
cout<<diff;
return 0;

## Minimum Absolute Difference in an Array

You are viewing a single comment's thread. Return to all comments →

//Same approch in C++ using vector int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */

int n; cin>>n; vector vec(n); generate(vec.begin(), vec.end(), []{int x; cin>>x; return x;}); sort(vec.begin(), vec.end());

}

this will only work for checking consecutive elements diff not ANY

forr that we are doing sort(vec.begin(), vec.end());

The consecutive elements give the minimal than "any"