What I am trying to do is:
I have a list of values, and I want to list each three of them. For example:
I have:
"one", "two", "three", "four", "five"
And I want to print the following:
one two three
one two four
one four five
two three four
two three five
and so on
You got the point, I want every three values (not in order off course)
What I did is:
import java.util.*;
public class Solution {
public static void main(String args[]) {
String[] people = new String[] { "one", "two", "three", "four",
"five" };
Solution s = new Solution();
s.solve(people, 3, new LinkedList<>(), 0);
}
public void solve(String[] people, int n, List<String> data, int i) {
if (data.size() == n) {
System.out.println(data.toString());
} else if (i < people.length) {
String value = people[i];
solve(people, n, data, i + 1);
data.add(value);
solve(people, n, data, i + 1);
}
}
}
You can run it:
My problem is that it prints:
[five, four, five]
Which is obviously wrong, how can It print two same values? In my code, I add the value once and I don't add it another time
Could you help?