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 :
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
Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →Merge two sorted linked lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C
View Solution →Get Node Value
This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t
View Solution →Delete duplicate-value nodes from a sorted linked list
This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -
View Solution →Cycle Detection
A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer
View Solution →Find Merge Point of Two Lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share
View Solution →