1
$\begingroup$

I have a question concerning algorithms and time complexity(analysis).So I have a field A,which is sorted and has natural numbers where n is >=2(ascending order).How would you write an algorithm which has 2 indices,so that N[i] - N[j]=1750,if those two indices exist.Wenn those two don't exist,then the Algorithm needs to return 0.The Time Complexity should be T(n)=O(n).I keep trying but I fail,because everytime I get O(n^2).

  • 1
    Show what you have done so far, then we will be able to help you=)2017-01-25
  • 0
    Start with i = j = 0. Check the difference. In one case, you increment i, in the other case you increment j. Take advantage of the fact that the list starts out in ascending order.2017-01-25
  • 0
    You will find "saddleback search" a useful term to "google".2017-01-26

1 Answers 1

1

HINT

Have a pointer $p = N[0]$ and $q = N[1]$. Then shift $p$ if you have $q-p> 1750$ and $q$ if $q-p < 1750$.