How can I optimize memory with below solution

public ArrayList<Integer> solve(ArrayList<Integer> A, ArrayList<Integer> B) {
List<Integer> prodDiv = subarr(A, 1);
List<Integer> sortedOnPos = sortBasedOnPos(prodDiv, B);
return (ArrayList<Integer>) sortedOnPos;
}

private List<Integer> subarr(ArrayList<Integer> a, int n) {
HashMap<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
for(int i = 1;i<=a.size();i++){
final int mod = 1000000007;
int k = 0;
while(k <= a.size()-i) {
int max = findmax(a, k, i);
map.computeIfAbsent(max, x -> prod(mod, max));
list.add(map.get(max));
k++;
}
}

return list;
}

private int findmax(ArrayList<Integer> a, int k, int t) {
int max = Integer.MIN_VALUE;
for(int i = k;i< k+t;i++) {
max = Math.max(max, a.get(i));
}
return max;

}

private int prod(int mod, int a) {
long res = a;
for (int d = 2; d d <= a; ++d) {
if (a % d != 0) continue;
int m = (d
d == a) ? d : a;
res = (res * m) % mod;
}
return (int) res;
}

private List<Integer> sortBasedOnPos(List<Integer> prodDiv, List<Integer> B){
Collections.sort(prodDiv, (x,y)->y-x);
return B.stream().map(i -> prodDiv.get(i-1)).collect(Collectors.toList());

}

Did you find a solution yet , if yes then please help.

Click here to start solving coding interview questions