I have given interview with one company where they asked me Sliding Window Minimum value problem as below -
Find maximum value of minimum values in sub-array of size x
e.g. arr = [2,5,4,6,8] and x = 3 then subarray would be [2,5,4], [5,4,6] and [4,6,8].
The respective minimum values are 2,4 and 4. The maximum of these values is 4.
I have return below code but it is giving Time Limit Exceeded for some test
public static int max(int x, List<Integer> num){
int n = num.size();
List<Integer> minList = new ArrayList<>();
for(int i = 0; i <= n - x; i++){
int minVal = Integer.MAX_VALUE;
for(int j = i; j < i + x; j++){
minVal = Math.min(min, num.get(j));
}
minList.add(minVal);
}
return Collections.max(minList);
}
Can someone correct it or point out for correct one.