Question

Hash Tables. In C programming language, solve these problems efficiently using Hashtables and analyze the running...

Hash Tables. In C programming language, solve these problems efficiently using Hashtables and analyze the running time of your solution.

1. Given an unsorted array of length N, print all elements that appear more than once.

2. Find the intersection of K unsorted arrays of N elements each. The intersection consists of elements that appear in all the K arrays.

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

code:(a)

#include <stdio.h>
int main() {
int n,i;
int a[100001];
for(i=0;i<100001;i++)a[i]=0;//intializing map array to 0
scanf("%d",&n);//accepting size of array
int array[n];
for(i=0;i<n;i++)
scanf("%d",&array[i]);//accepting elements of array
for(i=0;i<n;i++){
a[array[i]]++;//incrementing no of occurrence of array[i]
if(a[array[i]]==2)//if current element occurs for second time print it
printf("%d ",array[i]);}
   return 0;
}
//since the loop is to iterate array element once hence time complexity is o(size of array);

sample input/output:

code(b)

#include <stdio.h>
int main() {
int n,i,k,x,j;
int a[100001];
for(i=0;i<100001;i++)a[i]=0;//intializing map array to 0
scanf("%d%d",&k,&n);//accepting no of array and size of array
int b[n];//to store kth array element
for(j=1;j<=k;j++)
{
for(i=0;i<n;i++)
{
scanf("%d",&x);// input element of jth array
b[i]=x;
if(a[x]<(j*(j+1))/2)// hashing to find if x is present in array j
a[x]+=j;// increment hash value by j
}
}
for(i=0;i<n;i++)
if(a[b[i]]==(k*(k+1))/2)// element whose hash value is sum of first k natural numbers will be present in all k array
printf("%d ",b[i]);
   return 0;
}
//since the loop is to iterate array element once hence time complexity is o(sum of size of arrays)=o(k*n)

sample input/output:

please upvote if it was helpful.feel free to ask any related query.

Add a comment
Know the answer?
Add Answer to:
Hash Tables. In C programming language, solve these problems efficiently using Hashtables and analyze the running...
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
  • Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps....

    Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps. This is a basic course and the furthest we have gone is BASIC arrays. Write the code for the following problems using arrays. Write what would go in the “int main()” part of the program. For these problems, the entire program and program output is not needed. Just include what goes in the int main() part of the program. 1. Declare an integer array...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into...

    Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...

  • Design an algorithm for the following description. Solution can be done in pseudo-code or steps of...

    Design an algorithm for the following description. Solution can be done in pseudo-code or steps of the algorithm. Describe and analyze an algorithm that takes an unsorted array A of n integers (in an unbounded range) and an integer k, and divides A into k equal-sized groups, such that the integers in the first group are lower than the integers in the second group, and the integers in the second group are lower than the integers in the third group,...

  • Please solve this question using c++ language Problem 2. More Probleml. The program builds the array...

    Please solve this question using c++ language Problem 2. More Probleml. The program builds the array of integers that contains duplicates as well. Your program should find the sum of all unique elements in the array using a function findUniqueSum Arrays Write a program that as before asks the user to enter input as in оn Sample Input/Output Enter a list of numbers ending with -999 3 1 4 55-999 The sum of unique numbers is: 13 Enter a list...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

  • 2. (10 points.) The procedure HASH-SEARCH takes a hash table T[O : m-1) and a key...

    2. (10 points.) The procedure HASH-SEARCH takes a hash table T[O : m-1) and a key k as its parameters. Each element of T is either a key or NIL. The procedure searches for k in T by probing. If it finds k, then it returns the index j where k appears in T. If it cannot find k, then it returns -1. (A procedure very much like HASH-SEARCH is discussed on pages 269–274 of Cormen.) HASH-SEARCH(T, k) i= 0...

  • please solve it before 11:15 today Subject :C++ programming - Lab_9_Sec 51.pdf 1411113: Programming for Engineers...

    please solve it before 11:15 today Subject :C++ programming - Lab_9_Sec 51.pdf 1411113: Programming for Engineers Fall 2017/2018 LAB #9 Name: ID: Date Section Objectives: After completing this lab, you will be able to .Analyze a problem. .Implement the solution in C++ . Practice arrays and strings. Lab Exc. Mark Score Correct input/output Computational correctness Correct input/output Computational correctness Exercise#1: Number of occurrences in an array Write a program that fills an array with 10 random numbers between 0 and...

  • This has to be in MSP430 assembly language. Thank you! Here is a link with instructions...

    This has to be in MSP430 assembly language. Thank you! Here is a link with instructions set for msp430 (pages 5-8): https://www.ti.com/sc/docs/products/micro/msp430/userguid/as_5.pdf Write an MSP430 assembly language subroutine, REP_FREE, to examine the elements of a list of positive word-size numbers stored at location LIST_IN. The list is already sorted in ascending order. The first element is the number, n, which is the length of the array. The subroutine will copy the elements from location LIST IN to location LIST_OUT. While...

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