-1

So i've tried interpreting this pseudocode a friend made and i wasn't exactly sure that my method returns the right result. Anyone who's able to help me out?

I've done some test cases where e.g. an array of [2,0,7] or [0,1,4] or [0, 8, 0] would return true, but not cases like: [1,7,7] or [2,6,0].

Array(list, d)
for j = 0 to  d−1 do
for i = 0 to d−1 do
for k = 0 to d−1 do
if list[j] + list[ i] + list[k] = 0 then
return true

end if
end for
end for
end for
return false

And i've made this in java:

public class One{
    public static boolean method1(ArrayList<String> A, int a){
    for(int i = 0; i < a-1; i++){
        for(int j = 0; j < a-1; j++){
            for(int k = 0; k < a-1; k++){
                if(Integer.parseInt(A.get(i)+A.get(j)+A.get(k)) == 0){
                    return true;
                }
                }
            }
        }
    return false;
}

}

Thanks in advance

Peter
  • 21
  • 1
  • 2
  • 7
  • Who's `n`?! (`i < n-1`) – fanton Feb 13 '16 at 11:49
  • @fanton corrected it – Peter Feb 13 '16 at 11:51
  • Ok, now I see that you're calling `parseInt` method on a whole bunch of stuff. Considering that you have a list of integers represented as strings (somehow weird), you probably want to do `Integer.parseInt(A.get(i)) + Integer.parseInt(A.get(j)) + Integer.parseInt(A.get(k)) == 0` instead of what you are doing. – fanton Feb 13 '16 at 11:52
  • @fanton That won't change anything though, just tested. – Peter Feb 13 '16 at 11:57
  • Are the test case results expected and correct? – Maertin Feb 13 '16 at 11:57
  • @Peter: How do you call the method? Post the snippet that's using your method. – fanton Feb 13 '16 at 11:58
  • @Maertin that's what im not sure about. I posted the typical results and want to know if they somewhat are correct comparing them to the pseudocode. – Peter Feb 13 '16 at 12:05
  • Ok - so the results and the code are what you came up with. The only thing that is in original is the pseudo code that you put. – Maertin Feb 13 '16 at 12:11
  • @Maertin Yes, that is correct. I made the code, not the pseudo. – Peter Feb 13 '16 at 12:14

2 Answers2

0

For a fix to your concrete problem, see my comment. A nicer way to write that code would be to actually use a list of Integer instead of String, because you will then want to convert the strings back to integers. So, your method looks better like this:

public static boolean method(List<Integer> A) {
    for (Integer i : A)
        for (Integer j : A)
            for (Integer k : A)
                if (i + j + k == 0)
                     return true;
   return false;
}

See that you don't even need the size as parameter, since any List in Java embeds its own size.

Somehow offtopic

You're probably trying to solve the following problem: "Find if a list of integers contains 3 different ones that sum up to 0". The solution to this problem doesn't have to be O(n^3), like yours, it can be solved in O(n^2). See this post.

Community
  • 1
  • 1
fanton
  • 720
  • 5
  • 19
  • It differs a little from my code though. In the case where [x,y,0] returns true for you, it returns false for mine? What would the pseudocode want? – Peter Feb 13 '16 at 12:10
  • The pseudocode doesn't seem to care about different numbers. Neither the Java code above. The difference is: If you have an array that contains natural numbers from 0 to N, would you like your algorithm to return `true` of `false` ? – fanton Feb 13 '16 at 12:15
0

Ok, so here is what I believe the pseudo code is trying to do. It returns true if there is a zero in your list or if there are three numbers that add up to zero in your list. So it should return true for following test cases. (0,1,2,3,4,5), (1,2,3,4,-3). It will return false for (1,2,3,4,5). I just used d=5 as a random example. Your code is good for the most part - you just need to add the ith, jth and kth elements in the list to check if their sum equals zero for the true condition.

Maertin
  • 384
  • 1
  • 8