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.

Question From Hackerrank.
Solution in C# -

class Solution {
    static int beautifulPairs(int[] A, int[] B) {
        int count = 0;
        for(int i=0; i<A.Length; i++) {
            for(int j=0; j<B.Length; j++) {
                if(A[i] == B[j]) {
                    B[j] = -1;
                    count++;
                    break;
                }
            }
        }
        return count == A.Length? count-1: count+1;
    }
static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
        int n = Convert.ToInt32(Console.ReadLine());
        int[] A = Array.ConvertAll(Console.ReadLine().Split(' '), ATemp => Convert.ToInt32(ATemp)) ;
        int[] B = Array.ConvertAll(Console.ReadLine().Split(' '), BTemp => Convert.ToInt32(BTemp));
        int result = beautifulPairs(A, B);
       textWriter.WriteLine(result);
        textWriter.Flush();
        textWriter.Close();
    }
}

Post a Comment

Previous Post Next Post