baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:
I was wondering did anyone seen anything like this before and knows a solution to this problem ?
Problem:
Given an unsorted array of N numbers such that N[i] not equal to N[j] for j,i <= N, find an intersect between subarray N[x..y] and N[k..l] for x,y,k,l in N, when elements in N[x..y] are enlarged by a constant factor zExample:
let say I have an array like this:
N = [1,3,5,2,6,7,55,14,23,54,56,15,44,11,9,87,10,58,19,17,26, ...]
And let say that I am given an interval I(4,8) and another one II(14,18), that is :Now given that the z = 3 the intersect between I and II would beI(4,8) = [6,7,55,14,23] II(14,18) = [9,87,10,58,19]In the initial array N there cannot be any duplicates and the numbers span from 1..N. I know that I can do this by matching every number in I to every number in II but this would be an algorithm with a nested loop. And that is bad if my arrays are big (as they turn out to be) plus i have to repeat this for a large number of intervals (I'(a,b)) and different z in every iteration.I inters II = [9,10,58]
So my question is: what would be an alternative to this naive approach? Does anyone know how to pre-processes N to make those queries in O(1) time because this would be ideal.
cheers
baxy
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Computing array intersections
by BrowserUk (Patriarch) on Mar 31, 2012 at 16:39 UTC | |
|
Re: Computing array intersections
by mbethke (Hermit) on Mar 31, 2012 at 16:17 UTC |