473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

efficient algorithm [Merged]

13
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:

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
9 1244
sovixi
13
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:

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 #2
weaknessforcats
9,208 Expert Mod 8TB
Start at the right.
Apr 18 '07 #3
JosAH
11,448 Expert 8TB
Start at the right.
Not necessarily so: if n is near the smallest element in s then it's better to
start at the left.

kind regards,

Jos
Apr 18 '07 #4
sicarie
4,677 Expert Mod 4TB
::subscribing::
Apr 18 '07 #5
JosAH
11,448 Expert 8TB
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:

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.
You asked exactly this same question in the C/C++ forum. Do you want me
to merge both threads and move them to the 'software development' forum?

Double posting is considered a nono; don't do that anymore.

kind regards,

Jos
Apr 18 '07 #6
sovixi
13
any code sugestions how to do it????
Apr 18 '07 #7
JosAH
11,448 Expert 8TB
any code sugestions how to do it????
Nope, we don't do code here but here's a hint: because set s is sorted in
ascending order you can find two indexed l and h (h == l+1) such that
s[l] <= n and s[h] >= n; You can find l and h in O(log(|s|) where |s| is the
size of set s.

repeat the following (while applicable)
x= s[l]+s[h]
if (x == n) found solution
else if (x < n) h= h+1
else l= l-1

This needs O(|s|) additions at most so voila: problem solved.

kind regards,

Jos
Apr 18 '07 #8
sovixi
13
thanks a lot for your help.

Ps I have posted this topic on various forums while looking for an answer. Sorry for that.
Apr 18 '07 #9
JosAH
11,448 Expert 8TB
thanks a lot for your help.

Ps I have posted this topic on various forums while looking for an answer. Sorry for that.
You're welcome of course and don't mention those double posts: I closed your
thread in the java forum. I think I'll delete it now.

kind regards,

Jos
Apr 18 '07 #10

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

Similar topics

2
by: Piotr | last post by:
Is there any way to split all merged words but www and e-mail addresses? I have regexp preg_replace("/(\.)(])/", "\\1 \\2", "www.google.com any,merged.words mymail@domain.com") it give me...
13
by: sf | last post by:
Just started thinking about learning python. Is there any place where I can get some free examples, especially for following kind of problem ( it must be trivial for those using python) I have...
8
by: Ben Fidge | last post by:
I have a requirement to compare two lists, generating a third which is a combination of both. With the first list as the reference list and the second as the test list, I want the each of the...
11
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
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...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
4
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
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...
1
by: vekka | last post by:
Hi! Could someone please help me to understand the use of merge algorithm(conceptually) with linked lists. What I want to do in the linked list function is: the function gets two inputs, i.e the...
82
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....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.