0

The following code for above requirement. However I am not getting proper output. There is problem with input list that I am passing in recursion

import java.util.ArrayList;
import java.util.Collections;

public class abc
{
    
    public static void m(ArrayList<Integer> op, ArrayList<Integer> ip) {
       if(ip.size()==0) {
           System.out.println(op);
           return;
       }
       ArrayList<Integer> l1=new ArrayList<Integer>();
       ArrayList<Integer> l2=new ArrayList<Integer>();
       l1.addAll(op);
       l2.addAll(op);
       l1.add(ip.get(0));
       ip.remove(0);
       m(l2,ip);
       m(l1,ip);
    }

    public static void main(String[] args) {
        Integer [] z = {1,3,2};
        ArrayList<Integer> ip=new ArrayList<Integer>();
        Collections.addAll(ip, z);
        ArrayList<Integer> op=new ArrayList<Integer>();
        m(op,ip);
    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • Can you clarify your program requirements? When you run it with input `{1, 3, 2}`, what output do you see? What is the expected output? – Kaan Jun 12 '22 at 18:16
  • There is an algorithm for generating all subsequences of a string using recursion here: https://www.geeksforgeeks.org/recursive-program-to-generate-power-set/ With minimal adjustment you can make it work for your situation. – Edd Jun 12 '22 at 18:33
  • i guess you want an output like: {1} {1,3} {1,3,2} to achieve this you have to go throw the list and not print the list object at once, cause this will only print the default toString return value of the ArrayList-class what got me wondering is that you have two new arraylists in the method m and call m twice in it!? what to you want to achieve with this? – Sebastian Jun 12 '22 at 19:38
  • @kaan I want output as {}{2},{3},{3,2},{1},{1,2}, {1,3},{1,3,2}. But output is {},{2},{3},{1}. Logic is perfectly right and hence getting first 3 outputs right. But after that there is problem with input list ip. It is keeping elements of previous function call in it. – Sourabh Gayake Jun 13 '22 at 19:40
  • @Sebastian I want output as {}{2},{3},{3,2},{1},{1,2}, {1,3},{1,3,2}. But output is {},{2},{3},{1}. I have used 2 list to achieve tree where I choose one element in one list and dont choose that element in other list. – Sourabh Gayake Jun 13 '22 at 19:42
  • @Edd I have checked that code. My logic is also perfectly right. But problem is I want output as {}{2},{3},{3,2},{1},{1,2}, {1,3},{1,3,2}. But output is {},{2},{3},{1}. I am getting first 3 outputs right. But after that there is problem with input list ip. It is keeping elements of previous function call in it. – Sourabh Gayake Jun 13 '22 at 19:44

1 Answers1

0

The problem is not, like you said in your comment, that the list ip keeps elements of the previous function call. The problem is that you don't create a new arraylist as ip parameter for the next function call, meaning, as it is call by reference, that you remove the elements of one function call already in the other function call. You can fix this by just creating a new array list for the new function call, like you already to for op. However you could also use this:

 m(l2,new ArrayList<>(ip));
 m(l1,new ArrayList<>(ip));

so you don't have to use an extra addAll() ;)

Sebastian
  • 153
  • 1
  • 10
  • thank you for helping me, I got correct output with changes that you suggested. But there one doubt. I have made same code with String. Here I am not facing that old problem. Why is it so? What is difference between String and List? String l1=op; String l2=op; l2+=ip.charAt(0); ip=ip.substring(1); m(ip,l1); m(ip,l2); – Sourabh Gayake Jun 13 '22 at 22:02
  • String is an immutable object, that means that you can't change it's value. So if you habe a string "xyz" and remove the first char, rhere is a new string object created. You can see this already in your code, as you use ip = ip.substring(1), would it be a list (mutable) you only need to use list.remove(0); – Sebastian Jun 14 '22 at 04:03
  • If you're happy with my answer you can accept it and upvote it ;) – Sebastian Jun 14 '22 at 08:29