Print all subsequence arrays where no elements are adjacents

loknath :

In the following i can able to print all the sub sequence arrays but can someone please help to print all the subsequence where no elements adjacent.

`package test;

import java.util.ArrayList;
import java.util.List;

public class Test1 {
    public static List<List<Integer>> combinations(int[] arr) {
        List<List<Integer>> combinations = new ArrayList<List<Integer>>();
        List<Integer> total;
        for (int i = 0; i < arr.length; i++) {
            int k = combinations.size();
            for (int j = 0; j < k; j++) {
                // if ((i + 1 < arr.length)) {

                total = new ArrayList<Integer>(combinations.get(j));
                total.add(new Integer(arr[i]));

                combinations.add(total);
                // }
            }
            total = new ArrayList<Integer>();
            total.add(new Integer(arr[i]));
            combinations.add(total);
        }
        System.out.println(combinations);
        return combinations;
    }

    public static void main(String args[]) {
        int arr[] = { 1, 2, 3 };
        combinations(arr);
    }
}`

output :- [[1], [1, 2], [2], [1, 3], [1, 2, 3], [2, 3], [3]]

expected output : - [[1], [2], [1, 3],[3]]

vivek_23 :

Generate Combinations:

private static List<List<Integer>> generateCombinations(int[] arr){
        List<List<Integer>> combs = new ArrayList<List<Integer>>();
        int prev2 = 1,prev1 = 1;

        for(int i=0;i<arr.length;++i){
             //individual
            List<Integer> l = new ArrayList<>();
            l.add(arr[i]);
            combs.add(l);   

            if(i < 2){ // since these are base cases for our fibonacci sequence
                continue;
            }

            int size = prev1 + prev2 - 1; 

            for(int j=0;j<size;++j){
                List<Integer> new_list = new ArrayList<>(combs.get(j));
                new_list.add(arr[i]);
                combs.add(new_list);
            }           

            prev1 = prev1 + prev2;
            prev2 = prev1 - prev2;
        }        

        return combs;
    }

Driver Code:

public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        List<List<Integer>> result = generateCombinations(arr);
        int size = result.size();
        for(int i=0;i<size;++i){
            System.out.println(result.get(i).toString());
        }
    }

Output:

[1]
[2]
[3]
[1, 3]
[4]
[1, 4]
[2, 4]
[5]
[1, 5]
[2, 5]
[3, 5]
[1, 3, 5]

Algorithm:

  • Generating combinations avoiding adjacent elements actually follows a fibonacci sequence.

Let's take array as as {1,2,3,4,5,6}

+-------------------------------------------------------------------------+---+---+---+---+---+---+
|                                                                         |   |   |   |   |   |   |
+-------------------------------------------------------------------------+---+---+---+---+---+---+
| Individual element's generated combinations excluding adjacent elements | 1 | 1 | 2 | 3 | 5 | 8 |
+-------------------------------------------------------------------------+---+---+---+---+---+---+
| Elements of the array                                                   | 1 | 2 | 3 | 4 | 5 | 6 |
+-------------------------------------------------------------------------+---+---+---+---+---+---+
  • In other words, this means that I would iterate all combinations from the start and stop at the point where it will be total combinations I could generate - 1(where -1 is excluding for itself).

  • Let's go through the combinations for better clarity. Assume, the first 2 combinations are already there.

[1]

[2]

  • So, for 3 we would go from the start like,

[1,3]

[3] (this we would add ourselves)

  • For 4, we would have all combinations in front of us as:

[1]

[2]

[1,3]

[3]

  • Now, we would only go till [2] and then stop, making combinations as:

[1,4]

[2,4]

[4] (adding this ourselves).

  • and so on so forth for upcoming elements. Hope this helps.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=146387&siteId=1