Vaibhav Jha
Vaibhav Jha

Reputation: 37

Return Subset of an array in form of a matrix [JAVA]

Given an integer array (of length n), find and return all the subsets of input array. So I have solved this problem but I'm not sure about the underlying concept at work here.

Here is the correct solution.

public static int[][] subsets(int input[]) {
              
        return subsets(input,0);
    }
    private static int[][] subsets(int input[], int index){
        if(index == input.length){
            int[][] a = new int[1][0];
            return a;
        }
        
        int[][] smallAns = subsets(input,index+1);
        int[][] ans = new int[2*(smallAns.length)][];
        
        for(int i = 0 ; i < smallAns.length ; ++i){
            
                ans[i] = smallAns[i];
            
        }
        for(int i = 0 ; i < smallAns.length ; ++i){
            ans[i+smallAns.length] = new int[smallAns[i].length+1];
        }
        
        for(int i = 0 ; i < smallAns.length ; ++i){
            for(int j = 0 ; j < ans[i+smallAns.length].length ; ++j){
                if(j == 0){
                    ans[i+smallAns.length][j] = input[index];
                }
                else{
                    ans[i+smallAns.length][j] = smallAns[i][j-1];
                }
            }
        }
        
        return ans;
    }

And this was my original solution:

    public static int[][] subsets(int input[]) {
              
        return subsets(input,0);
    }
    private static int[][] subsets(int input[], int index){
        if(index == input.length){
            int[][] a = new int[1][1];
            return a;
        }
        
        int[][] smallAns = subsets(input,index+1);
        int[][] ans = new int[2*(smallAns.length)][];
        
        for(int i = 0 ; i < smallAns.length ; ++i){
            
                ans[i] = smallAns[i];
            
        }
        for(int i = 0 ; i < smallAns.length ; ++i){
            ans[i+smallAns.length] = new int[smallAns[i].length+1];
        }
        
        for(int i = 0 ; i < smallAns.length ; ++i){
            for(int j = 0 ; j < ans[i+smallAns.length].length ; ++j){
                if(j == 0){
                    ans[i+smallAns.length][j] = input[index];
                }
                else{
                    ans[i+smallAns.length][j] = smallAns[i][j-1];
                }
            }
        }
        
        return ans;
    }

My original(wrong code the second one) code added 0 at the end of all the subsets. Like this enter image description here

The correct code given at the beginning solves the problem. The only difference between those two codes is that first's base case returns a matrix of 1X1 (wrong one) and the second's base case returns a matrix of 1X0 (correct one) .But I'm curious about returning a 1X0 matrix at the base case. Could someone please explain how is not throwing nullPointerExceptions when i'm trying to access the length of column, etc.

Upvotes: 0

Views: 300

Answers (1)

ownerofglory
ownerofglory

Reputation: 19

Look at the if statement in your code

if(index == input.length){
            int[][] a = new int[1][1];
            return a;
}

You initialise the variable a with a two-dimensional array (array of arrays), so to speak matrix with 1 row and 1 column. Since a[0][0] is not assigned any value it's given a default value for int data type 0.

The variable a should be initialised with an array of array of 0 length instead

int[][] a = new int[1][0];

Upvotes: 1

Related Questions