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

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →