This article describes the calculation maximum subscript distance algorithm implemented in Java. Share it for your reference, as follows:
Question description
Given a shaping array, find the maximum subscript distance j−i, if A[i] < A[j] and i < j
solution
Complexity: Three scans, each time the complexity O(N)
Algorithm: {5,3,4,0,1,4,1}
Find the descending sequence {5,3,0} starting from the first element
i=3,j=6, j initialized from the tail scan, i=3, j=6, A[i]=0
Implement code
public static int maxindexdistance(int A[]) { boolean[] isDes = new boolean[A.length]; int min = A[0]; isDes[0] = true; for (int i = 0; i < A.length; i++) { if (A[i] < min) { isDes[i] = true; min = A[i]; } } int maxdis = 0; int i = A.length - 1; int j = A.length - 1; System.out.println(Arrays.toString(isDes)); while (i >= 0) { while (isDes[i] == false) { i--; } while (j > i && A[j] <= A[i]) { j--; } if ((j - i) > maxdis) { maxdis = j - i; } i--; } return maxdis;}For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.