Hi,

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 z
Example:
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 :
I(4,8) = [6,7,55,14,23] II(14,18) = [9,87,10,58,19]
Now given that the z = 3 the intersect between I and II would be
I inters II = [9,10,58]
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.
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


In reply to Computing array intersections by baxy77bax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.