import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static List<Integer> productsNConsecutiveItems(List<Integer> lst, int n) {
List<Integer> products = new ArrayList<>();
if (n <= 0 || lst.size() < n) {
return products; // empty
}
int outSize = lst.size() - n + 1;
for (int i = 0; i < outSize; i++) {
int prod = 1;
// Multiply lst[i] * lst[i+1] * ... * lst[i+n-1]
for (int j = 0; j < n; j++) {
prod *= lst.get(i + j);
}
products.add(prod);
// Example for n = 3:
// 2 * 3 * 4 = 24
// 3 * 4 * 5 = 60
// 4 * 5 * 6 = 120
// 5 * 6 * 7 = 210
// 6 * 7 * 8 = 336
// 7 * 8 * 9 = 504
// 8 * 9 * 10 = 720
}
return products;
}
public static void main(String[] args) {
List<Integer> lst = Arrays.asList(2, 3, 4, 5, 6, 7, 8, 9, 10);
int n = 3;
List<Integer> products = productsNConsecutiveItems(lst, n);
System.out.println(products);
}
}
/*
run:
[24, 60, 120, 210, 336, 504, 720]
*/