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.
9 1244
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.
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
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
any code sugestions how to do it????
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
thanks a lot for your help.
Ps I have posted this topic on various forums while looking for an answer. Sorry for that.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |