2016-08-06

Write a Programs to Generate Maximum non-empty subarray of an array

Given Array A of size N  find those non-empty subarrays  (sequence of consecutive elements)

Sample:
Array    [ 1 ,1 ,3 ]

Output:
1
1
3
1  1
1  3
1  1  3

Solution:

  1. class TestClass {
  2.     public static void main(String args[] ) throws Exception {
  3.      int arr[] = {1 ,1 3};
  4.      subArray();
  5.     }
  6.     public static void subArray(int arr[], int n)
  7.     {
  8.     for (int i=0; i <n; i++)
  9.     {
  10.         for (int j=i; j<n; j++)
  11.         {
  12.             for (int k=i; k<=j; k++){
  13.                 System.out.println(arr[k]);
  14.             }
  15.         System.out.println();
  16.         }
  17.      }
  18.     }
  19. }

No comments:

Post a Comment