1

I am a beginner in Java programming and I am trying to print the subarrays in a given array using the concept of recursion.

I am getting my desired result but I am also getting some runtime errors.

My code:

import java.util.Scanner;

public class GFG {
    public static void printArrays(int[] arr, int start,int end,int n){
        for (int i = start; i <= end; i++){
            System.out.print(arr[i]+"");
        }
        System.out.print("\n");
    }
    public static void subArrays(int[] arr, int start, int end,int n) {
        if (end < n) {
            printArrays(arr, start, end, n);
            subArrays(arr, start, end + 1, n);
        }

        if (end >= n) {
            subArrays(arr, start + 1, start + 1, n);
        }

        if (start >= n) {
            return;
        }
    }

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
        subArrays(arr, 0, 0, n);
    }
}

The error that I'm getting is this: 


Exception in thread "main" java.lang.StackOverflowError
    at GFG.subArrays(GFG.java:16)
    at GFG.subArrays(GFG.java:16)
    at GFG.subArrays(GFG.java:16)

The 16th line of code is "subArrays(arr,start+1,start+1,n);"

My output is this:

1
12
123
2
23
3
5
  • This means you're missing an end condition that will stop the recursion. Either stepping through the code, or (my preference) "playing computer" with paper and pencil, will help you to understand what's happening. Commented Mar 26, 2020 at 16:24
  • yes, I understand. But I cannot understand what I am missing. Commented Mar 26, 2020 at 16:26
  • Then step through it again; it's short enough it won't take long. Commented Mar 26, 2020 at 17:11
  • Recursion, once understood, will become much more intuitive. IMO paper/pencil or stepping through is the fastest way to understand it deeply--and once you do you'll find it in a lot of places. It's not suitable for everything, but when it is, it's like magic. Commented Mar 26, 2020 at 17:26
  • 1
    I get what you mean. I used to struggle a LOT with recursion, so I decided to start from the very basics and go ahead. I solved some questions with pen and paper for every stage. I then tried to solve questions with my intuition and I have started getting results! Commented Mar 26, 2020 at 18:02

1 Answer 1

1

Your problem is because you are checking the condition for return (i.e. the value of start) at the end but before that, you are already calling printArrays(arr, start, end, n);, subArrays(arr, start + 1, start + 1, n); and subArrays(arr, start, end + 1, n); where the index is going beyond the limit. Put the following check at the beginning of subArrays:

if (start >= n) {
    return;
}

as follows:

public static void subArrays(int[] arr, int start, int end, int n) {
    if (start >= n) {
        return;
    }
    if (end < n) {
        printArrays(arr, start, end, n);
        subArrays(arr, start, end + 1, n);
    }    
    if (end >= n) {
        subArrays(arr, start + 1, start + 1, n);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This removed the error. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.