So I'm working on this project and it has me stumped.
We're given 2 arrays of integers. The first array is the set of items we will use and the second array is a set of target values.
From the first array, I am supposed to find if there are any 3 numbers that sum up to one of the target values in the second array. So for every target value in the second array, I check to see if there are any 3 numbers that sum up to that target. If there is, I just print out a "yes" or a "no"
Now, I know how to do this in O(n^3) time, but the project requires O(n^2) and I can't figure out how to do that - largely because I also have to loop through the array of target values and do the check on each of them.
I was given a hint to use graphs, trees, BFS, but even then I'm struggling to see how i can do this.
Anyone know what data structure I should use or how I should approach this? Thank you!
Task: You will write a java program which reads a set of integer data from a file, stores that data into a data structure, and repeatedly determines whether any 3 items from the data set sum to a target value. O(n^2)
Example first array: 15 82 22 36 29 11 31 4 31 Example second array: 119 9 -2 57 29 73 93 Example output: 119 YES 9 NO -2 NO 57 YES 29 NO 73 YES 93 NO