Two Sorted Arrays, sum of 2 elements equal a certain number

2024/10/12 23:28:48

I was wondering if I could get some help. I want to find an algorithm that is THETA(n) or linear time for determining whether 2 numbers in a 2 sorted arrays add up to a certain number.

For instance, let's say we have 2 sorted arrays: X and Y

I want to determine if there's an element of X and an element of Y that add up to exactly a certain number, let's say 50.

I have been able to come up with these algorithms in Python so far, but I am pretty sure they are order of THETA(n^2) rather than THETA(n).

def arrayTestOne(X,Y):S =[1 for x in X for y in Y if x+y == 50]def arrayTestTwo(X,Y):for x in X:for y in Y:if x + y == 50:print("1")

I'm thinking it's the double for loops that break the linear time, but how else would you iterate through 2 lists? Any thoughts would be appreciated.

Answer

What you can do is start with the highest in one list and the lowest in the other, and check the sum.

If the sum is your target, you're done.

If it's too high, go to the next highest value in the first list.

If it's too low, go to the next lowest value in the second.

If you go through both lists without reaching the target, you return false.

https://en.xdnf.cn/q/118148.html

Related Q&A

I cant seem to install numpy

I tried to install numpy, but whenever I start my program, I get these messages.Error importing numpy: you should not try to import numpy fromits source directory; please exit the numpy source tree, an…

Using slices in Python

I use the dataset from UCI repo: http://archive.ics.uci.edu/ml/datasets/Energy+efficiency Then doing next:from pandas import * from sklearn.neighbors import KNeighborsRegressor from sklearn.linear_mode…

Elasticsearch delete_by_query wrong usage

I am using 2 similar ES methods to load and delete documents:result = es.search(index=users_favourite_documents,doc_type=favourite_document,body={"query": {"match": {user: user}}})A…

SQLAlchemy: Lost connection to MySQL server during query

There are a couple of related questions regarding this, but in my case, all those solutions is not working out. Thats why I thought of asking again. I am getting this error while I am firing below quer…

row to columns while keeping part of dataframe, display on same row

I am trying to move some of my rows and make the them columns, but keep a large portion of the dataframe the same.Resulting Dataframe:ID Thing Level1 Level2 Time OAttribute IsTrue Score Value 1 …

SQLAlchemy InvalidRequestError when using composite foreign keys

My table relationships in SQLAlchemy have gotten quite complex, and now Im stuck at this error no matter how I configure my relationship.Im a bit new to SQLAlchemy so Im not sure what Im doing wrong, b…

google search by google api in r or python

I want to search some thing (ex:"python language") in google by python or R and it will give me the list of links for that google search like:https://en.wikipedia.org/wiki/Python_(programming…

NumPy Wont Append Arrays

Im currently working on a neural network to play Rock-Paper-Scissors, but Ive run into an enormous issue.Im having the neural network predict what will happen next based on a history of three moves, wh…

(Django) Limited ForeignKey choices by Current User in UpdateView

I recently was able to figure out how to do this in the CreateView, but the same is not working for the UpdateView (Heres the original post on how to do it in the CreateView: (Django) Limited ForeignKe…

Merge list concating unique values as comma seperated retaining original order from csv

Here is my data:data.csvid,fname,lname,education,gradyear,attributes 1,john,smith,mit,2003,qa 1,john,smith,harvard,207,admin 1,john,smith,ft,212,master 2,john,doe,htw,2000,devHere is the code:from iter…