Codechef Long challenge
JULY 2020
PROBLEM - Chefina and swaps (CHFNSWPS)
PROBLEM DESCRIPTION

Let us understand the problem with the help of example..!!
Arr1 = [1,1]
Arr2 = [2,2]
Example 1:
Let us understand the problem with the help of example..!!
Arr1 = [1,1]
Arr2 = [2,2]
Now we want arrays to be equal..!!
Example 1:
Let us understand the problem with the help of example..!!
Arr1 = [1,1]
Arr2 = [2,2]
Now we want arrays to be equal..!!
Example 1:
Let us understand the problem with the help of example..!!
Arr1 = [1,1]
Arr2 = [2,2]
Now we want arrays to be equal..!!
Arr1 = [1,2]
Arr2 = [1,2]
Example 1:
Let us understand the problem with the help of example..!!
Arr1 = [1,1]
Arr2 = [2,2]
Now we want arrays to be equal..!!
Arr1 = [1,2]
Arr2 = [1,2]
Cost : min(1,2) = 1
Example 1:
Let us understand the problem with the help of example..!!
Arr1 = [1,1]
Arr2 = [2,2]
Now we want arrays to be equal..!!
Arr1 = [1,2]
Arr2 = [1,2]
Cost : min(1,2) = 1
Hence in cost 1 we can make array identical..!!
Example 1:
Let us understand the problem with the help of example..!!
Arr1 = [1,2]
Arr2 = [2,1]
Example 2:
Let us understand the problem with the help of example..!!
Arr1 = [1,2]
Arr2 = [2,1]
Simply sort the array in non-decreasing manner..!!
Example 2:
Let us understand the problem with the help of example..!!
Arr1 = [1,2]
Arr2 = [2,1]
Simply sort the Arr2 in non-decreasing manner..!!
Arr1 = [1,2]
Arr2 = [1,2]
Hence in 0 operations we can get the identical arrays..!!
Example 2:
Observations from the problem..!!
1. Each and every unique number must be present even times in both the arrays. If not then the answer will be "-1".
Observations from the problem..!!
1. Each and every unique number must be present even times in both the arrays. If not then the answer will be "-1".
Why??
Example : arr1 = [1,1,2,2]
arr2 = [3,3,4,4]
Freq[1] = 2, Freq[2] = 2, Freq[3] = 2, Freq[4] = 2
Observations from the problem..!!
1. Each and every unique number must be present even times in both the arrays. If not then the answer will be "-1".
Why??
Example : arr1 = [1,1,2,2]
arr2 = [3,3,4,4]
Freq[1] = 2, Freq[2] = 2, Freq[3] = 2, Freq[4] = 2
Example : arr1 = [1,2,3,4]
arr2 = [1,2,3,4]
Observations from the problem..!!
1. Each and every unique number must be present even times in both the arrays. If not then the answer will be "-1".
Why??
Example : arr1 = [1,1,2,2]
arr2 = [3,3,3,4]
Freq[1] = 2, Freq[2] = 2, Freq[3] = 3, Freq[4] = 1
Observations from the problem..!!
1. Each and every unique number must be present even times in both the arrays. If not then the answer will be "-1".
Why??
Example : arr1 = [1,1,2,2]
arr2 = [3,3,3,4]
Freq[1] = 2, Freq[2] = 2, Freq[3] = 3, Freq[4] = 1
No possible way present..!!!!!!
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Let us suppose we have given arrays
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Let us suppose we have given arrays
Arr1 = [1,1,1,1,2,2]
Arr2 = [2,2,2,2,1,1]
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Let us suppose we have given arrays
Arr1 = [1,1,1,1,2,2]
Arr2 = [2,2,2,2,1,1]
Arr1 =>Freq[1] = 4, Freq[2] = 2
Arr2 =>Freq[1] = 2, Freq[2] = 4
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Arr1 = [1,1,1,1,2,2]
Arr2 = [2,2,2,2,1,1]
Arr1 =>Freq[1] = 4, Freq[2] = 2
Arr2 =>Freq[1] = 2, Freq[2] = 4
Now some how we need to make these frequencies equal..!! But how we can do that

Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Arr1 = [1,1,1,1,2,2]
Arr2 = [2,2,2,2,1,1]
Arr1 =>Freq[1] = 4, Freq[2] = 2
Arr2 =>Freq[1] = 2, Freq[2] = 4
Take the absolute difference of the frequencies and divide the extra elements to both arrays equally..!!
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Arr1 = [1,1,1,1,2,2]
Arr2 = [2,2,2,2,1,1]
Arr1 =>Freq[1] = 4, Freq[2] = 2
Arr2 =>Freq[1] = 2, Freq[2] = 4
abs(4-2) = 2
abs(2-4) = 2
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Arr1 = [1,1,1,1,2,2]
Arr2 = [2,2,2,2,1,1]
Arr1 =>Freq[1] = 4, Freq[2] = 2
Arr2 =>Freq[1] = 2, Freq[2] = 4
abs(4-2) = 2
abs(2-4) = 2
2/2 = 1
2/2 = 1
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Arr1 = [1,1,1,2,2,2]
Arr2 = [2,2,2,1,1,1]
Arr1 =>Freq[1] = 3, Freq[2] = 3
Arr2 =>Freq[1] = 3, Freq[2] = 3
abs(4-2) = 2
abs(2-4) = 2
2/2 = 1
2/2 = 1
Observations from the problem..!!
2. How many element frequency of a particular number needs to be swapped..!!
Arr1 = [1,1,1,2,2,2]
Arr2 = [1,1,1,2,2,2]
Arr1 =>Freq[1] = 3, Freq[2] = 3
Arr2 =>Freq[1] = 3, Freq[2] = 3
abs(4-2) = 2
abs(2-4) = 2
2/2 = 1
2/2 = 1
Observations from the problem..!!
3. From which element to swap to get the minimum cost??
Observations from the problem..!!
3. From which element to swap to get the minimum cost??
Tricky Part..!!
Observations from the problem..!!
3. From which element to swap to get the minimum cost??
Tricky Part..!!
Have a closer look..!!

Observations from the problem..!!
3. From which element to swap to get the minimum cost??
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [2,2,2,2,1,1]
Arr2 = [1,1,1,1,2,2]
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [2,2,2,2,1,1]
Arr2 = [1,1,1,1,2,2]
vec1 = []
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [2,2,2,2,1,1]
Arr2 = [1,1,1,1,2,2]
vec1 = [1]
freq[1]arr1 = 2, freq[1]arr2 = 4 => abs(2-4) = 2 => 2/2 = 1
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [2,2,2,2,1,1]
Arr2 = [1,1,1,1,2,2]
vec1 = [1,2]
freq[1]arr1 = 2, freq[1]arr2 = 4 => abs(2-4) = 2 => 2/2 = 1
freq[2]arr1 = 4, freq[2]arr2 = 4 => abs(4-2) = 2 => 2/2 = 1
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [2,2,2,2,1,1]
Arr2 = [1,1,1,1,2,2]
vec1 = [1,2]
min(1,2) = 1
Answer = 1
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [2,2,2,2,1,1]
Arr2 = [1,1,1,1,2,2]
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [2,2,2,1,1,1]
Arr2 = [1,1,1,2,2,2]
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [2,2,2,1,1,1]
Arr2 = [1,1,1,2,2,2]
After sorting
Observations from the problem..!!
The first idea that comes into the mind is..!! Get all the elements which needs to be swapped in an array and swap the minimum element with maximum element to get the overall minimum cost..!!
Arr1 = [1,1,1,2,2,2]
Arr2 = [1,1,1,2,2,2]
After sorting
Observations from the problem..!!
Are we done??
Anything more left..!!
Observations from the problem..!!
You will get WA in some test case for the approach we discussed ..!! Let me explain you why??
Observations from the problem..!!
You will get WA in some test case for the approach we discussed ..!! Let me explain you why??
Because of the intermediate element..!!
Approach
Rather than swapping the minimum element in the array with the maximum element..!!
We can also swap with an intermediate element twice to set the array
Approach
Rather than swapping the minimum element in the array with the maximum element..!!
We can also swap with an intermediate element twice to set the array
Lets see how?? .. With the help of an example..!!
Approach
Example :
Arr1 = [2,2,3,3,10,10]
Arr2 = [2,2,3,3,20,20]
Approach
Example :
Arr1 = [2,2,3,3,10,10]
Arr2 = [2,2,3,3,20,20]
Approach
Example :
Arr1 = [2,2,3,3,10,10]
Arr2 = [2,2,3,3,20,20]
Instead of directly swapping 10 and 20... what we can do is.. we can swap 10 with 2..!!
Approach
Example :
Arr1 = [2,2,3,3,2,10]
Arr2 = [10,2,3,3,20,20]
Instead of directly swapping 10 and 20... what we can do is.. we can swap 10 with 2..!!
Approach
Example :
Arr1 = [2,2,3,3,2,10]
Arr2 = [10,2,3,3,20,20]
Now cost = 2
Approach
Example :
Arr1 = [2,2,3,3,20,10]
Arr2 = [10,2,3,3,2,20]
Now cost = 2
Now we can swap 20 with 2.
Approach
Example :
Arr1 = [2,2,3,3,10,20]
Arr2 = [2,2,3,3,10,20]
The overall cost reduces from 10 to 4.
After sorting arrays become identical..!!
Approach
Example :
Arr1 = [2,2,3,3,10,20]
Arr2 = [2,2,3,3,10,20]
The trick involved in this problem was of Intermediate element..!!
And the intermediate element will always be the minimum element from both the arrays..!!
Approach
Summarizing the approach :
1. Take the frequency of all the elements , if the frequency of any element is odd then answer is "-1".
2. Get the difference of frequency of elements and divide it by two (for dividing equally to both arrays) and store it a vector.
3. Sort the vector and run till its (size/2) and get the min(2*minimum_val, vector[i]) and add it to overall sum.
Code
int n;
cin >> n;
long long int* A = new long long int[n];
long long int* B = new long long int[n];
map<long long int, long long int> mp1;
map<long long int, long long int> mp2;
unordered_set<long long int> s;
vector<long long int> v1;
long long int mini = INT_MAX;
for(int i =0; i<n; i++)
{
cin >> A[i];
mini = min(A[i],mini);
mp1[A[i]]++;
s.insert(A[i]);
}
for(int i = 0; i<n;i++)
{
cin >> B[i];
mini = min(B[i],mini);
mp2[B[i]]++;
s.insert(B[i]);
}
Code
int cas = 0;
long long int diff = 0;
for(auto i : s)
{
long long int aa = 0;
long long int bb = 0;
if(mp1.find(i)!=mp1.end())
aa = mp1[i];
if(mp2.find(i)!=mp2.end())
bb = mp2[i];
if((aa+bb)%2!=0)
cas = 1;
diff = abs(aa-bb)/2;
for(int j = 0; j<diff; j++)
{
v1.push_back(i);
}
}
long long int sum =0;
if(cas!=1)
{
sort(v1.begin(),v1.end());
for(int i = 0; i<v1.size()/2; i++) //[1,2,3,4] => [1,2]
{
sum += min(2*mini,v1[i]);
}
}
if(cas == 1)
cout << "-1" << endl;
else
cout << sum << endl;
} Still, Have any Doubts???
Still, Have any Doubts???
Comment it You will get a reply
OR
Send your doubt to email provided in description.
Copy of Codechef Cook off
By Chirayu Jain
Copy of Codechef Cook off
- 58