#include
using namespace std;
void sort(int p,int n);
int min(int p,int n);
int m=100000010;
int main()
{
int t,i;
//cout<<“Enter the value of t:”;
cin>>t;
while(t)
{
int n;
//cout<<“Enter the value of n:”;
cin>>n;
int a[n],p=a;
for(i=0;i<n;i++)
{
//cout<<“Enter value at index “<<i<<”:”;
cin>>a[i];
}
sort(p,n);
cout<<min(p,n);
t–;
}
}
void sort(int p,int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if((p+j)<(p+j+1))
{
temp=(p+j); (p+j)=(p+j+1); (p+j+1)=temp;
}
}
}
}
int min(int p,int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(((p+j)-(p+j+1))<m)
{
m=((p+j)-*(p+j+1));
}
}
}
return m;
}
My program gives the correct output to the sample input but gives a WA when I submit it. I used the inbuilt sort function of C++
Here is the link to my code https://www.codechef.com/viewsolution/36180293
Can someone please help me to find out what’s wrong with my code
what is wrong with this code? I am getting Wrong Answer
#include <bits/stdc++.h>
using namespace std;
long long arr[5001];
int main() {
int t;
cin >> t;
while(t --) {
int n;
cin >> n;
for (int i = 0; i < n; i ++) {
cin >> arr[i];
}
sort(arr, arr+n);
long long ans = INFINITY;
for(int i = 1; i < n; i ++) {
ans = min(ans, arr[i] - arr[i - 1]);
}
cout << ans;
}
return 0;
}
int main(void) {
int t;
scanf("%d",&t);
while(t–){
long long int n,x;
int y=10000000;
scanf("%lld",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%lld",&arr[i]);}
printf("%lld\n",y);
}
return 0;
}
why this is a wrong approach ,its give me right ans but when i am submitting it ,it shows that it is wrong .please help me.