0

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);
    }
}
Community
  • 1
  • 1

3 Answers3

1

Tracing a code and drawing figures for array is a good way to understand what's your program doing.

int arr[] = {5, 3, 6, 4, 6, 6, 7, 5};

int maxLength = 0, count = 0;
for(int i = 0; i < arr.length - 1; i++){
    if(arr[i + 1] < arr[i]){
        count = 0;
    }else{
        count++;
        if(count > maxLength){
            maxLength = count;
        }
    }
}
System.out.println(maxLength + 1);

at last 1 is added because we can't compare a number to it self.

XO56
  • 332
  • 1
  • 12
  • what do you mean by `it does not print the array`? do you want to print the sub array of arr which has the longest non-decreasing order? however, the program print `4` for the `arr = {4,6,6,7}` which is valid. – XO56 Feb 03 '21 at 21:18
0

There are a couple of mistakes that I notice right off the bat.

Inside the if expression, you're always comparing the first element of the array with the next element. It should be a comparison between the current and next element of the array.

The count variable is not declared anywhere. What is the purpose of the count variable?

In the second if block, you're not storing the length in a separate variable before assigning it to zero.

0

You need to track the size of the current ascent and the longest ascent with separation variables. On a given iteration you are either starting a new ascent or continuing the existing one. To simplify the iteration we can handle a few special cases up front (i.e. array that is null, empty, or single-element).

public int maxAscentLength(int[] arr) {
  if (arr == null || arr.length == 0) {
    return 0;
  }
  if (arr.length == 1) {
    return 1;
  }
  // the largest ascent so far
  int maxLength = 0;
  // length of current ascent - init 1 because 1st element is assumed
  int currLength = 1;
  // start iteration from 1 rather than 0 because 1st element is assumed
  for (int index = 1 ; index < arr.length ; index++) {
    if (arr[index-1] <= arr[index]) {
      // continue ascent
      ++currLength;
    } else {
      // end ascent
      maxLength = Math.max(maxLength, currLength);
      currLength = 1;
    }
  }
  return Math.max(maxLength, currLength);
}
vsfDawg
  • 1,425
  • 1
  • 9
  • 12