The question is: To select 2 numbers out of n positive integer inputs (order matters), where the number that is placed anywhere before the second must be greater than the second by a given number m. Count the total number of selections.
An example answer would be, given the inputs 5 1 3 2 4 and m is 0, the total number of selections is 5 (51,53,52,54,32)
Another example answer would be, given the inputs 6 5 4 3 2 1 and m is 2, the total number of selections is 6 (63,62,61,52,51,41)
How do I improve my double for-loop method in terms of time?
using namespace std;
int main()
{
int n, m, i, j, m = 0, sum = 0;
cin >> n >> m;
int *a=new int[n];
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(i=0;i<n-1;i++)
{
if (a[i] - m <= 0) continue;
int temp=a[i]-m;
for(j=i+1;j<n;j++)
if(a[j]<temp) sum++;
}
cout<<sum<<endl;
return 0;
}
`