C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 28th, 2005, 09:59 PM
wu_weidong wu_weidong is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 9 wu_weidong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 37 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old September 29th, 2005, 03:04 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 995 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 802356 Folding Title: Super Ultimate Folder - Level 2Folding Points: 802356 Folding Title: Super Ultimate Folder - Level 2Folding Points: 802356 Folding Title: Super Ultimate Folder - Level 2Folding Points: 802356 Folding Title: Super Ultimate Folder - Level 2Folding Points: 802356 Folding Title: Super Ultimate Folder - Level 2Folding Points: 802356 Folding Title: Super Ultimate Folder - Level 2Folding Points: 802356 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 13 h 57 m 35 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
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:
Original - cpp Code
  1. int can_value_be_made(int value, int N, int a[], int b[])
  2. {
  3.     int i, j, sum;
  4.     i = 0;
  5.     j = N - 1;
  6.     while((i != N) && ( j >= 0))
  7.     {
  8.         sum = a[i] + b[j];
  9.  
  10.         if( sum == value)
  11.         {
  12.             return 1;
  13.         }
  14.         else
  15.         {
  16.             if(sum < value)
  17.                 i++;
  18.             else
  19.                 j--;
  20.         }
  21.     }
  22.     return 0;
  23. }


In the worst case scenario (value can not be made), this takes 2n operations, which still classifies as O(n).
Comments on this post
MichaelSoft agrees: A very nice function, well done 'roommate'
__________________
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > O(n) algorithm


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT