Beautiful Pairs


Problem Statement :


You are given two arrays,  and , both containing  integers.

A pair of indices  is beautiful if the  element of array  is equal to the  element of array . In other words, pair  is beautiful if and only if . A set containing beautiful pairs is called a beautiful set.

A beautiful set is called pairwise disjoint if for every pair  belonging to the set there is no repetition of either  or  values. For instance, if  and  the beautiful set  is not pairwise disjoint as there is a repetition of , that is .

Your task is to change exactly  element in  so that the size of the pairwise disjoint beautiful set is maximum



You are given two arrays,  and , both containing  integers.

A pair of indices  is beautiful if the  element of array  is equal to the  element of array . In other words, pair  is beautiful if and only if . A set containing beautiful pairs is called a beautiful set.


Output Format

Determine and print the maximum possible number of pairwise disjoint beautiful pairs.

Note: You must first change  element in , and your choice of element must be optimal.


A beautiful set is called pairwise disjoint if for every pair  belonging to the set there is no repetition of either  or  values. For instance, if  and  the beautiful set  is not pairwise disjoint as there is a repetition of , that is .

Your task is to change exactly  element in  so that the size of the pairwise disjoint beautiful set is maximum



Solution :



title-img


                            Solution in C :

In  C :





#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int n;
    scanf("%d",&n);
    int a[n],b[n],i,j,count=0;
    for(i=0;i<=n-1;i++)
        {
        scanf("%d",&a[i]);
    }
    for(i=0;i<=n-1;i++)
        {
        scanf("%d",&b[i]);
    }
    for(i=0;i<=n-1;i++)
        {
        for(j=0;j<=n-1;j++)
            {
            if(b[j]!=-1)
                {
            if(a[i]==b[j])
                {
                count++;
                b[j]=-1;
                break;
            }
            }
        }
    }
    if(count==n)
        printf("%d",count-1);
    else
    printf("%d",count+1);
    return 0;
}
                        


                        Solution in C++ :

In  C++  :







#include <bits/stdc++.h>
using namespace std;

int n;
int cntA[1001];
int cntB[1001];

int MAIN()
{
	memset(cntA, 0, sizeof(cntA));
	memset(cntB, 0, sizeof(cntB));
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		int t;
		cin >> t;
		cntA[t] ++;
	}
	for(int i = 1; i <= n; i++)
	{
		int t;
		cin >> t;
		cntB[t] ++;
	}
	int ans = 0;
	for(int i = 1; i <= 1000; i++)
		ans += min(cntA[i], cntB[i]);
	if(ans == n)
		ans --;
	else
		ans ++;
	cout << ans << endl;
	
	return 0;
}

int main()
{
	int start = clock();
	#ifdef LOCAL_TEST
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	#endif
	ios :: sync_with_stdio(false);
	cout << fixed << setprecision(16);
	int ret = MAIN();
	#ifdef LOCAL_TEST
		cout << "[Finished in " << clock() - start << " ms]" << endl;
	#endif
	return ret;
}
                    


                        Solution in Java :

In  Java :






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int len=s.nextInt();
        HashMap<Integer,Integer> hmap=new HashMap<Integer,Integer>();
        for(int i=0;i<len;i++)
            {
            int current=s.nextInt();
            if(!hmap.containsKey(current))
                {
                
                hmap.put(current,1);
            }
            else
                {
                int temp=hmap.get(current);
                hmap.put(current,++temp);
            }
        }
        int counter=0;
        for(int i=0;i<len;i++)
            {
            int current=s.nextInt();
            if(hmap.containsKey(current))
            {
                int temp=hmap.get(current);
                if(temp>0)
                {
                    hmap.put(current,--temp);
                    counter++;
                }
                else
                    {
                    hmap.remove(current);
                }
                
            }
        }
        if(counter==len)
            System.out.println(counter-1);
        else if(counter<len)
            System.out.println(counter+1);
        else
            System.out.println(counter);
    }
}
                    


                        Solution in Python : 
                            
In  Python3 :







input()
a = [x for x in input().split()]
b = [x for x in input().split()]
aDict = dict()
bDict = dict()
for val in a:
    if val in aDict:
        aDict[val] += 1
    else:
        aDict[val] = 1
for val in b:
    if val in bDict:
        bDict[val] += 1
    else:
        bDict[val] = 1

total = 0
for val in aDict:
    while aDict[val] > 0 and val in bDict and bDict[val] > 0:
        total += 1
        aDict[val] -= 1
        bDict[val] -= 1

if total == len(a):
    print(total - 1)
else:
    print(total + 1)
                    


View More Similar Problems

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →