Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will be given an unalphabetized collection of book titles followed by a seri

ID: 3749121 • Letter: Y

Question

You will be given an unalphabetized collection of book titles followed by a series of queries. A "query" is just a book title, and your job is to return the index of each book in the collection as well as the number of titles you had to look at to return that index. (For example, if the book is the first title you look at, that number would be 1.)

Input Format

The first line will consist of an integer, n, indicating how many books are in the collection. The next n lines will consist of strings representing book titles. The next line will consist of an integer, m, the number of titles you have to look for. The next m lines will each consist of a book title to look up. The input will end with a blank line. For example:

Constraints

You may assume n and m will always be non-negative integers. Additionally, you may assume that there are no duplicate titles.

Output Format

For each query, output a line in the format "i: x" where i is the index of the book title and x is the number of titles you looked at to find it. If the title is not in the collection, give -1 as i to indicate it was not found. For example:

Sample Input 0

Sample Output 0

Explanation / Answer

Below given program explains how the algorithm can work using Java. Can be converted in any language as per your requirement. Please apply the constraints and rewrite the solution.

import java.util.*;

public class chegg {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
   Scanner s = new Scanner(System.in);
   int N = s.nextInt();//total number of books
   int M = s.nextInt();//list of books to be found
   int count = 0;// keeps count of the number of elent checked
    //System.out.println("-1" + ":" + count+1);
   String[] bookList = new String[N]; //string array containing whole book
   for(int i =0; i<N; i++) {
       bookList[i] = s.next();
       System.out.println(bookList[i]);
   }
   String[] retrieveBookList = new String[M];
   for(int j =0; j<M; j++) {
       retrieveBookList[j] = s.next();
       System.out.println(retrieveBookList[j]);
   }
   //comparing and printing the required output
   for(int k=0; k<M; k++) { //this for loop considers the retriveBookList
       count = 0;
       for(int l=0; l<N; l++) {//this is fot bookList
           if(retrieveBookList[k]!= bookList[l]) {//each elemet of retrivebookList array is compared with all the elements of booklist Array
               count++;
              // System.out.println(count);
           }else {
               if(count == N) {
                   System.out.println("-1" + ":" + count+1);//this prints if element is not found
               }else {
               System.out.println(k+1 + ":" + count+1);//this prints if the element is found
               }
           }
       }
   }
// System.out.println(bookList[0]);
   //System.out.println(retrieveBookList[0]);
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote