# Find the intersection point between two sorted arrays with unknown lengths in lesser than O(n)

Two sorted arrays of positive integers, X[] and Y[] are given.But, the array sizes are unknown to us. We may assume that accessing any index beyond the last element of the array returns -1. The elements in each array are distinct but the two arrays may have common elements. An intersection point between the two arrays is an element common to both the arrays, i.e., p ( p>0 ) be an intersection point if there exists i,j such that X[i]=Y[j]=p

Given a positive integer q, how can we design an algorithm ( in pseudocode ) to check if q is an intersection point between X and Y in strictly less than O(n) time complexity.

I proceeded by thinking of applying binary search on both the arrays X and Y, since they are sorted and trying to find if q exists in both of them. But not knowing the lengths leads me astray.

• Hint: can you find the size of such an array in O(log n) time? Nov 7, 2020 at 19:10
• @Jakube...yeah was wondering about that, could it be such like I try accessing indexes in powers of 2, until I get a -1 flag...? Nov 7, 2020 at 19:15
• Sounds good. With that you can get an upper bound for the size in O(log n). You can then continue to find the exact size by doing a normal binary search, and do the normal binary search to find p. But you can also just do binary search straight when you have the upper bound. Nov 7, 2020 at 19:23
{"ft_lang_label":"__label__en","ft_lang_prob":0.926158,"math_prob":0.92016304,"size":852,"snap":"2023-40-2023-50","text_gpt3_token_len":191,"char_repetition_ratio":0.12617925,"word_repetition_ratio":0.0,"special_character_ratio":0.23004694,"punctuation_ratio":0.09392265,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99452007,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-29T02:25:03Z\",\"WARC-Record-ID\":\"<urn:uuid:2a236a5b-1d6e-4625-82f6-236083547420>\",\"Content-Length\":\"165595\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ed687e40-b6ac-4edb-8441-4446acea0187>\",\"WARC-Concurrent-To\":\"<urn:uuid:1015cfe6-7b9b-4ff9-b438-eab0b29aeb08>\",\"WARC-IP-Address\":\"104.18.43.226\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/132029/find-the-intersection-point-between-two-sorted-arrays-with-unknown-lengths-in-le\",\"WARC-Payload-Digest\":\"sha1:CDUW2WGXXLPYQF55MX6HBNFZUX26N3O2\",\"WARC-Block-Digest\":\"sha1:VYWOKHBQ5F776ZBS45XV7IAQW5DPCOZ5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100047.66_warc_CC-MAIN-20231129010302-20231129040302-00252.warc.gz\"}"}