+ 5
Question about daily coding problem 1#
Inside my code is a description. I don't understand what "Can you do it in one pass?" means. https://code.sololearn.com/cV3VQCDiRGFL/?ref=app
10 Answers
+ 2
If you want to do it in O(n) you can use Hashing.
https://en.wikipedia.org/wiki/One-pass_algorithm
+ 9
I understand "one pass" as "one iteration" but I'not sure...!
ššš
+ 3
Diego
Is this a one pass solution?
https://code.sololearn.com/cMoh3wz0S2oE/?ref=app
I have only one for loop to the get the numbers from the array. And a list. If the number is inside my list print true, if not add k-number to the list.
If I'm right I check every number only once. And I think that is the sense of one pass.
+ 3
You can simply do
if (hash.contains(k-numbers[i]) == true){
// Code goes here
}
+ 3
For bigger lists performance.
https://stackoverflow.com/questions/32552307/hashset-vs-arraylist-contains-performance
https://www.baeldung.com/java-hashset-arraylist-contains-performance
Same goes for hash.add(numbers[i]).
+ 3
Coder What makes you think that? I think it is clear that I had only problems with the phrase "one pass". At no point did I write that someone should solve this challenge.
+ 2
Diego Thank you :)
+ 2
Diego And add numbers[i] ? Is there a difference?
+ 2
What happens when I use other Lists, for example ArrayList instead of a HashSet?
+ 2
Diego Thanks :)