We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Greedy
  4. Beautiful Pairs

Beautiful Pairs

Problem
Submissions
Leaderboard
Discussions
Editorial

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.

Function Description

Complete the beautifulPairs function in the editor below. It should return an integer that represents the maximum number of pairwise disjoint beautiful pairs that can be formed.

beautifulPairs has the following parameters:

  • A: an array of integers
  • B: an array of integers

Input Format

The first line contains a single integer , the number of elements in and .
The second line contains space-separated integers .
The third line contains space-separated integers .

Constraints

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.

Sample Input 0

4
1 2 3 4
1 2 3 3

Sample Output 0

4

Explanation 0

You are given and .
The beautiful set is and maximum sized pairwise disjoint beautiful set is either or .
We can do better. We change the element of array from to . Now new B array is: and the pairwise disjoint beautiful set is . So, the answer is 4.
Note that we could have also selected index 3 instead of index 2 but it would have yeilded the same result. Any other choice of index is not optimal.

Sample Input 1

6
3 5 7 11 5 8
5 7 11 10 5 8

Sample Output 1

6

Author

YuryBandarchuk

Difficulty

Easy

Max Score

30

Submitted By

28376

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy