Code: Select all
import java.util.*;
// Other imports go here
// Do NOT change the class name
class Main
{
public void cq1(ArrayList list) {
ListIterator l1=list.listIterator();
ListIterator l2=list.listIterator();
while(l1.hasNext()){l1.next();l2.next();}//loop to send the listIterator to the end of ArrayList
l1.previous();//make this iterator point to the second last element
while(l1.hasPrevious())
{
int a=l1.previous();
int b=l2.previous();
if (a < b)
{
list.remove(l1.previousIndex()+1);
list.add(0, a);
}
System.out.println(list);
}
}//fn
public static void main(String[] args)
{
Main obj=new Main();
int a[]={-4, 16, 9, 1, 64, 25, 36, 4, 49};
ArrayList oe = new ArrayList();
for(int i :a)oe.add(i);
obj.cq1(oe);
}//main
}//class