JOIN
Get Time
forums  Revision History
Search My Post History  |  My Watches  |  User Settings
Forums Tutorial Discussions Computational Complexity Example 3 Revision History (1 edit)
Example 3
Please refer to the example 3 having following code:

int j=0;
for (int i=0; i<N; i++) {
while ( (j><N-1) && (A[i]-A[j] > D) )
j++;
if (A[i]-A[j] == D) return 1;
}

This would fail if the input integer array has following values and D =1 :

10, 5, 4, 2

I feel there is a problem with the way j is incremented.

Am i missing something??

Thanks
Example 3
Please refer to the example 3 having following code:

int j=0;
for (int i=0; i<N; i++) {
while ( (j><N-1) && (A[i]-A[j] > D) )
j++;
if (A[i]-A[j] == D) return 1;
}

This would fail if the input integer array has following values:

10, 5, 4, 2

I feel there is a problem with the way j is incremented.

Am i missing something??

Thanks