# Print all subarrays of a given array

If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs.

In this post, we will see how to print all subarrays of given array.

## Problem

Print all print all subarrays of given array.
For example:

If array is {1,2,3}
then you need to print
{1}, {2}, {3}, {1,2}, {2,3}, {1,2,3}

## Solution

If there are n elements in the array then there will be (n*n+1)/2 subarrays.
Here is a simple algorithm for it.

• We will use three loop to print subarrays.
• Outer loop will be used to get start index
• First inner loop will be used to get end index
• Second inner loop will be used to print element from start to end index.

here is simple program to print all subarrays of given array..

That’s all about how to print all subarrays of a given array

import_contacts

### You may also like: ## Related Posts

• 18 June

### Maximum Number of Vowels in a Substring of Given Length

Table of ContentsApproach – 1 Generate All Substrings Using substring() MethodApproach – 2 Using Sliding Window Method (Linear Time Solution) In this article, we will look at an interesting problem related to the Strings and [Sliding-Window Algorithm](https://java2blog.com/sliding-window-maximum-java/ “Sliding-Window Algorithm”). The problem is : "Given a String we have to Find the Maximum Number of Vowel […]

• 04 June

### Search for a range Leetcode – Find first and last position of element in sorted array

Table of ContentsApproach 1 (Using Linear Search)Approach 2 (Using Modified Binary Search-Optimal) In this article, we will look into an interesting problem asked in Coding Interviews related to Searching Algorithms. The problem is: Given a Sorted Array, we need to find the first and last position of an element in Sorted array. This problem is […]

• 30 April

### Convert Postfix to Infix in Java

Learn about how to convert Postfix to Infix in java.

• 30 April

### Convert Prefix to Postfix in Java

Learn about how to convert Prefix to Postfix in java.

• 16 April

### Data Structures in java

• 