Question

for java: NOTE: Make sure, you have exception handling code in place for each exceptional condition...

for java:

NOTE: Make sure, you have exception handling code in place for each exceptional condition for every question. Also, make sure you have tested all the codes with various boundary conditions.

Given a list of pairs; Give an efficient method (O(n)) to print all “Symmetric Pairs”. A pair is symmetric if both pair (i, j) and pair (j, i) exist in the list. For example, in { {3, 1}, {2, 6}, {3, 5}, {7, 4}, {5, 3}, {8, 7} } , note that {3, 5} is already present in the list when you encounter {5, 3}, that means they are symmetric pair, so print this pair when you encounter {5, 3}. [Hint: use hash table]
Algorithm:
1) Read the pairs of elements one by one and insert them into a hash table. For each pair, consider the first element as a key and second element as value.
2) While inserting the key elements check if the hashing of the second element of the current pair is the same as the first number of the current pair.
3) If they are the same, this means that the pairs are symmetric and output that pair.
4) Otherwise, insert that element into that. That means, use the first number of the pair as key and the second number as value and insert them into the hash table.
5) By the time we complete the scanning of all pairs, we have output all the symmetric pairs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class Main {
public static void main(String[] args)
{
Hashtable<Integer, Integer> t = new Hashtable<Integer, Integer>();
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
int a,b;
System.out.println("Enter pair of values with space");
String s="";
for(int i=0;i<n;i++)
{
s=sc.nextLine();
String d[]=s.split(" ");
a=Integer.parseInt(d[0]);
b=Integer.parseInt(d[1]);
Integer val = t.get(b);
if (val != null && val == a)
System.out.println("(" + a + ", " + b + ")");
else
t.put(a, b);
}
}
}

Add a comment
Know the answer?
Add Answer to:
for java: NOTE: Make sure, you have exception handling code in place for each exceptional condition...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in C++ Code should work for all cases In this assignment you are requested to implement...

    in C++ Code should work for all cases In this assignment you are requested to implement insert, search, and delete operations for an open-addressing hash table with double hashing. Create an empty hash table of size m= 13. Each integer of the input will be a key that you should insert into the hash table. Use the double hashing function h{k, i) = (hı(k) + ih2(k)) mod 13 where hi(k)= k mod 13 and h2(k) = 1+(k mod 11). The...

  • Explain each answer. Select the correct option for the following multiple choice questions or provide short...

    Explain each answer. Select the correct option for the following multiple choice questions or provide short answers accordingly: 1. Consider a hash table with chaining, of size N. Assume in each of the following scenarios the table starts empty. Further assume K pairs of <key, element> are added and K N Then, in which of the following situations will a find operation for a particular key have a potential worst-case runtime of O(N) a) The keys of all elements are...

  • please use Java!! We are storing the inventory for our fruit stand in a hash table....

    please use Java!! We are storing the inventory for our fruit stand in a hash table. The attached file shows all the possible key/fruit combinations that can be inserted into your hash table. These are the real price lookup values for the corresponding fruit. Problem Description In this programming assignment, you will implement a hash table class fruitHash. Your hash table must include the following methods: 1) hashFunction(key) This method takes a key as an argument and returns the address...

  • Use Java language You may assume that you always map strings to integers, and therefore you...

    Use Java language You may assume that you always map strings to integers, and therefore you need not worry about using generics. Here are the methods that your map must implement. void put (String key, Integer value); insert this key-value pair into the map under the following rules: If key already exists in the map, change its value to value, overwriting what was there If key is not already there, insert this key-value pair into the map. boolean containsKey(String key);...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • Insert elements into a hash table implemented using chain hashing, as an array of linked list...

    Insert elements into a hash table implemented using chain hashing, as an array of linked list in which each entry slot is as a linked list of key/value pairings that have the same hash (outcome value computed using certain hash function). You are allowed to use “Hash Function”, h(k) = k % x where, x is the value you will need to decide to use, that you find appropriate for this implementation. The main requirements are the following: 1. Input:...

  • Task: Comparing the performance between Linear Probing and Double Hashing. Requirements: Please make sure your program...

    Task: Comparing the performance between Linear Probing and Double Hashing. Requirements: Please make sure your program compiles, otherwise your submission will not be graded and you will receive zero. Point deduction rule: Compile warning: 3 points each. Minor error: such as not meeting the assignment input/output requirement, 5 points each. Major error: examples include, infinite loop, runtime errors, any runtime exception, 15 points each. Any code not compliant to the assignment requirement (e.g., not using List interface and AbstractMap and...

  • 5. Hashing (a) Consider a hash table with separate chaining of size M = 5 and...

    5. Hashing (a) Consider a hash table with separate chaining of size M = 5 and the hash function h(x) = x mod 5. i. (1) Pick 8 random numbers in the range of 10 to 99 and write the numbers in the picked sequence. Marks will only be given for proper random numbers (e.g., 11, 12, 13, 14 ... or 10, 20, 30, 40, .. are not acceptable random sequences). ii. (2) Draw a sketch of the hash table...

  • JAVA Create an ArrayList called list of type Character 2. Add the following five elements |...

    JAVA Create an ArrayList called list of type Character 2. Add the following five elements | < \ / > (4 points) 3. Print the elements of the list, with a label (3 points) 4. Delete the last element of the list (4 points) 5. Use the method size to determine how many elements there are in the list (no hard-coded numbers). Print the number of elements. (3 points) 6. Add a plus sign ( + ) to the list...

  • IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public...

    IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public QuadraticProbingHashTable( int size ): As the signature of this method suggests, this is a constructor for this class. This constructor will initialize status of an object from this class based on the input parameter (i.e., size). So, this function will create the array HashTable with the specified size and initialize all of its elements to be null. 2. public int hash(int value, int tableSize...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT