Consider an array A[1..n] of random length and random positive integer values. We define a subarray of A as a contiguous segment of A. We denote the subarray from position k to position l (both included) as A[k..l]. The subarray A[k..l] is an ascent if A[j] ≤ A[j + 1] for all j where k ≤ j < l. In other words, an ascent is a nondecreasing segment of A.compute the maximal length of an ascent in A. For instance, given an array A =[5; 3; 6; 4; 6; 6; 7; 5], your algorithm should display:
The maximal length of an ascent would be 4
, and A[4..7] = [4; 6; 6; 7]
is the longest ascent in the array A. The algorithm cannot use any auxiliary storage such as ’extra’ arrays to perform what is needed. I am not sure how to solve this, this is the closest I've gotten to the solution.
class practice {
public static void ascentLength(int arr[], int size) {
int length = 0;
int index = 0;
for (int i = 0; i < size - 1; i++) {
index = i;
if (arr[0] <= arr[i + 1]) {
System.out.println(arr[i]);
length++;
}
if (arr[0] >= arr[i + 1]) {
length = 0;
}
}
System.out.println("length: " + length);
}
/* Driver program to test above function */
public static void main(String[] args) {
int arr[] = {5, 3, 6, 4, 6, 6, 7, 5};
int n = arr.length;
ascentLength(arr, n);
}
}