Untitled
unknown
plain_text
2 years ago
1.1 kB
32
Indexable
int maxVal = A[0]; int minVal = A[0]; int max_idx = -1; int min_idx = -1; int ans = A.length; for (int i = 0; i < A.length; i++) { if(A[i] < minVal) { minVal = A[i]; } if(A[i] > maxVal) { maxVal = A[i]; } } for (int i = 1; i < A.length; i++) { if (A[i] == maxVal) { max_idx =i; } if (A[i] == minVal) { min_idx =i; } if (min_idx != -1 && min_idx != -1) { int TempInd = Math.abs(max_idx - min_idx) +1; if (TempInd < ans) ans =TempInd; } } return ans; } } Given an array A, find the size of the smallest subarray such that it contains at least one occurrence of the maximum value of the array and at least one occurrence of the minimum value of the array.
Editor is loading...