[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Algorithm help
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /sci/ - Science & Math

Thread replies: 11
Thread images: 1
File: leia.jpg (67 KB, 300x300) Image search: [Google]
leia.jpg
67 KB, 300x300
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?
Thread replies: 11
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.