473,503 Members | 12,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

efficient algorithm

13 New Member
Hey I'm looking for a solution to the belowe question. Any recommendations on how to write this algorithm???
Thanks:


You are given a sequence s of numbers, in increasing order, for example:

s = 2, 5, 7, 9, 13, 15.

You are also given a number n, say n = 16. You have to try to find two numbers x and y from s, such that x+y = n, if such numbers exist. In this example, you could take x = 7 and y = 9.

However, you must do so as efficiently as possible: you must not perform more additions than the number of elements in s. Describe an algorithm to do this.
Apr 18 '07 #1
3 1124
Tig201
103 New Member
This is a rough idea but hopefully it will help
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.   Dim lst(6) As Integer, Cnt1 As Integer, Cnt2 As Integer
  3.   Dim No As Integer, NoNew As Integer
  4.  
  5.   No = 16
  6.   lst(1) = 2
  7.   lst(2) = 5
  8.   lst(3) = 7
  9.   lst(4) = 9
  10.   lst(5) = 13
  11.   lst(6) = 15
  12.  
  13.   For Cnt1 = 1 To UBound(lst) Step 1
  14.     NoNew = No - lst(Cnt1)
  15.     For Cnt2 = Cnt1 + 1 To UBound(lst) Step 1
  16.       If NoNew = lst(Cnt2) Then
  17.         MsgBox (lst(Cnt1) & "+" & lst(Cnt2) & "=" & No)
  18.         Cnt2 = UBound(lst)
  19.         Cnt1 = UBound(lst)
  20.       End If
  21.     Next Cnt2
  22.   Next Cnt1
  23. End Sub
  24.  
The idea is that you could eliminate the numbers as you go through the list
Apr 18 '07 #2
Killer42
8,435 Recognized Expert Expert
However, you must do so as efficiently as possible: you must not perform more additions than the number of elements in s. Describe an algorithm to do this.
I don't think this is possible, unless you add the further constraint that the numbers must be next to each other in the list. (Of course, I'm no mathematician.)

Unless you get sneaky and use subtraction instead of addition, but I doubt that's acceptable.
Apr 19 '07 #3
Killer42
8,435 Recognized Expert Expert
This is a rough idea but hopefully it will help
...
Hm... neat little routine. I like it! :)
Apr 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
2268
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
11
3585
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
1
1871
by: Al | last post by:
Hi, I need to store literally thousands if images and scanned document in the Database. I would like to know what is the most efficient algorithm both in terms of speed and storage for saving images...
1
3951
by: pedagani | last post by:
Dear comp.lang.c++, I'm interested in knowing the general techniques used to handle large binary files (>10GB) efficiently such as tweaking with filebuf , etc. Reading chunk by chunk seems to be...
9
1477
by: sovixi | last post by:
Hey I'm looking for a solution to the belowe question. Any recommendations on how to write this algorithm??? Thanks: You are given a sequence s of numbers, in increasing order, for example: ...
9
1245
by: sovixi | last post by:
Hey I'm looking for a solution to the below question. Any recommendations on how to write this algorithm??? Thanks: You are given a sequence s of numbers, in increasing order, for example: ...
4
1565
by: Luna Moon | last post by:
seeking highly efficient caches scheme for demanding engineering computing? HI all, To same the time of costly function evaluation, I want to explore the possibility of caching. Suppose in...
12
1707
by: pedagani | last post by:
Dear comp.lang.c++, Could you make this snippet more efficient? As you see I have too many variables introduced in the code. //Read set of integers from a file on line by line basis in a STL...
82
3610
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
5
3932
by: seemanikam | last post by:
Can anyone suggest me how to devise an efficient divide-and-conquer algorithm for the Tower-of-Hanoi problem when the disks are colored alternately red and blue, and with the extra rule that no disk...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.