← Back to questions
TCS NQT• Prep

Sub Arrays

Here is the optimized solution in Java. Copy or view it below.
Java
import java.util.*;

public class SubArrays {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter the number of elements");
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.println("The subarrays are-");
        for (int i = 0; i < arr.length; i++)
            for (int j = i; j < arr.length; j++) {
                for (int k = i; k <= j; k++)
                    System.out.print(arr[k] + " ");
                System.out.println("");
            }
    }

}