Please read the new rule regarding the restriction on the use of AI tools. ×

raghavvmittal11's blog

By raghavvmittal11, history, 8 months ago, In English

Intution — here we make 3 multiset to store the unique elements now we insert the input of the first array into the multiset (ms1) similarly we insert the input of the second array into the multiset (ms2) now we store the common elements in the ms1 and ms2 into the common named multiset. now we make conditions: 1. element not present in either ms1 or ms2 we return false 2. element either present in the first array but not in the second array we increase its count1 and remove the item. 3.similarly we do for other one element is present in the second array but not in the first array we increase its count2 and remove the item.

now for the case where its the common elemnt in both of the arrays we can condition it like if(common.size() < (k/2-cnt1)+(k/2-cnt2)) where we check is remaining elements of both the array sum is equal to the common array size. If then print the yes else no.

Your code here...

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define endl "\n"
#define pb push_back
#define mod 1000000007
#define N 500010
#define inf 100000000000000000

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int testcases = 1;
    cin>>testcases;
    while (testcases--)
    {
        // 1. keep it simple
        int n,m,k;
        cin>>n>>m>>k;
        int a[n], b[m];// array to store the two array in input
        multiset<int>ms1, ms2, common;// set to store the unique elements
        
        // insert each element from input to arr1
        for(int i=0;i<n;i++)
        {
        	cin>>a[i];
        	ms1.insert(a[i]);
        }
        // insert each element from input to arr2
        for(int i=0;i<m;i++)
        {
        	cin>>b[i];
        	ms2.insert(b[i]);
        	
        	// if the element of the b(arr2) is present in arr1 then insert it the common
        	if(ms1.find(b[i])!=ms1.end())
        	{
        		common.insert(b[i]);
        	}
        }
        
        bool ans=true;
        // Maintain the two count
        int cnt1=0, cnt2=0;
        for(int i=1;i<=k;i++)
        {
            // if element if not present in the both array return false
        	if(ms1.find(i)==ms1.end() and ms2.find(i)==ms2.end())
        	{
        		ans=false;
        		break;
        	}
        	// if common if take this case later
        	if(common.find(i)!=common.end())
        	{
        		continue;
        	}
        	// if found in either of the array then increas the respective count and remove that no 
        	else if(ms1.find(i)!=ms1.end())
        	{
        		cnt1++;
        		ms1.erase(ms1.find(i));
        	}
        	else
        	{
        		cnt2++;
        		ms2.erase(ms2.find(i));
        	}
        	
        	// If the count of any array is greater than the k/2 return false
        	if(cnt1 >k/2 or cnt2>k/2)
        	{
        		ans=false;
        		break;
        	}
        }
        // now for common 
        // remaining elements1(k/2-cnt1) + remaining elements2(k/2-cnt2)  if not equal to the common size return false
        if(common.size() < (k/2-cnt1)+(k/2-cnt2))
        	ans=false;
        if(ans)
        	cout<<"YES"<<endl;
        else
        	cout<<"NO"<<endl;
    }
}
  • Vote: I like it
  • -13
  • Vote: I do not like it