1

enter image description here

I was trying to solve this question using this simple recursive and backtracking approach.My idea was this:

code

import java.util.*;

public class Main
{

    static void generate(String s, String ans , int r)
    {
        if(ans.length()-1==r)
        {
            System.out.print(ans);

            return;
        }

        char ch=s.charAt(0);

        for(int i=1;i<=s.length()-1;i++)
        {
            String ros=s.substring(i);

            generate(ros,ans+ch,r);
        }
    }

    public static void main(String[] args) {
        String a="123";
        Scanner sc=new Scanner(System.in);
        int r=sc.nextInt();
        String ans=" ";
        generate(a,ans,r);
    }
}

my output with string 123 : 12

Required output : 12 13 23

I hope my recursive tree is right but I am not able to code it. My code seems to have some major issues

BeUndead
  • 3,463
  • 2
  • 17
  • 21
  • By 'combinations', do you specifically mean 'of length 2'? 'Cause there's '', '1', '2', '3', '123' which are combinations of those not included in your intended output. – BeUndead Mar 05 '21 at 13:10
  • 1
    Yes, I mean all the combinations of length 2 – ANSHUMAN MISHRA Mar 05 '21 at 13:44
  • And to check, does input with repeated characters count as different combinations. For example, for '111' as input, would you want '11', '11', '11' as output, or simply '11'? – BeUndead Mar 05 '21 at 14:13

2 Answers2

2

if my understanding is right, input "12345" and 3, output should be

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

then this is my program

import java.util.*;


public class Main{
    static void generate(String s, int r){
        recursiveGenerate(s, new LinkedList<>(), r, 0);
    }

    static void recursiveGenerate(String s, Deque<Character> cache, int len, int start){
        if(cache.size() == len){
            System.out.println(Arrays.toString(cache.toArray()));
        }
        else{
            for(int i = start; i < s.length(); i++){
                cache.addLast(s.charAt(i));
                recursiveGenerate(s, cache, len, i + 1);
                cache.removeLast();
            }
        }
    }

    public static void main(String[] args){
        String a = "12345";
        int r = 3;
        generate(a, r);
    }
}
Nathan Drake
  • 71
  • 1
  • 6
0
Here is different code using ArrayList but using same logic as **Nathan Drake** :

import java.util.*;

public class Main

{

    public static void PrintAllCombinations(int[] arr, int r) {
List<Integer> list = new ArrayList<Integer>();
PrintAllCombinationsUtils(arr, r, list, 0);
}

private static void PrintAllCombinationsUtils(int[] arr, int r, List<Integer> list, int start) {

if (list.size() >= r) {

System.out.println(list);

return;

}

for (int i = start; i < arr.length; i++) {

list.add(arr[i]);

PrintAllCombinationsUtils(arr, r, list, i + 1);

list.remove(list.size() - 1);

}

}

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        int r=sc.nextInt();

        int arr[]=new int[5];

        for(int i=0;i<arr.length;i++)

        {
            arr[i]=sc.nextInt();

        }

        PrintAllCombinations( arr, r);

    }
}