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

Algorithm help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 1

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


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.