0

I have the following input

int combinationCount = 3; 
int arr[] = {1, 1, 2, 2, 3}; 

combinationCount is {1,2,3}, combinationCount defines the number of sequence of number. For Eg: combinationCount = 3 means {1,2,3} and combinationCount = 2 means {1,2}

Array is always sorted, I want to print the output as the number of combinations as follow

[1,2,3], [1,2,3], [1,2,3], [1,2,3] //I have to iterate the whole array as it is just logic for a problem

Output Explanation (I want to print values, not index): This is just an explanation of output which shows the index position of the value printed.

Index position of each value
[0, 2, 4], [0, 3, 4], [1, 2, 4], [1, 3, 4]

Example 2
int combinationCount = 2; // means combination is {1,2}
int arr[] = {1, 2, 2};
Print: [1,2], [1,2]

Example 3
int combinationCount = 3; // means combination is {1,2,3}
int arr[] = {1, 1, 3};
Print nothing

The program which I written is as follow:

int combinationCount = 3;
int arr[] = {1, 1, 2, 2, 3}; 
List<Integer> list = new ArrayList<>();
int prev = 0;

for (int i = 0; i < arr.length; i++) {
    if (arr[i] == 1) {
        prev = 1;
        list = new ArrayList<>();
        list.add(1);
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] == prev + 1) {
                prev = arr[j];
                list.add(arr[j]);
            } else if (arr[j] > (prev + 1)) {
                break;
            }
        }
        if (list.size() == combinationCount) {
            System.out.print(list + ",");
        } 
    } else {
        break;
    }
}

Output coming as
[1,2,3],[1,2,3]

which is not correct

Somewhere I am missing loop and how optimized code we can write? Any suggestions pls. Kindly let me know for any concern.

Abdul Basith
  • 83
  • 1
  • 11

1 Answers1

1

You can use Cartesian Product. I have used this answer as reference.

public class Test {

    public static List<List<Integer>> product(List<List<Integer>> lists) {
        List<List<Integer>> result = new ArrayList<>();
        int solutions = lists.stream().mapToInt(List::size).reduce(1, (a, b) -> a * b);

        for (int i = 0; i < solutions; i++) {
            int j = 1;
            List<Integer> tempList = new ArrayList<>();
            for (List list : lists) {
                tempList.add((Integer) list.get((i / j) % list.size()));
                j *= list.size();
            }
            result.add(tempList);
        }
        return result;
    }

    public static void main(String[] args) {

        int combinationCount = 2, count = 0;
        int arr[] = {1, 1, 3};
        Map<Integer, List<Integer>> map = new HashMap<>();
        List<List<Integer>> combinations = new ArrayList<>();

        for (Integer idx = 0; idx < arr.length; idx++) {
            map.computeIfAbsent(arr[idx], k -> new ArrayList<>()).add(idx);
        }

        for (int i = 1; i <= combinationCount; i++) {
            if (map.getOrDefault(i, null) != null)
                count += 1;
        }

        if (count == combinationCount) {
            List result = product(new ArrayList(map.values()));
            System.out.println(result);
        } else {
            System.out.println("No combination found");
        }
    }
}

Output:

No combination found
deadshot
  • 8,881
  • 4
  • 20
  • 39