Difference Between Both The Codes (Problem Code:ZCO15002)

CODE 1>>>#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k,i,cnt=0,j;
cin>>n>>k;
int *p=new int[n];
for(i=0;i<n;i++)
cin>>p[i];
sort(p,p+n);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(abs(p[i]-p[j])>=k)
{
cnt+=n-j;
break;
}
}
}
cout<<cnt<<endl;
}
CODE 2>>

#include
using namespace std;
#include
int main()
{
int n,k,i,cnt=0,j;
cin>>n>>k;
int *a=new int[n];
for(i=0;i<n;i++)
cin>>a[i];

for(i=0;i<n-1;i++)
{
    for(j=i+1;j<n;j++)
    {
       if(abs(a[i]-a[j])>=k)
       {
           cnt+=1;
     
       }
    }
}
cout<<cnt<<endl;

}

Could you please give the link to your submission or format you code using preformatted text.

1 Like

You became @ssjgz :slight_smile:

3 Likes

CODE 1>>CodeChef: Practical coding for everyone
CODE 2>>CodeChef: Practical coding for everyone

Code 1 is optimized code
While code 2 is brute force

Thats the difference
Try to optimize your code