Given 2 sorted arrays with distinct elements, one sorted in increasing order and other sorted in decreasing order. Can we find the element which is present in both of them in *O*(*logN*) or *O*((*logN*)^{2})?

# | User | Rating |
---|---|---|

1 | Radewoosh | 3759 |

2 | orzdevinwang | 3697 |

3 | jiangly | 3662 |

4 | Benq | 3644 |

5 | -0.5 | 3545 |

6 | ecnerwala | 3505 |

7 | tourist | 3486 |

8 | inaFSTream | 3478 |

9 | maroonrk | 3454 |

10 | Rebelz | 3415 |

# | User | Contrib. |
---|---|---|

1 | adamant | 173 |

2 | awoo | 167 |

3 | SecondThread | 163 |

4 | BledDest | 162 |

4 | Um_nik | 162 |

6 | maroonrk | 161 |

6 | nor | 161 |

8 | -is-this-fft- | 150 |

9 | Geothermal | 146 |

10 | TheScrasse | 143 |

Given 2 sorted arrays with distinct elements, one sorted in increasing order and other sorted in decreasing order. Can we find the element which is present in both of them in *O*(*logN*) or *O*((*logN*)^{2})?

↑

↓

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Dec/02/2023 18:16:38 (j3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Good question. Personally I don't think it's possible but I have utterly no proof of my suspicion. I can only think of (trivial)

O(NlogN) solution.Yeah NlogN solution is clear

You can do

O(N): just merge this two arrays and iterate over elements to see if there are equal neighbors.From this point of view it is clear thatO(N) is the best complexity. For example, for arrays where elements of two arrays alternate when you merge them can be checked only through accessing all their elements by comparing all pairs.Thanks!

We can use two pointers. Start from the left in one array and from the right in the other one (minimum element in each one). If the elements are equal, you've found a match. If not, move the pointer that has the minimum element.