Print all subsets of an array - java

I'm trying to print all the subsets of an array. For example:
Input: nums = [1,2]
Output:
[[1], [2], [1,2], []]
I've written a recursive approach, given that all such solutions start with a condition and the program calls itself for a sub-array. But I get a ConcurrentModificationException at for(ArrayList<Integer>subset: subsets2).
How to fix the issues and convert it to a working solution?
UPDATE: I don't need a working solution. I need to fix my own code. Please work on my own code below:
import java.util.ArrayList;
import java.util.List;
public class TestPractice {
public static void main(String[] args){
int[] x = {1,2};
System.out.println(subsets(x));
}
public static List<ArrayList<Integer>> subsets(int[] nums) {
ArrayList<ArrayList<Integer>> subsets = new ArrayList<ArrayList<Integer>>();
if (nums.length ==1){
ArrayList<Integer> subset1 = new ArrayList<Integer>();
ArrayList<Integer> subset2 = new ArrayList<Integer>();
subset2.add(nums[0]);
subsets.add(subset1);
subsets.add(subset2);
return subsets;
}
int[] nums_1 = new int[nums.length-1];
for(int i = 0; i< nums.length-1; i++) {
nums_1[i]=nums[i];
}
List<ArrayList<Integer>> subsets2= subsets(nums_1);
//now add nums[n] to all subsets of nums[0:n-1]
for(ArrayList<Integer>subset: subsets2) {
ArrayList<Integer> subset1 = new ArrayList<Integer>();
subset1.add(nums[nums.length-1]);
subset1.addAll(subset);
subsets2.add(subset1);
}
return subsets2;
}
}

Don't add elements to a List when you're iterating over it. Your code can be greatly simplified by only choosing to either take or not take the current element each time. Demo
public static Set<List<Integer>> subsets(Set<List<Integer>> result, ArrayList<Integer> curr, int[] nums, int idx) {
result.add((List<Integer>)curr.clone());
if(idx < nums.length){
subsets(result, curr, nums, idx + 1);
final ArrayList<Integer> next = (ArrayList<Integer>) curr.clone();
next.add(nums[idx]);
subsets(result, next, nums, idx + 1);
}
return result;
}
public static void main(String[] args){
int[] x = {1,2};
System.out.println(subsets(new HashSet<>(), new ArrayList<>(), x, 0));
}
You can also use bitmasking. Demo
public static Set<List<Integer>> subsets(int[] nums) {
final Set<List<Integer>> result = new HashSet<>();
for(int i = 0; i < 1 << nums.length; i++){
final List<Integer> curr = new ArrayList<>();
for(int j = 0; j < nums.length; j++){
if(((i>>j)&1)==1) curr.add(nums[j]);
}
result.add(curr);
}
return result;
}
public static void main(String[] args){
int[] x = {1,2,};
System.out.println(subsets(x));
}

concurrentmodificationexception occurs when you are looping and updating the same list , so create a new list and update that list inside the loop instead of subsets2.add(subset1).
Below I have create a new list result and updating result list inside loop instead of subset2
import java.util.ArrayList;
import java.util.List;
public class TestPractice {
public static void main(String[] args){
int[] x = {1,2};
System.out.println(subsets(x));
}
public static List<ArrayList<Integer>> subsets(int[] nums) {
ArrayList<ArrayList<Integer>> subsets = new ArrayList<ArrayList<Integer>>();
if (nums.length ==1){
ArrayList<Integer> subset1 = new ArrayList<Integer>();
ArrayList<Integer> subset2 = new ArrayList<Integer>();
subset2.add(nums[0]);
subsets.add(subset1);
subsets.add(subset2);
return subsets;
}
int[] nums_1 = new int[nums.length-1];
for(int i = 0; i< nums.length-1; i++) {
nums_1[i]=nums[i];
}
List<ArrayList<Integer>> subsets2= subsets(nums_1);
List<ArrayList<Integer>> result= subsets(nums_1);
//now add nums[n] to all subsets of nums[0:n-1]
for(ArrayList<Integer>subset: subsets2) {
ArrayList<Integer> subset1 = new ArrayList<Integer>();
subset1.add(nums[nums.length-1]);
subset1.addAll(subset);
result.add(subset1);
}
return result;
}
}

It can be solved using both, Iterative and Recursive Approach
For this problem using LinkedList is faster as compared to ArrayList
Also, you don't need to handle the case when nums.length == 1
Iterative Approach
import java.util.LinkedList;
import java.util.List;
public class TestPractice {
public static void main(String[] args) {
int[] x = { 1, 2 };
System.out.println(subsets(x));
}
public static List<LinkedList<Integer>> subsets(int[] nums) {
LinkedList<LinkedList<Integer>> subsets = new LinkedList<LinkedList<Integer>>();
subsets.add(new LinkedList<Integer>());
for(int i = 0; i < nums.length; i++) {
LinkedList<LinkedList<Integer>> temp = new LinkedList<LinkedList<Integer>>();
for(LinkedList<Integer> l1: subsets) {
LinkedList<Integer> l2 = new LinkedList<Integer>(l1);
l2.add(nums[i]);
temp.add(l2);
}
subsets.addAll(temp);
}
return subsets;
}
}
Recursive Approach
import java.util.LinkedList;
import java.util.List;
public class TestPractice {
public static void main(String[] args) {
int[] x = { 1, 2 };
System.out.println(subsets(x));
}
public static List<List<Integer>> subsets(int n[]) {
List<List<Integer>> subsets = new LinkedList<List<Integer>>();
helper(n, 0, subsets, new LinkedList<Integer>());
return subsets;
}
public static void helper(int[] n, int i, List<List<Integer>> subsets, LinkedList<Integer> subset1) {
if(i == n.length) {
subsets.add(subset1);
return;
}
helper(n, i + 1, subsets, subset1);
LinkedList<Integer> subset2 = new LinkedList<Integer>(subset1);
subset2.add(n[i]);
helper(n, i + 1, subsets, subset2);
}
}

Related

Using HashSet to filter duplicates does not work in subsets with duplicate problem

The problem is "Given a list of numbers that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order."
I have some simple code to use seemly similar ways to solve this problem. Howerver, Hashset approach does not work for some test cases such as [10, 10, 1, 2, 2, 2, 10]. But sort and neighbor integer comparison approach work.
It is just different from the permutation.
This code works:
import java.util.*;
public class test {
static ArrayList<ArrayList<Integer>> subsets_with_duplicate(ArrayList<Integer> numbers) {
ArrayList<ArrayList<Integer>> res=new ArrayList<>();
Collections.sort(numbers);
helper(res, new ArrayList<>(), numbers, 0);
return res;
}
static void helper(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> slate, ArrayList<Integer> numbers, int pos){
int n=numbers.size();
res.add(new ArrayList<Integer>(slate));
//Set<Integer> set=new HashSet<>();
for(int i=pos; i<n; i++){
//if(!set.add(numbers.get(i)))
if(i!=pos&&numbers.get(i).equals(numbers.get(i-1)))
continue;
slate.add(numbers.get(i));
helper(res, slate, numbers, i+1);
slate.remove(slate.size()-1);
}
}
// Driver code
public static void main(String args[])
{
ArrayList<Integer> arr= new ArrayList<>(Arrays.asList(10, 10, 1, 2, 2, 2, 10));
for(var v:subsets_with_duplicate(arr)){
System.out.println(v);
}
}
}
This code does not work -
import java.util.*;
public class test {
static ArrayList<ArrayList<Integer>> subsets_with_duplicate(ArrayList<Integer> numbers) {
ArrayList<ArrayList<Integer>> res=new ArrayList<>();
//Collections.sort(numbers);
helper(res, new ArrayList<>(), numbers, 0);
return res;
}
static void helper(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> slate, ArrayList<Integer> numbers, int pos){
int n=numbers.size();
res.add(new ArrayList<Integer>(slate));
Set<Integer> set=new HashSet<>();
for(int i=pos; i<n; i++){
if(!set.add(numbers.get(i)))
//if(i!=pos&&numbers.get(i).equals(numbers.get(i-1)))
continue;
slate.add(numbers.get(i));
helper(res, slate, numbers, i+1);
slate.remove(slate.size()-1);
}
}
// Driver code
public static void main(String args[])
{
ArrayList<Integer> arr= new ArrayList<>(Arrays.asList(10, 10, 1, 2, 2, 2, 10));
for(var v:subsets_with_duplicate(arr)){
System.out.println(v);
}
}
}
Why?
The partial output difference between these two solutions are highlighted in red.
You are missing Collections.sort(numbers); in your last program. If you uncomment then it will show the same result as earlier. Because without the HashSet code you are sorting data but not in the second program.
static ArrayList<ArrayList<Integer>> subsets_with_duplicate(ArrayList<Integer> numbers) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
Collections.sort(numbers);
helper(res, new ArrayList<>(), numbers, 0);
return res;
}
static void helper(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> slate, ArrayList<Integer> numbers,
int pos) {
int n = numbers.size();
res.add(new ArrayList<>(slate));
Set<Integer> set = new HashSet<>();
for (int i = pos; i < n; i++) {
if (!set.add(numbers.get(i))) {
// if ((i != pos) && numbers.get(i).equals(numbers.get(i - 1))) {
continue;
}
slate.add(numbers.get(i));
helper(res, slate, numbers, i + 1);
slate.remove(slate.size() - 1);
}
}
// Driver code
public static void main(String args[]) {
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(10, 10, 1, 2, 2, 2, 10));
for (var v : subsets_with_duplicate(arr)) {
System.out.println(v);
}
}

How do I add two arraylists with an uneven amount of elements

I am trying to get the sum of two array lists elements with a different amount of elements in each one is a method. So far, I have been able to get the elements to add, but when the results print, it leaves out numbers because the array lists have different elements. Let's say array list a has 5 elements and array list b has 3 elements. The missing amounts in the array are supposed to be 0. So if a has {1, 2, 3, 4, 5} and b has {2, 3, 1}. The result should be {3, 5, 4, 4, 5}.
public static ArrayList<Integer> sum(ArrayList<Integer> a, ArrayList<Integer> b)
{
ArrayList <Integer> result = new ArrayList <Integer> ();
for(int i = 0; i < a.size(); i++) {
result.add(a.get(i) + b.get(i));
}
return result;
}
Take two lists add each elements one by one and store there sum in new list if there is no more element in any of the list take the remaining elements from the larger list and append at the end of the new list.
import java.util.*;
public class MainSum{
public static void main(String ... $){
var out = System.out;
List<Integer> a = new ArrayList<>(List.of(1, 2, 3, 4, 5));
List<Integer> b = new ArrayList<>(List.of(2, 3, 1));
List<Integer> result = MainSum.sum(a, b);
out.println(result);
}
public static List<Integer> sum(List<Integer> a, List<Integer> b){
List<Integer> result = new ArrayList<>();
for(int index =0 ;index<a.size() && index< b.size();index++)
result.add(a.get(index) + b.get(index));
if( a.size() != b.size())
result.addAll(a.size()>b.size()?a.subList(b.size(), a.size()):b.subList(a.size(), b.size()));
return result;
}
}
Output:
[3, 5, 4, 4, 5]
Note that you have 2 arrays in different sizes, so you should take it into consideration and your loop can't be always on a size, since it might be bigger than b size and then you try to access elements that aren't exist.
Something you can do is to loop over elements which are exist in both arrays, and then complete the rest. Suggestion:
public static ArrayList<Integer> sum(ArrayList<Integer> a, ArrayList<Integer> b) {
ArrayList result = new ArrayList();
int aSize = a.size();
int bSize = b.size();
for (int i = 0; i < Math.min(aSize, bSize); i++) { // Loop over elements that exist in both arrays
result.add(a.get(i) + b.get(i));
}
if (aSize > bSize) { // Complete missing from a
for (int i = aSize - bSize + 1; i < aSize; i++) {
result.add(a.get(i));
}
} else if (bSize > aSize) { // Complete missing from b
for (int i = bSize - aSize + 1; i < bSize; i++) {
result.add(b.get(i));
}
}
return result;
}
You have to make sure to iterate to the higher index, and make sure you don't run into a problem with a non existing index.
BiFunction<ArrayList<Integer>, Integer, Integer> getSafe =
(l, i ) -> i < l.size() ? l.get(i) : 0;
for (int i = 0; i < Math.max(list1.size(), list2.size(); i++) {
result.add(getSafe.apply(list1) + getSafe.apply(list1));
}
Or with an Iterator if you want to overengineer
Iterator<Integer> bothIterator = new Iterator<>() {
Iterator<Integer> it1 = list1.iterator();
Iterator<Integer> it2 = list2.iterator();
public boolean hasNext() {
return it1.hasNext() || it2.hasNext();
}
public Integer next() {
int result = it1.hasNext() ? it1.next();
result += it2.hasNext() ? it2.next() : 0;
return result;
}
}
int result = 0;
while (bothIterator.hasNext()) {
result += bothIterator.next();
}
Another approach can be based on customizing each list return values.
public class TestArrSum {
public static void main(String[] args)
{
List<Integer> a = List.of(1,2,3,4,5);
List<Integer> b = List.of(2,3,1);
List<Integer> result = sum(a,b);
result.forEach(System.out::println);
}
public static List<Integer> sum(List<Integer> a, List<Integer> b)
{
List<Integer> result = new ArrayList<Integer>();
for(int i=0;i<a.size() || i<b.size();i++)
{
result.add(getValue(a,i)+getValue(b,i));
}
return result;
}
public static int getValue(List<Integer> l,int i)
{
try
{
return l.get(i);
}
catch(ArrayIndexOutOfBoundsException e)
{
return 0;
}
}
}
Note: One advantage can be in terms of verbosity in case if need that method signature to contain more lists to be sum.
The same result can be obtained also with an intermediary data structure which latter will be transformed to List.
static class MyMap extends HashMap<Integer,Integer>
{
private static final long serialVersionUID = 1L;
MyMap()
{
super();
}
public void putKV(Integer key,Integer value)
{
Integer old = this.get(key);
if(old==null)
{
this.put(key, value);
}
else
{
this.put(key, old+value);
}
System.out.println(key+":"+value+":"+this.get(key));
}
public List<Integer> toList()
{
List<Integer> l = new ArrayList<Integer>();
for(int i=0; i<this.size();i++)
{
l.add(this.get(i));
}
return l;
}
}
Can be used as:
MyMap m = new MyMap();
for(int i=0;i<a.size();i++)
{
m.putKV(i, a.get(i));
}
for(int i=0;i<b.size();i++)
{
m.putKV(i, b.get(i));
}
List<Integer> resultM = m.toList();
resultM.forEach(k->System.out.println(k));

Return ArrayList of k-Subsequent of n-Array in recursive function

I have the following code that prints all k-subsequent of array of size n
import java.util.*;
public class Main {
public static void main(String[] args){
int[] arr = {1,2,3,4,5,6};
int k = 3;
int start= 0;
List<int[]> subs = subseq(arr, k, 0, new int[k]);
for(int[] s : subs)
System.out.println(Arrays.toString(s));
}
static List<int[]> subseq(int[] arr, int len, int start, int[] result){
List<int[]> rez = new ArrayList<>();
if (len == 0){
/* PRINT HERE WORKS FINE IT PRINT EACH SUBSEQUENT CORRECTLY*/
//System.out.println(Arrays.toString(result));
rez.add(result);
return rez;
}
for (int i = start; i <= arr.length-len; i++){
result[result.length - len] = arr[i];
subseq(arr, len-1, i+1, result);
}
return rez;
}
}
My issue is with rez List that is returned in recursive function, It always empty, can any one help fixing it ? I tried many solutions none worked
Basically, you were creating new arraylist to store all the sequences .You need to create a single result arraylist which will be storing all the arrays. While adding to res , i have created new array so that it does not replace the previous arrays that are added. Below is the working solution
import java.util.*;
public class Main {
public static void main(String[] args){
int[] arr = {1,2,3,4,5,6};
int k = 3;
int start= 0;
Main obj = new Main();
List<int[]> rez = new ArrayList<>();
obj.subseq(arr, k, start, new int[k], rez);
for(int[] s : rez)
System.out.println(Arrays.toString(s));
}
public void subseq(int[] arr, int len, int start,
int[] result, List<int[]>
res){
if (len == 0){
/* PRINT HERE WORKS FINE IT PRINT EACH SUBSEQUENT CORRECTLY*/
// System.out.println(Arrays.toString(result));
int[] a = new int[result.length];
for(int i = 0; i < result.length; i++) {
a[i] = result[i];
}
res.add(a);
return;
}
for (int i = start; i <=arr.length-len; i++){
result[result.length - len] = arr[i];
subseq(arr, len-1, i+1, result,res);
}
}
}

Arraylist Insertion-Sort

How do you sort an arraylist using insertion? Here is my arraylist it is filled with random Integers:
ArrayList<Integer> oh = new ArrayList<Integer>();
Random random = new Random();
for (int i = 0; i < 100; i++)
{
oh.add(random.nextInt());
}
Try this can help you to solve problem, sorting Arraylist with insertion sort.
public static void main(String args[]) {
ArrayList<Integer> oh = new ArrayList<Integer>();
Random random = new Random();
for (int i = 0; i < 100; i++) {
oh.add(random.nextInt());
}
int[] elementlist = new int[oh.size()];
Iterator<Integer> iter = oh.iterator();
for (int j = 0; iter.hasNext(); j++) {
elementlist[j] = iter.next();
}
int[] list = insertionSort(elementlist);
for (Integer in : list) {
System.out.println(in);
}
}
public static int[] insertionSort(int[] list) {
for (int i = 1; i < list.length; i++) {
int next = list[i];
// find the insertion location while moving all larger element up
int j = i;
while (j > 0 && list[j - 1] > next) {
list[j] = list[j - 1];
j--;
}
// insert the element
list[j] = next;
}
return list;
}
For more info go in link
import java.util.ArrayList;
public class InsertionSort {
private static ArrayList<Integer> inputArray = new ArrayList<Integer>();
public static ArrayList<Integer> getInputArray() {
return inputArray;
}
public InsertionSort(ArrayList<Integer> inputArray){
InsertionSort.inputArray = inputArray;
}
public void sortGivenArray(){
for(int i=1;i<inputArray.size();i++){
int key = inputArray.get(i);
for(int j= i-1;j>=0;j--){
if(key<inputArray.get(j)){
inputArray.set(j+1,inputArray.get(j));
if(j==0){
inputArray.set(0, key);
}
}else{
inputArray.set(j+1,key);
break;
}
}
}
}
}
import java.util.ArrayList;
import daa.InsertionSort;
public class MainPractise implements Cloneable {
public static void main(String[] args) {
ArrayList<Integer> unsortedArray = new ArrayList<Integer>();
unsortedArray.add(8);
unsortedArray.add(7);
unsortedArray.add(6);
unsortedArray.add(5);
unsortedArray.add(4);
unsortedArray.add(0);
unsortedArray.add(2);
InsertionSort is = new InsertionSort(unsortedArray);
System.out.println("---------Initial Unsorted Array---------");
for(int i:InsertionSort.getInputArray()){
System.out.print(i+" ");
}
is.sortGivenArray();
System.out.println("\n------------Sorted Array------------");
for(int i:InsertionSort.getInputArray()){
System.out.print(i+" ");
}
}
}

Find all combinations of n numbers of a given set of numbers

i want to write a app to find all possible combinations of n numbers of a given set of numbers and return a hashset of them..
for example if a give set{1,5,7,9} and size of set 2 then i will take:
[1, 5] [1, 7] [1, 9] [5, 7] [5, 9]
i have the following code from this post but i can't modified it for a given size of set every time.
import java.util.ArrayList;
import java.util.Arrays;
class SumSet {
static void sum_up_recursive(ArrayList<Integer> numbers,ArrayList<Integer> partial) {
System.out.println("sum("+Arrays.toString(partial.toArray())+")=");
for(int i=0;i<numbers.size();i++)
{
ArrayList<Integer> remaining = new ArrayList<Integer>();
int n = numbers.get(i);
for (int j=i+1; j<numbers.size();j++)
remaining.add(numbers.get(j));
ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
partial_rec.add(n);
sum_up_recursive(remaining,partial_rec);
}
}
static void sum_up(ArrayList<Integer> numbers) {
sum_up_recursive(numbers,new ArrayList<Integer>());
}
public static void main(String args[]) {
Integer[] numbers = {1,5,7,9};
sum_up(new ArrayList<Integer>(Arrays.asList(numbers)));
}
}
thank you all...
Also i found this solution
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class Comb {
static HashSet combine(Integer[] arr, int k, int startId, int[] branch, int numElem,HashSet arrSet)
{
if (numElem == k)
{
//System.out.println("k: "+k+(Arrays.toString(branch)));
ArrayList<Integer> mySet = new ArrayList<Integer>();
for(int i=0;i<branch.length;i++)
{
mySet.add(branch[i]);
}
arrSet.add(mySet);
return arrSet;
}
for (int i = startId; i < arr.length; ++i)
{
branch[numElem++]=arr[i];
combine(arr, k, ++startId, branch, numElem, arrSet);
--numElem;
}
return arrSet;
}
public static void main(String args[])
{
int k = 3;
Integer[] input ={1,5,7,9}; "ABCD".toCharArray();
int[] branch = new int[k];//{0,0};//new char[k];
HashSet arrSet=new HashSet();
arrSet=combine(input, k, 0, branch, 0, arrSet);
}
}
I changed recursive method from niiraj874u's answer to return a value. So now it gives list of hash sets as you wanted.
static List<Set<Integer>> sum_up_recursive(List<Integer> numbers,
Set<Integer> partial , int sizeOfset) {
List<Set<Integer>> result = new ArrayList<Set<Integer>>();
if(partial.size() == sizeOfset)
result.add(partial);
for (int i = 0; i < numbers.size(); i++) {
ArrayList<Integer> remaining = new ArrayList<Integer>();
int n = numbers.get(i);
for (int j = i + 1; j < numbers.size(); j++)
remaining.add(numbers.get(j));
Set<Integer> partial_rec = new HashSet<Integer>(partial);
partial_rec.add(n);
result.addAll(sum_up_recursive(remaining, partial_rec, sizeOfset));
}
return result;
}
public static void main(String args[]) {
Integer[] numbers = { 1, 5, 7, 9 };
int size = 2;
List<Set<Integer>> allCombinations = sum_up_recursive(Arrays.asList(numbers), new HashSet<Integer>(), size);
for (Set<Integer> set : allCombinations) {
for (Integer num : set)
System.out.print(num + " ");
System.out.println();
}
}
package com.app.atb;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class Challenge {
public static void main(String[] args) {
Set<Integer> myset = new HashSet<Integer>();
myset.add(1);
myset.add(5);
myset.add(7);
myset.add(9);
List<Set<Integer>> achieved = solveChallenge(myset);
System.out.println(achieved);
}
public static List<Set<Integer>> solveChallenge(Set<Integer> myset) {
int n = myset.size();
Integer[] myInts = new Integer[n];
Iterator<Integer> iterator = myset.iterator();
int index = 0;
while (iterator.hasNext()) {
myInts[index] = iterator.next();
++index;
}
List<Set<Integer>> myList = new ArrayList<Set<Integer>>();
Set<Integer> subSet;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (j != i) {
subSet = new HashSet<Integer>();
subSet.add(myInts[i]);
subSet.add(myInts[j]);
myList.add(subSet);
}
}
}
return myList;
}
}
We can add one more parameter sizeOfset in SumSet.sum_up_recursive(ArrayList<Integer>, ArrayList<Integer>, int) method like below. please run it tell me Does it give the output what you want ?
import java.util.ArrayList;
import java.util.Arrays;
public class SumSet {
static void sum_up_recursive(ArrayList<Integer> numbers,
ArrayList<Integer> partial , int sizeOfset) {
if(partial.size() == sizeOfset)
{
System.out.println("sum(" + Arrays.toString(partial.toArray()) + ")=");
}
for (int i = 0; i < numbers.size(); i++) {
ArrayList<Integer> remaining = new ArrayList<Integer>();
int n = numbers.get(i);
for (int j = i + 1; j < numbers.size(); j++)
remaining.add(numbers.get(j));
ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
partial_rec.add(n);
sum_up_recursive(remaining, partial_rec, sizeOfset);
}
}
static void sum_up(ArrayList<Integer> numbers) {
sum_up_recursive(numbers, new ArrayList<Integer>(), 2);
}
public static void main(String args[]) {
Integer[] numbers = { 1, 5, 7, 9 };
sum_up(new ArrayList<Integer>(Arrays.asList(numbers)));
}
}

Categories