Intro to Hashing problems made easy

/*--- Below are 3 problems i have used hasing technique, After seeing below problems you'll be 100% clear on the basic hasing needed for competative programming


First Repeating Element 

Given an array arr[] of size N. The task is to find the first repeating element in an array of integers, i.e., an element that occurs more than once and whose index of first occurrence is smallest.

Input :
The first line contains an integer T denoting the total number of test cases. In each test cases, First line is number of elements in array N and second its values.

Output:
In each separate line print the index of first repeating element, if there is not any repeating element then print “-1” (without quotes). Use 1 Based Indexing.

Constraints:
1 <= T <= 500
1 <= N <= 106
0 <= Ai <= 106

Example:
Input:
1
7
1 5 3 4 3 5 6

Output:
2

Explanation:
Testcase 1: 5 is appearing twice and its first appearence is at index 2 which is less than 3 whose first occuring index is 3.
*/
#include<iostream>
using namespace std;
int main()
 {
int t,n,i,f;
cin>>t;
while(t--)
{f=0;
    cin>>n;
    int k=1000001;
    int a[n],b[k]={0};
 
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n;i++)
    {
     
        if(b[a[i]]==0)
         b[a[i]]=1;
         else
         {
              b[a[i]]=-1;
         }
    }
     f=0;
    for(i=0;i<n;i++)
    {
        if(b[a[i]]==-1)
        {f=1;
            cout<<i+1<<endl;
            break;
        }
    }
    if(f==0)
    cout<<-1<<endl;
}
return 0;
}

/*

Intersection of two arrays

Given two arrays A and B respectively of size N and M. The task is to print the count of elements in the intersection (or common elements) of the two arrays.

For this question, intersection of two arrays can be defined as the set containing distinct common elements between the two arrays.

Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N and M, N is the size of array A and M is size of array B. The second line of each test case contains N input A[i].
The third line of each test case contains M input B[i].

Output:
Print the count of intersecting elements.

Constraints:
1 = T = 100
1 = N, M = 105
1 = A[i], B[i] = 105

Example:
Input:
4
5 3
89 24 75 11 23
89 2 4
6 5
1 2 3 4 5 6
3 4 5 6 7
4 4
10 10 10 10
20 20 20 20
3 3
10 10 10
10 10 10

Output:
1
4
0
1
*/

// note intersection of 2 arrays , union of 2 arrays etc can be solved using hashing concept
// code for  intersection of 2 arrays
#include<iostream>
using namespace std;
int main()
 {
int t,n,k,i,j;
cin>>t;
while(t--)
{
    cin>>n>>k;
    int a[n],b[k],c[100001]={0},count=0;
    for(i=0;i<n;i++)
    {cin>>a[i];
        c[a[i]]=1;
    }
    for(i=0;i<k;i++)
    {
        cin>>b[i];
        if(c[b[i]]==1 )
        {
    count++;
    c[b[i]]=-1;
        }
    }
    cout<<count<<endl;
}
return 0;
}
// Now you can definatly solve for union of 2 elements , solve it without seeing the code below
// incase you need assistance code is already down.., got more doubts let me know in the comments //section 

/*





Constraints:
1 = T = 100
1 = N, M = 105
1 = A[i], B[i] < 105

Example:
Input:
2
5 3
1 2 3 4 5
1 2 3
6 2
85 25 1 32 54 6
85 2

Output:
5
7

input:
1
6 5
1 1 2 2 3 3
8 9 7 6 5
output 
8
Explanation:
Testcase 1: 1, 2, 3, 4 and 5 are the elements which comes in the union set of both arrays. 
*/
#include<iostream>
using namespace std;
int main()
 {
int t,n,k,i,j;
cin>>t;
while(t--)
{
    cin>>n>>k;
    int a[n],b[k],c[100001]={0},count=0;
    for(i=0;i<n;i++)
    {cin>>a[i];
     
        if(c[a[i]]==0)
        {count++;
            c[a[i]]=1;
         
        }
    }
    for(i=0;i<k;i++)
    {
        cin>>b[i];
        if(c[b[i]]==0 )
        {
    count++;
    c[b[i]]=1;
        }
    }
    cout<<count<<endl;
}
return 0;
}

Comments

Popular posts from this blog

Stack - print bracket number

Codeforces Minimum subscribers in a window