generate functionTue, 22 Nov 2022

A list of Chetan's favourite authors ordered from least favourite to most favourite is given. Find the nth favourite author. Hint: Use size() to find the length of the arraylist and get() to access the element at the given index. Note that the index starts from 0. Sample Input 1 ["Chetan Bhagat", ""Aravind Adiga", "Khushwant Singh", "Salman Rushdie", "Shyam Singha Roy"] 1 Sample Output 1 Shyam Singha Roy Explanation 1 There are 5 authors in total and the 1st one from last (most favourite end) is Shyam Singha Roy.

public static void main(String[] args) { Scanner in = new Scanner(; int size = in.nextInt(); List<String> list = new ArrayList<String>(); for(int i = 0; i < size; i++) { String str =; list.add(str); } int n = in.nextInt(); Collections.reverse(list); String favorite = list.get(n-1); System.out.println(favorite); }

Questions about programming?Chat with your personal AI assistant