Can someone tell what im doing wrong.
created
last reply
- 6
replies
- 708
views
- 2
users
- 1
link
Can someone tell what im doing wrong.
#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.
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.
Topic | Category | Replies | Views | Activity |
---|---|---|---|---|
Getting WA on the problem PIE continuously, what am I missing? spoj.com | ProblemSet Archive | 2 | 172 | Mar 8 |
What are allowed characters in task t9 | ProblemSet Archive | 4 | 212 | Feb 24 |
Help me please! | ProblemSet Archive | 4 | 17 | 5d |
AIBOHPHOBIA - LPS vs Direct Approach | ProblemSet Archive | 1 | 110 | Mar 31 |
ABCPATH - ABC Path | ProblemSet Archive | 1 | 88 | May 11 |