| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
O(n) algorithm
Hi all,
I am supposed to design an O(n) algorithm that solves the following problem. Input: An integer v and 2 sorted arrays A and B, each having n integers. Output: Yes if and only if there exist i and j such that v = A[i] + B[j]. No if no such i and j exist. The algorithms I came up with were linear search, which gave O(n^2) and binary search, which gave O(n log n). None gave me an O(n) algorithm. Please help. Thank you. Regards, Rayne |
|
#2
|
|||||
|
|||||
|
Nice assignment....
Must say I was stuck here too, but my roommate, who's a bit better at the O(x) stuff came up with this: cpp Code:
In the worst case scenario (value can not be made), this takes 2n operations, which still classifies as O(n).
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 275
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > O(n) algorithm |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|