Question

In: Computer Science

Triplicates. Given three lists of N names each, devise a linearithmic algorithm to determine if there...

Triplicates. Given three lists of N names each, devise a linearithmic algorithm to determine if there is any name common to all three lists, and if so, return the first such name

Solutions

Expert Solution

Hi buddy, I can give you two solutions. 1. Using HashMap<String,Integer>. It stores the count of a certain String. If we ever encounter a string of count 3 while dealing with the third list. We will stop processing and return the string as the answer.

Java Program : (Comments have been added for your better understanding)

import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
   public static void main (String[] args) throws java.lang.Exception
   {
   //This is list 1
       ArrayList<String> l1 = new ArrayList();
      
       //I'm loading the list with the sample data
       String s1[] = new String[]{"a","b","c","d","b"};
       for(String x : s1){
       l1.add(x);
       }
      
       //This is list2 and loading the list with the sample data
       ArrayList<String> l2 = new ArrayList();
       String s2[] = new String[]{"b","d","b","c","p"};
       for(String x : s2){
       l2.add(x);
       }
      
       //This is list3 and loading the list with the sample data
       ArrayList<String> l3 = new ArrayList();
       String s3[] = new String[]{"z","q","a","b","d"};
       for(String x : s3){
       l3.add(x);
       }
      
       //Initializing the hash map
       HashMap<String,Integer> count = new HashMap();
      
       //This loop iterates through all the elements in the list 1 and assigns a count
       //of 1 to all the Strings in this list
       for(String x : l1){
       if(!count.containsKey(x)){
       count.put(x,1);
       }
       }
      
       //This loop iterates through all the elements in the list 2 and assigns a count of
       //2 If this String already exists in the map
       for(String x : l2){
       if(count.containsKey(x)){
       count.put(x,2);
       }
       }
      
       //This loop iterates through all the elements in the list 3 and Prints the first
       //String whose count in the hash map is 2 (i.e this string exists in all the three lists)
       for(String x : l3){
       if(count.containsKey(x)&&count.get(x)==2){
       System.out.println("The First String common to all the lists is : "+x);
       return;
       }
       }
   }
}

OUTPUT:

The First String common to all the lists is : b

-----------------------------------------------------------------------------------------------------------------------------------------------

2. This is a bit complex and involves another data structure called "Trie" (Suffix Tree). I would suggest you to search for Trie/Suffix tree for further information. It is very useful and important data structure.


Related Solutions

1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there...
1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there is any name common to all three arrays, and if so, return the first such name.? 2. Given an input array with all equal values, compare the following sorting algorithm using big-O notation. Running Time Space Complexity Merge Sort Quick Sort Heap Sort
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Describe an optimal algorithm for finding the integers common to two lists of n integers each....
Describe an optimal algorithm for finding the integers common to two lists of n integers each. Evaluate how long each step in your algorithm takes using Θ-notation.
For each given integer n, determine if n is a sum of two squares. (You do...
For each given integer n, determine if n is a sum of two squares. (You do not have to find the squares.) (a) n = 19 (b) n = 45 (c) n = 99 (d) n = 999 (e) n = 1000
For each given integer n, determine if n is a sum of two squares. (You do...
For each given integer n, determine if n is a sum of two squares. (You do not have to find the squares.) (a)n= 19 (b)n= 45 (c)n= 99 (d)n= 999 (e)n=1000
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n)...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n) algorithm to find the duplicates between two sorted arrays (m, n are the sizes of two arrays). For instance, for inputs {1,3,5,7} and {1,2,3,4}, the correct outputs should be {1,3
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling...
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling if L and M store the same sequence of elements (but perhaps with different starting points). Please provide a main() function with it in Java to test it.
We are given an array of n numbers A in an arbitrary order. Design an algorithm...
We are given an array of n numbers A in an arbitrary order. Design an algorithm to find the largest and second largest number in A using at most 3/2n -2 comparisons. (i) describe the idea behind your algorithm in English (3 points); (ii) provide pseudocode (5 points); (iii) analyze the number of comparisons used in your algorithm (2 points).
The author lists three types of waste. List the three with the given definitions. Give a...
The author lists three types of waste. List the three with the given definitions. Give a real world example of each type of waste.
Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm...
Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm to find the largest value of ‘n’ for which f(n) is less than a target. Show all the work (including the values for the left index, right index, middle index and the function value at the middle index) for each iteration as well as write down the initial values of the left index and right index and the corresponding function values. f(n) = 3n^3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT