Sup /sci/
I need your help
So I have linear array and elements are sorted form lowest to highest.
I just need algorithm that returns indexes if arr[i]-arr[j]=160.
Runtime should be O(n).
help me /sci/ you're my only hope
try stackoverflow, I hear they love answering these kinds of questions
>>7765937
lmao you lazy fuck
>>7765937
If you are too retarded to figure that out yourself then you should literally put a gun in your mouth.
>>7765937
My algorithm:
>create a second array b where b[i] = a[i]+160
>merge the two arrays in one ordered array (since the two subarrays are already ordered this is linear)
>check for duplicates (they are always adjacent)
>the original indexes of the two elements (one coming from a and one from b) are the j and i you were looking for
This obviously run in O(n).
I'll let you work out the implementation.
>>7766013
obtuse as fuck and impractical
other than that, you're retarded for spoonfeeding OP with a clearly introductory question on algorithms, congratulations
>>7765937
low=1
high=1
here:
while (a[high]-a[low]<160)
high++
endwhile
while (a[high]-a[low]>160)
low++
endwhile
if (a[high]-a[low]==160)
return (high, low)
endif
if (high==end)
return "nope"
endif
goto here
>>7766020
i = 0;
for (j = 0; j < n; j++){
while (a[i] < a[j] - 160) a[i]++;
if (a[i] == a[j] - 160) return (i, j);
}
return (-1, -1);
>>7766020
>high==end
Kill yourself
Here's some code http://ideone.com/Gngps6
What exactly is this for? Far too easy to be algorithms homework, but it doesn't seem like that useful of an algorithm.
>>7766020
>goto
>/sci/ is supposed to be smart?