
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Maximum subarray sum in array formed by repeating the given array k times in C++
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- How to create a subarray from another array in Java
- Check if subarray with given product exists in an array in Python
- Find mean of subarray means in a given array in C++
- Construct an array from GCDs of consecutive elements in given array in C++
- Find Elements Greater Than a Given Number In a Subarray in Java
- How to check if array contains three consecutive dates in java?
- Generate a random array of integers in Java
- Check if a number can be written as sum of three consecutive integers in C++
- Longest consecutive path from a given starting character
- Check if a string can be formed from another string using given constraints in Python
- JavaScript to check consecutive numbers in array?
- Return an array formed from the elements of a masked array at the given indices in NumPy
- Write a program in Java to find the missing positive number in a given array of unsorted integers
