This is the Java Program to Print the Longest Sub-Array that is increasing.
Given an array of integers, find the longest contiguous subarray whose elements are increasing, that is, the elements following the preceding elements in the subarray must be greater than them.
Example:
Array = [5, 6, 3, 0, 7, 8, 9, 1, 2]
Output: 0 7 8 9
Traverse the array from beginning to end and check for the first element which is smaller than the element following it, from that index find out the subarray which is increasing, if the current subarray length is greater than the previous subarray length, then update the longest increasing subarray. Initially, the subarray would be of length one, referring to the first element of the array.
Here is the source code of the Java Program to Print the Longest Sub-Array that is Increasing. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Print the Longest Sub-Array that is Increasing
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class LongestIncreasingSubarray {
// Function to calculate the longest increasing subarray
static int[] longestIncreasingSubarray(int[] array){
int[] index = new int[2];
int i,j,start = 0;
int max = 0;
for(i=0; i<array.length-1; i++){
if(array[i] < array[i+1]){
start = i;
max++;
for(j = i+1; j<array.length-1; j++){
if(array[j] > array[j+1])
break;
else
max++;
}
if(max > index[1] - index[0] + 1){
index[0] = start;
index[1] = j--;
}
i = j;
}
}
return index;
}
// Function to read input and display the output
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size;
System.out.println("Enter the size of the array");
try {
size = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Invalid Input");
return;
}
int[] array = new int[size];
System.out.println("Enter array elements");
int i;
for (i = 0; i < array.length; i++) {
try {
array[i] = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("An error occurred");
}
}
int[] index = longestIncreasingSubarray(array);
System.out.println("The longest increasing subarray is");
for(i=index[0];i<=index[1];i++){
System.out.print(array[i] + " ");
}
}
}
1. In function longestIncreasingSubarray(), we declare array index [] to store the starting and ending index of the longest subarray.
2. The loop for(i=0; i<array.length-1; i++) is used to iterate through the array.
3. The condition if(array[i] < array[i+1]) is used to check if the current pair of elements is increasing.
4. If it is true, then a nested loop for(j = i+1; j<array.length-1; j++) is set-up to look for the end of the increasing subarray.
5. The condition if(max > index[1] – index[0] + 1) checks if the current subarray length, is greater than the previous one. It updates the index array, if it is true.
6. The variable max stores the length of the increasing subarray and variable start stores the starting index.
Time Complexity: O(n) where n is the number of elements in the array.
Case 1 (Simple Test Case - Mixed Elements): Enter the size of the array 9 Enter array elements 5 6 3 0 7 8 9 1 2 The longest decreasing subarray is 0 7 8 9 Case 2 (Simple Test Case - Elements in Ascending Order): Enter the size of the array 5 Enter array elements 1 2 3 4 5 The longest increasing subarray is 1 2 3 4 5 Case 3 (Simple Test Case - Elements in Descending Order): Enter the size of the array 6 Enter array elements 6 5 4 3 2 1 The longest increasing subarray is 6
Sanfoundry Global Education & Learning Series – Java Programs.
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check Java Books
- Check Programming Books
- Apply for Java Internship