
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Find the Number Of Subarrays Having Sum in a Given Range in C++
- Maximum subarray sum after dividing array into subarrays based on the given queries in Java
- JavaScript Return an array that contains all the strings appearing in all the subarrays
- Find the Number Of Subarrays Having Sum in a Given Range using C++
- How to find the sum of all elements of a given array in JavaScript?
- Java program to print all distinct elements of a given integer array in Java
- Program to find sum of all odd length subarrays in Python
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- How do you find the sum of all the numbers in a java array
- Find Sum of all unique subarray sum for a given array in C++
- Find all good indices in the given Array in Python\n
- Find the sum of maximum difference possible from all subset of a given array in Python
- Sum of XOR of all subarrays in C++
- Convert array into array of subarrays - JavaScript
