Print all subarrays of a given array

Previous
Next

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

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog