This is the Java Program to Print the Longest Sub-Array that is decreasing.
Given an array of integers, find the longest contiguous subarray whose elements are decreasing, that is, the elements following the preceding elements in the subarray must be smaller than them.
Example:
Array = [5, 6, 3, 0, 7, 8, 9, 1, 2]
Output: 6 3 0
Traverse the array from beginning to end and check for the first element which is greater than the element following it, from that index find out the subarray which is decreasing, if the current subarray length is greater than the previous subarray length, then update the longest decreasing subarray. Initially, the subarray would be of length one, referring to the first elements of the array.
Here is the source code of the Java Program to Print the Longest Sub-Array that is Decreasing. 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 Decreasing
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class LongestDecreasingSubarray {
// Function to calculate the longest decreaing subarray
static int[] longestDecreasingSubarray(int[] array){
int[] index = new int[2];
int i,j,start = 0;
int max = 0;
for(i=0; i<array.length-1; i++){
max = 0;
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 user 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 = longestDecreasingSubarray(array);
System.out.println("The longest decreasing subarray is");
for(i=index[0];i<=index[1];i++){
System.out.print(array[i] + " ");
}
}
}
1. In function longestDecreasingSubarray(), the array index is created to hold the starting and ending indexes of the longest decreasing subarray.
2. The loop for(i=0; i<array.length-1; i++) is used to traverse the array from left to right.
3. The condition if(array[i] > array[i+1]) checks if the current element is greater than the following element.
4. If the condition is true, then using the nested loop for(j = i+1; j<array.length-1; j++) the length of the decreasing subarray is calculated.
5. The condition if(max > index[1] – index[0] + 1) checks whether the current subarray is longer than the previous subarray.
6. If it is the indexes are updated.
Time Complexity: O(n2) where n is the number of elements in the array.
Case 1 (Simple Test Case): Enter the size of the array 9 Enter array elements 5 6 3 0 7 8 9 1 2 The longest decreasing subarray is 6 3 0 Case 2 (Simple Test Case - another example): Enter the size of the array 5 Enter array elements 9 7 5 3 1 The longest decreasing subarray is 9 7 5 3 1
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]
- Check Programming Books
- Practice Programming MCQs
- Check Java Books
- Apply for Computer Science Internship
- Apply for Java Internship