Check a Subarray is Formed by Consecutive Integers from a Given Array in Java


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to check if a sub array is formed by consecutive integers from a given array of integers. The sub array refers to some segment of the array from an array. Here it has been told to check the subarray formed by consecutive integers elements i.e., the array elements are a set of continuous numbers where we can get one after another element.

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 the original array is {8, 0, 2, 4, 1, 3, 1, 9, 5, 11}
After finding the sub array the result will be:
The largest subarray is from index [1, 5]
The elements of subarray are: 0 2 4 1 3

Instance-2

Suppose the original array is {9, 7, 3, 2, 0, 2, 18, 0}
After finding the sub array the result will be:
The largest subarray is from index [2, 3]
The elements of subarray are: 3 2

Instance-3

Suppose the original array is {0, 2, 1, 3, 5, 4, 5, 11}
After finding the sub array the result will be:
The largest subarray is from index [0, 5]
The elements of subarray are: 0 2 1 3 5 4

Algorithm

  • Step-1 − Declare and initialize an integer array.

  • Step-2 − For loop to denote start and end of the sub array.

  • Step-3 − Now check if the sub array is consecutive.

  • Step-4 − Print the elements of the sub array.

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.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array

In this approach, array elements will be initialized in the program. Then as per the algorithm check if a sub array is formed by consecutive integers from a given array of integers.

Example

import java.lang.*;
public class Main {
   public static void main (String[] args) {
      int[] A = { 0, 12, 14, 13, 45, 41, 5, 11 };
      int len = 1;
      int start = 0, end = 0;
      for (int i = 0; i < A.length - 1; i++) {
         int min_val = A[i], max_val = A[i];
         for (int j = i + 1; j < A.length; j++) {
            min_val = Math.min(min_val, A[j]);
            max_val = Math.max(max_val, A[j]);
            if (isConsecutive(A, i, j, min_val, max_val)) {
               if (len < max_val - min_val + 1) {
                   len = max_val - min_val + 1;
                   start = i;
                   end = j;
               }
            }
         }
      }
      System.out.println("The largest subarray is from index ["+start + "," + end + "]");
      System.out.print("The elements of subarray are: ");
      for (int x = start; x <= end; x++) {
         System.out.print(A[x]+" ");
      }
   }
   private static boolean isConsecutive(int[] A, int i, int j, int min, int max) {
      if (max - min != j - i) {
         return false;
      }
      boolean visited[] = new boolean[j - i + 1];
      for (int k = i; k <= j; k++) {
         if (visited[A[k] - min]) {
            return false;
         }
         visited[A[k] - min] = true;
      }
      return true;
   }
}

Output

The largest subarray is from index [1,3]
The elements of subarray are: 12 14 13 

Example

import java.lang.*;
public class Main {
   public static void main (String[] args) {
      int[] A = { 9, 4, 3, 1, 0, 2, 18, 0 };
      findMaxSubarray(A);
   }
   public static void findMaxSubarray(int[] A) {
      int len = 1;
      int start = 0, end = 0;
      for (int i = 0; i < A.length - 1; i++) {
         int min_val = A[i], max_val = A[i];
         for (int j = i + 1; j < A.length; j++) {
            min_val = Math.min(min_val, A[j]);
            max_val = Math.max(max_val, A[j]);
            if (isConsecutive(A, i, j, min_val, max_val)) {
               if (len < max_val - min_val + 1) {
                  len = max_val - min_val + 1;
                  start = i;
                  end = j;
               }
            }
         }
      }
      System.out.println("The largest subarray is from index [" +start + "," + end + "]");
      System.out.print("The elements of subarray are: ");
      for (int x = start; x <= end; x++) {
         System.out.print(A[x]+" ");
      }
   }
   private static boolean isConsecutive(int[] A, int i, int j, int min, int max) {
      if (max - min != j - i) {
         return false;
      }
      boolean visited[] = new boolean[j - i + 1];
      for (int k = i; k <= j; k++) {
         if (visited[A[k] - min]) {
            return false;
         }
         visited[A[k] - min] = true;
      }
      return true;
   }
}

Output

The largest subarray is from index [1,5]
The elements of subarray are: 4 3 1 0 2 

In this article, we explored different approaches to check if a subarray is formed by consecutive integers from a given array by using Java programming language.

Updated on: 30-Jan-2023

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements