Display the population of each country from the least to the highest.
[Input Format: First input refers to the no of countries, second one is an array to get the country names and the third one is an array to get the population of each country (the population is given in crores)]
[Assumption: no two countries will have same population]
Sample Input1:
5
Hong Kong
china
japan
australia
america
135
133
12
2
32
Sample Output1:
australia
japan
america
china
Hong Kong
2
12
32
133
135
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
String coun[] = new String[size];
int pop[] = new int[size];
for (int i = 0; i < size; i++) {
coun[i] = sc.next();
}
for (int i = 0; i < size; i++) {
pop[i] = sc.nextInt();
}
String sort_coun[] = new String[size];
int[] sort_pop = Arrays.copyOf(pop, size);
Arrays.sort(sort_pop);
for (int i = 0; i < size; i++) {
int findindex = findindex(pop, sort_pop[i]);
if (findindex != -1)
sort_coun[i] = coun[findindex];
}
for (int i = 0; i < size; i++) {
System.out.println(sort_coun[i]);
}
for (int i = 0; i < size; i++) {
System.out.println(sort_pop[i]);
}
}
public static int findindex(int[] a, int target) {
for (int i = 0; i < a.length; i++) {
if (a[i] == target)
return i;
}
return -1;
}
}