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.

ArrayList<String> authors = new ArrayList<String>(); authors.add("Chetan Bhagat"); authors.add("Aravind Adiga"); authors.add("Khushwant Singh"); authors.add("Salman Rushdie"); authors.add("Shyam Singha Roy"); int index = 1; int size = authors.size(); int nth = size - index; String nthFavouriteAuthor = authors.get(nth); System.out.println(nthFavouriteAuthor);

Questions about programming?Chat with your personal AI assistant