Find All the Subarrays of a Given Array in Java


An array is a linear data structure in which elements are stored in contiguous memory locations.

As per problem statement, we have to find all the subarrays of a given array. Subarrays are part or a section of an array. When we talk about all subarrays of an array, we talk about the total number of combinations that can be made using all the elements of the array without repeating.

Let’s explore the article to see how it can be done by using Java programming language.

To Show you Some Instances

Instance-1

Suppose we have the below array

[10, 2, 3, -5, 99, 12, 0, -1]

The subarrays of this array would be

10
10 2
10 2 3
10 2 3 -5
10 2 3 -5 99
10 2 3 -5 99 12
10 2 3 -5 99 12 0
10 2 3 -5 99 12 0 -1
2
2 3
2 3 -5
2 3 -5 99
2 3 -5 99 12
2 3 -5 99 12 0
2 3 -5 99 12 0 -1
3
3 -5
3 -5 99
3 -5 99 12
3 -5 99 12 0
3 -5 99 12 0 -1
-5
-5 99
-5 99 12
-5 99 12 0
-5 99 12 0 -1
99
99 12
99 12 0
99 12 0 -1
12
12 0
12 0 -1
0
0 -1
-1

Instance-2

Suppose we have the below array

[55,10,29,74]

The subarrays of this array would be

55
55 10
55 10 29
55 10 29 74
10
10 29
10 29 74
29
29 74
74

Algorithm

Algorithm-1

  • Step 1 − After storing the array, run a for loop from 0 to n. This will mark our starting point of the main array.

  • Step 2 − Run another for loop that runs from the first iterator to the ending point of the main array.

  • Step 3 − Now run another loop that traverses the elements between both two iterators.

  • Step 4 − Print the elements in a sequential order.

Algorithm-2

  • Step 1 − After storing the array, check if we have reached the end, then go out of the function.

  • Step 2 − If the start index is greater than the end index, then call the function itself from 0 to end+1.

  • Step 3 − Else print the array elements between the indices inside a for loop, and call the function again from start+1 to end.

  • Step 4 − Exit.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length

Below refers to the syntax of it −

array.length

where, ‘array’ refers to the array reference.

You can use Arrays.sort() method to sort the array in ascending order.

Arrays.sort(array_name);

Multiple Approaches

We have provided the solution in different approaches.

  • By Using for Loops

  • By Using Recursion

Let’s see the program along with its output one by one.

Approach-1: By Using for Loops

In this approach, we will use three for loops to find the subarrays of an array. The first loop to mark the start and the second to mark the end of the subarray, while the third prints the subarray.

Example

import java.io.*;
public class Main {
   public static void main(String[] args) {
      
      // The array elements
      int arr[] = { 10, 2, 3, 99, 12, 0 };
      System.out.println("The subarrays are-");
      
      // For loop for start index
      for (int i = 0; i < arr.length; i++)
      
      // For loop for end index
      for (int j = i; j < arr.length; j++) {
      
         // For loop to print subarray elements
         for (int k = i; k <=j; k++)
            System.out.print(arr[k] + " ");
            System.out.println("");
      }
   }
}

Output

The subarrays are-
10 
10 2 
10 2 3 
10 2 3 99 
10 2 3 99 12 
10 2 3 99 12 0 
2 
2 3 
2 3 99 
2 3 99 12 
2 3 99 12 0 
3 3 99 3 99 12 3 99 12 0 
99 
99 12 
99 12 0 
12 12 0 
0 

Approach-2: By Using Recursion

In this approach, we find all the subarrays but using recursion.

Example

import java.io.*;
public class Main {
   //main method
   public static void main(String[] args) {
      // The array elements
      int arr[] = { 10, 2, 3};
      System.out.println("The subarrays are-");
         
      // Calling the recursive function
      printSubArrays(arr, 0, 0);
   }
   
   // Recursive FUnction to Find all the subarrays
   static void printSubArrays(int[] arr, int head, int tail) {
   
      // Exits the function if we have reached the end
      if (tail == arr.length)
         return;
      
      // Increases the first index and calls itself
      else if (head > tail)
         printSubArrays(arr, 0, tail + 1);
      
      // Print the subarray and then increases the first element index
      else {
         for (int i = head; i < tail; i++)
            System.out.print(arr[i] + " ");
         System.out.println(arr[tail]);
         printSubArrays(arr, head + 1, tail);
      }
      return;
   }
}

Output

The subarrays are-
10
10 2
2
10 2 3
2 3
3

In this article, we explored how to find all the subarrays of a given array by using Java programming language.

Updated on: 22-Oct-2023

37K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements