Answer:
The below code would be used to answer the above question
Step-by-step explanation:
import java.io.*;
import java.util.*;
public class Main
{
public static List<Integer> construct_a_longest_pallindrome(int a[]){
Map <Integer,Integer> map = new HashMap<>();
List<Integer> ans1 = new ArrayList<>();
List<Integer> ans2 = new ArrayList<>();
for(int i=0;i<a.length;i++){
if(map.containsKey(a[i])){
map.put(a[i],map.get(a[i])+1);
}else
map.put(a[i],1);
}
int pos1 = 0;
int pos2 = 0;
for(Integer key : map.keySet()){
int val = map.get(key);
while(val % 2 == 0){
ans1.add(key);
ans2.add(key);
val = val-2;
}
if(val == 0)
map.remove(key);
else
map.put(key,1);
}
if(map.isEmpty() == false){
ans1.add(map.keySet().iterator().next());
}
Collections.reverse(ans2);
ans1.addAll(ans2);
return ans1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array");
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
System.out.println(construct_a_longest_pallindrome(arr));
}
}