In below code, I am able to get the required answer where I am removing smaller number than its previous index and next index. However along with the answer also i am encountering the error- Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 Need help in identifying how to break recursion here. Thanks in advance.
public class numberProblem {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(3);
list.add(20);
list.add(5);
list.add(30);
list.add(20);
list.add(60);
int len = list.size();
System.out.println(list);
testList(list, len);
}
public static void testList(ArrayList<Integer> list, int len) {
System.out.println("Len is:"+len);
System.out.println(list.size());
ArrayList<Integer> l = list;
len= l.size();
for(int i= 1;i<=len-1;i++) {
if(l.get(i)<l.get(i-1) && l.get(i)<l.get(i+1)) {
l.remove(i);
testList(l, l.size());
}
}
System.out.println(list);
}
}