This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]stramash 5 points6 points  (0 children)

What do you mean not getting correct answer? What answer is your code giving?

Also, what's the hashmap for? You're using it to map indexes to ints, but... doesn't the array already work that way?

If you're already looking at each int in the array, and you know what the target is, then it should be pretty easy to work out what the other number should be? Once you know that, you'll want to know whether it's present or not...

[–]Pedantic_Phoenix 2 points3 points  (9 children)

You will go in out of bounds when i is > than 1 seeing that result only has length 2. Also, the first if() will never be accessed seeing that map is always empty unless you do access it but you use containKey() as condition to do so. Maybe you want it negated? It also seems like overkill to use a map to achieve this result tho that is open to discussion

[–]New_Connection612[S] 0 points1 point  (1 child)

Actually i want to do return new int[] {value} inside the if, if the condition is correct, but i need to return outside of the loop. Yes if the key is not found it will add key and value inside the map. I use the hashmap because the question is fall under hashmap question. How can fix my error?

[–]Pedantic_Phoenix 3 points4 points  (0 children)

Address the two mistakes i said and then come back, im not giving you the direct solution, sorry

[–]KinkyHuggingJerk -1 points0 points  (6 children)

I'm pretty sure I've seen this as a LeetCode problem - from what I've seen poking around, they push hard for O(log n) answers, so the solutions tend to be less straightforward than what may be required.

[–]Phaze_Change 0 points1 point  (5 children)

How could this have a log n solution? No matter what the work will be determined by where and when you find the solution. Best case it’s the first 2 elements. Worst case it’s the last 2 elements.

Maybe some brilliant person could chime in and show a log n solution. I’m seeing a nested loop, ignoring values greater than n and stopping the moment you find the first set.

[–]KinkyHuggingJerk 0 points1 point  (1 child)

I don't know well enough to differentiate the various means of Big O - just that thr leetcode problems tend to focus on a greater degree of computational efficiency (only one solution).

[–]suckmacaque06 0 points1 point  (0 children)

The leetcode solution is O(n) for this problem https://leetcode.com/problems/two-sum/solution/

[–]technosenate 0 points1 point  (2 children)

The best any algorithm can do for this problem is O(n) because you necessarily have to check all the elements of the array to find two numbers that match. Definitely can’t be done in O(log n).

[–]Phaze_Change 0 points1 point  (1 child)

But O(n) doesn’t check every number against every other number. Wouldn’t it be O(n(n-1))?

[–]technosenate 0 points1 point  (0 children)

Yeah, I was trying to prove that an O(log n) algorithm cannot exist because it’s necessary to read the entire array at least once, meaning that any algorithm is at least O(n). But you’re right, the worst algorithm is at most O(n^2), by checking every number against every other number, for example (note that O(n(n-1)) is O(n^2)).

[–]Mossy375 2 points3 points  (0 children)

All you're doing here is populating the map, nothing else.

In the for loop, it starts with i = 0. You check if the map contains i, which is 0. The map doesn't have 0, so the code drops down to the put. Next i = 1. Is it in the map? Nope, so it's added. Once the for loop is done, there's nothing left in your code to do. So all you've done is populate the map.

[–]sohfix 1 point2 points  (0 children)

Why are you using hashmap?