I need an algorithmic help to convert an input array in Java having this content : {5,6,3,2,4,7}
To this format :
[[2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]
.
Advertisement
Answer
List<Integer> arrayList = new ArrayList<>(Arrays.asList(5,6,3,2,4,7)); Collections.sort(arrayList); System.out.println("Input:"+arrayList); List<List<Integer>> list = new ArrayList<List<Integer>>(); list.add(0, new ArrayList<Integer>(Arrays.asList(arrayList.get(0),arrayList.get(1)))); list.add(1, new ArrayList<Integer>(Arrays.asList(arrayList.get(1),arrayList.get(2)))); list.add(2, new ArrayList<Integer>(Arrays.asList(arrayList.get(2),arrayList.get(3)))); list.add(3, new ArrayList<Integer>(Arrays.asList(arrayList.get(3),arrayList.get(4)))); list.add(4, new ArrayList<Integer>(Arrays.asList(arrayList.get(4),arrayList.get(5)))); System.out.println("Output:"+list);