HackerRank says ~no response on stdout~. C++ - c++

I wrote this solution for the absolute permutation problem on HackerRank. It works fine on dev-C++ but doesn't work on Hackerrank. I've found that the code produces output when I remove the abs_perm(). What's the problem here?
#include <iostream>
using namespace std;
int arr[100000];
int check(int n, int k)
{
if ( (2*k == n) || (k == 0) || (n - 4*k == 0) )
return 1;
else if (k < n/2)
return check(n - 4*k, k);
else
return 0;
}
void swap(int &a, int &b)
{
int c = b;
b = a;
a = c;
}
void ini(int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = i+1;
}
}
void abs_perm(int n, int k)
{
for (int i = 0; i < k; i++)
{
swap(arr[i], arr[k+i]);
}
if (2*k == n)
return;
for (int i = n - 1; i > n - k - 1; i--)
{
swap(arr[i], arr[i-k]);
}
if (n - 4*k == 0)
return;
abs_perm(n - 4*k, k);
}
int main()
{
int T;
cin >> T;
int N[T], K[T];
for (int i = 0; i < T; i++)
{
cin >> N[i] >> K[i];
}
for (int i = 0; i < T; i++)
{
cout << N[i] << " " << K[i] << "\n";
}
for (int i = 0; i < T; i++)
{
if ( !check(N[i], K[i]) )
cout << "-1\n";
else
{
ini(N[i]);
abs_perm(N[i], K[i]);
for (int j = 0; j < N[i]; j++)
{
cout << arr[j] << " ";
}
cout << "\n";
}
}
return 0;
}

Array is a structure to use when you know at compile time the dimension of your structure. What you wrote at the begin in abs_perm() is not correct for standard compilers (in fact you don't know the dimension of your array). You can use a std::vector or a std::list which allocate memory dynamically or (bad solution) you can allocate an array with dimension that certainly contains all elements you will put inside.

Related

Can't find the minimum in an array with respect to maximum from the same array

I wanted to write a program to take two arrays as input and convert the first array such that the difference of maximum value and minimum value from the first array gives the smallest possible number.
I tried to write a code to find a smaller number most closest to the maximum from the array in C++, but the function for finding the minimum works on Codelite, but not on other compilers;
Is there any fix to solve this, either to the code or the compiler?
Here is the code I tried:
#include <iostream>
using namespace std;
void swap(int A[], int B[], int n)
{
int x, y, temp;
for(x=0;x<n;++x)
{
for(y=0;y<n;++y)
{
if(A[x]>B[y])
{
temp = A[x];
A[x] = B[y];
B[y] = temp;
}
}
}
}
void sortas(int A[], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
if (A[i] > A[j+1])
{
temp = A[i];
A[i] = A[j+1];
A[j+1] = temp;
}
}
}
}
int maxfind(int A[], int n)
{
int z, a;
a = A[0];
for(z=0;z<n;++z)
{
if(a<A[z])
{
a = A[z];
}
}
cout << "Max value in A is" << a << endl;
return a;
}
int minfind(int A[], int n, int amax, int amin)
{
int z, maxi;
maxi = amax;
for(z=0;z<n;++z)
{
if(maxi >= A[z])
{
amin = A[z];
}
else
{
maxi = maxi-1;
}
}
cout << "Mix value in A is" << amin << endl;
return amin;
}
int main() {
int z, t;
cout << "Enter number of test cases: ";
cin >> t;
int n, i, j, amax, amin;
for(z=0;z<t;++z)
{
cout << "Enter size of array" << endl;
cin >> n;
int A[n], B[n];
cout << "Enter Array A values:" << endl;
for(i=0;i<n;++i)
{
cin >> A[i];
}
cout << "Enter Array B values:" << endl;
for(j=0;j<n;++j)
{
cin >> B[j];
}
swap(A, B, n);
sortas(A, n);
cout << "Swapped and sorted array is: " << endl;
for(i=0;i<n;++i)
{
cout << A[i] << "\t" << B[i] << endl;
}
amax = 0;
amin = 0;
amax = maxfind(A, n);
amin = minfind(A, n, amax, amin);
}
return 0;
}
Here is the output to that code:
1 1 1 3 4 2
Max value in A is 1 Min value in A is 1
1 2 2
-1882830412 4 3 6 3
Max value in A is 2 Min value in A is -1882830412
The problem is with your bubble sort:
void sortas(int A[], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++) // why not start at i + 1 ??
{
if (A[i] > A[j+1]) // j + 1 is out of bounds when j == n - 1
{
temp = A[i];
A[i] = A[j+1]; // <-- Some random value is written to A.
A[j+1] = temp; // <-- Overwriting some variable on the stack
// of main() (possibly B ?)
}
}
}
}
A correct bubble sort (this is not the pedantic bubble sort), this is probably the most used.
void sortas(int A[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (A[i] > A[j])
std::swap(A[i], A[j]);
}
}
}
The actual bubble sort algorithn (the "pedantic" bubble sort), swaps only occur on neighboring values.
void sortas(int A[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < (n - i) - 1; ++j)
{
if (A[j] > A[j + 1])
std::swap(A[j], A[j + 1]);
}
}
}
Use one or the other, for integers, the performance is identical.

SelectionSort in c++ not sorting*

I am having a tough time trying to follow the logic here as to why it is not working correctly
expected output :
1 5 6 8
any help is greatly appreciated
Update: I got selection sort and insertion sort mixed up
OUTPUT:
unaltered array
5 8 1 6
1 -858993460 -858993460 6
#pragma once
#include <iostream>
using namespace std;
void swap(int &a,int &b)
{
int temp;
temp = b;
b = a;
a = temp;
}
void SelectionSort(int *arr,int n)
{
cout << "SelectionSORT1\n";
int i;
for (i = 0; i < n - 2; i++) //-1 ||-2//
{
int firstIndex;
firstIndex = arr[i];
int j;
for (j = i + 1;j < n - 1;j++)
{
if (arr[j] < arr[firstIndex])
{
firstIndex = j;
//cout << firstIndex;
}
swap(arr[i], arr[firstIndex]);
}
cout << "SelectionSORT2\n";
}
cout << "SelectionSORT3\n";
}
#include <iostream>
#include "SelectionSort.h"
using namespace std;
int main()
{
int array[] = { 5,8,1,6 };
int size = { sizeof(array) / sizeof(array[0]) };
cout << "unaltered array\n";
for (int i = 0; i < 4; i++)
{
cout << array[i] << " ";
}
cout << endl;
SelectionSort(array, size);
for (int i = 0; i < 4; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
UPDATE
#pragma once
#include <iostream>
using namespace std;
void swap(int &a,int &b)
{
int temp;
temp = b;
b = a;
a = temp;
}
void SelectionSort(int *arr,int n)
{
cout << "Selection SORT1\n";
int I;
for (i = 0; i < n ; i++) //-1 ||-2//
{
int firstIndex;
firstIndex = i;
int j;
for (j = i + 1;j < n ;j++)
{
std::cerr << j << ' ' << firstIndex << '\n';
if (arr[j] < arr[firstIndex])
{
firstIndex = j;
}
swap(arr[i], arr[firstIndex]);
}
cout << " \n";
}
cout << " \n";
}
#include <iostream>
#include "BubbleSort.h"
#include "InsertionSort.h"
#include "SelectionSort.h"
using namespace std;
int main()
{
int array[] = { 5,8,1,6 };
int size = { sizeof(array) / sizeof(array[0]) };
cout << "unaltered array\n";
for (int i = 0; i < size; i++)
{
cout << array[I] << " ";
}
cout << endl;
SelectionSort(array, size);
for (int i = 0; i < size; i++)
{
cout << array[I] << " ";
}
cout << endl;
unaltered array
5 8 1 6
SelectionSORT1
1 0
2 0
3 2
2 1
3 2
3 2
5 6 1 8
You are using the selection sort method not the insertion sort method.
Bit in any case the function is incorrect
void InsertionSort(int *arr,int n)
{
cout << "INSERTION SORT1\n";
int i;
for (i = 0; i < n - 2; i++) //-1 ||-2//
{
int firstIndex;
firstIndex = arr[i];
int j;
for (j = i + 1;j < n - 1;j++)
{
if (arr[j] < arr[firstIndex])
{
firstIndex = j;
//cout << firstIndex;
}
swap(arr[i], arr[firstIndex]);
}
cout << "INSERTION SORT2\n";
}
cout << "INSERTION SORT3\n";
}
For starters it will not sort an array that has two elements due to this for loop
for (i = 0; i < n - 2; i++) //-1 ||-2//
Secondly, the variable firstIndex is not initialized by a value of the index i
firstIndex = arr[i];
So the condition in this if statement
if (arr[j] < arr[firstIndex])
does not make a sense.
Thirdly this inner for loop
for (j = i + 1;j < n - 1;j++)
ignores the last element of the array.
Fourth, this unconditional call of the function swap within the inner for loop
swap(arr[i], arr[firstIndex])
also does not make a sense.
The function can look the following way
void SelectionSort( int a[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
size_t min = i;
for ( size_t j = i + 1; j < n; j++ )
{
if ( a[j] < a[min] )
{
min = j;
}
}
if ( min != i ) swap( a[i], a[min] );
}
}
And in main the variable size should have the type size_t - the type of the value of the expression with the operator sizeof
const size_t size = sizeof(array) / sizeof(array[0]);
And instead of the magic number 4 in for loops in main you should use the named constant size or you could use the range-based for loop as
for ( const auto &item : array )
{
cout << item << ' ';
}
cout << endl;
If you indeed mean the insertion sort method then a corresponding function can look for example the following way
void InsertionSort( int a[], size_t n )
{
for (size_t i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
int tmp = a[i];
size_t j = i;
for ( ; j != 0 && tmp < a[j - 1]; --j )
{
a[j] = a[j - 1];
}
a[j] = tmp;
}
}
}
Thank you all for your help
I got it to work like this
#pragma once
#include <iostream>
using namespace std;
void swap(int &a,int &b)
{
int temp;
temp = b;
b = a;
a = temp;
}
void InsertionSort(int arr[],int n)
{
int i;
for (i = 0; i < n ; i++)
{
int firstIndex,j;
firstIndex = i;
for (j = i + 1;j < n ;j++)
{
if (arr[j] < arr[firstIndex])
{
firstIndex = j;
}
}
swap(arr[i], arr[firstIndex]);
}
}
The following is C++:
std::set<int> sorted_array({ 5,8,1,6 });
If you have duplicates and need to keep them, use:
std::multiset<int> sorted_array({ 5,8,1,6 });
Done. One line.

is there other way to fill in array in C++ without vector

So I have array A and B, two of them contain random numbers and I need to write in the C array initially even numbers of A and B and then odd. I have made this wtih vector but I wonder if there is other way to do it like in Javascript there are methods like .unshift(), .push() etc
#include<iostream>
#include<vector>
using namespace std;
int main() {
const int n = 4;
int A[n];
int B[n];
vector<int>C;
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
C.push_back(A[i]);
}
if (B[i] % 2 == 0)
{
C.push_back(B[i]);
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
C.push_back(A[i]);
}
if (B[i] % 2 != 0)
{
C.push_back(B[i]);
}
}
cout << endl;
for (int i = 0; i < C.size(); i++)
cout << C[i] << " ";
}
I would suggest interleaving A and B initially:
for (int i = 0; i < n; i += 1)
{
C.push_back(A[i]);
C.push_back(B[i]);
}
And then partitioning C into even and odd elements:
std::stable_partition(C.begin(), C.end(), [](int i) { return i % 2 == 0; });
vector::push_back is the simplest way to have a collection that grows as you add things to the end.
Since you have fixed size for A and B, you could make them primitive arrays instead, which is what you have done. But for C you don't know how long it will be, so a collection that has a changeable size is appropriate.
You can use std::array, if you know the size you need in compile time. You can then add using an iterator.
#include<vector>
using namespace std;
int main() {
const int n = 4;
int A[n];
int B[n];
std::array<int, n+n>C; // <-- here
auto C_it = C.begin(); // <-- here
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
*C_it++ = A[i]; // <-- here
}
if (B[i] % 2 == 0)
{
*C_it++ = B[i];
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
*C_it++ = A[i];
}
if (B[i] % 2 != 0)
{
*C_it++ = B[i];
}
}
cout << endl;
for (int i = 0; i < C.size(); i++)
cout << C[i] << " ";
}
Alternatively if you want to be more safe you can hold the next unwritten index and access elements with C.at(last++) = A[i], which checks for out-of-bounds and throws an exception instead of UB.
well you don't to change much.
first of declare C array as int C[n+n]; and declare a variable for incrementing through c array as int j=0;
and in if statements of loops do this C[j]=A[i]; j++; for first if and C[j]=B[i]; j++; for the second if statements
int main() {
const int n = 4;
int A[n];
int B[n];
int C[n+n];
int j=0;
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
C[j]=A[i];
j++;
}
if(B[i]%2==0){
C[j]=B[i];
j++;
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
C[j]=A[i];
j++;
}
if (B[i] % 2 != 0)
{
C[j]=B[i];
j++;
}
}
j=0;
cout << endl;
for (int i = 0; i < C[].lenght(); i++)
cout << C[i] << " ";
}

all possible combinations to divide pack of candies

I have problem to solve and I'm stuck, I don't know how to start.
Suppose I have R childrens and S candies. I want to divide candies between childrens. Each child can get 0, 1, 2, 3 or 4 candies. How to find all the possibilities of such a division?
#include <iostream>
using namespace std;
void solve(int r, int s) {
if (s == 0)
{
cout << "no more candies" << endl;
return;
}
if (r == 0)
{
cout << "last child" << endl;
return;
}
for (int j = 0; j < 4 && j <= s; ++j)
{
cout << "r: " << r << " j: " << j << endl;
solve(r-1, s - j);
}
}
int main () {
int r, s;
cin >> r >> s;
solve(r, s);
return 0;
}
For now I have sth like this, I see in output that I have solutions here, but I don't know how to grab and store all possibilities into for example vector.
Just store counts and save variants at the last recursion level
vector<int> counts;
vector<vector<int>> sol;
void solve(int r, int s) {
if (s == 0)
{
sol.push_back(counts);
return;
}
if (r == 0)
{
return;
}
for (int j = 0; j <= 4 && j <= s; ++j)
{
counts[r - 1] += j;
solve(r - 1, s - j);
counts[r - 1] -= j;
}
}
int main() {
int r, s;
r = 3;
s = 5;
for (int j = 0; j < r; ++j)
counts.push_back(0);
solve(r, s);
for (int i = 0; i < sol.size(); i++) {
for (int j = 0; j < sol[i].size(); j++) {
cout << sol[i][j] << ' ';
}
cout << endl;
}
return 0;
}

Check if 2 arrays are same or not (C++)

I want to compare two int arrays and find if they are the same and if they are not i want to find the min and the max number that exist in one but not in the other. I use this code in c++ but seems to run into a segmentation fault 11. I would be grateful if someone points out the mistake to me.I would like to see better solutions if there are any.
+ I did the mergesort for time limit of 1 second.
#include <iostream>
using namespace std;
void merge(int *a,int s,int e)
{
int mid = (s+e)/2;
int i = s;
int j = mid+1;
int k = s;
int temp[100];
while(i<=mid && j<=e)
{
if(a[i] < a[j])
{
temp[k++] = a[i++];
}
else
{
temp[k++] = a[j++];
}
}
while(i<=mid)
{
temp[k++] = a[i++];
}
while(j<=e)
{
temp[k++] = a[j++];
}
for(int i=s;i<=e;i++)
{
a[i] = temp[i];
}
}
void mergeSort(int a[],int s,int e)
{
if(s>=e)
{
return;
}
int mid = (s+e)/2;
mergeSort(a,s,mid);
mergeSort(a,mid+1,e);
merge(a,s,e);
}
int min_array (int array1[],int n1)
{
int min = array1[0];
for(int i=1;i<n1;i++)
if(array1[i] < min)
min = array1[i];
return min;
}
int max_array (int array2[],int n2)
{
int max = array2[0];
for(int i=1;i<n2;i++)
if(array2[i] > max)
max = array2[i];
return max;
}
void check_same(int a[], int b[], int n)
{
bool check = true;
int check1 = 2, check2 = 2, counter1 = 0, counter2 = 0, i, j;
int pos1[n], pos2[n];
mergeSort(a, 0, n);
mergeSort(b, 0, n);
for(i=0; i<n; i++)
{
if (a[i] != b[i])
check = false;
for(j=0; j<n; j++)
{
if (a[i] != b[j])
check1 = 0;
else if (a[i] == b[j])
check1 = 1;
else if (a[j] != b[i])
check2 = 0;
else if (a[j] == b[i])
check2 = 1;
if (check1 == 1 && check2 == 1)
break;
}
if (check1 == 0)
pos1[counter1++] = i;
else if (check2 == 0)
pos2[counter2++] = i;
}
int differents[counter1 + counter2];
if (counter1 < counter2)
{
for (i=0; i<counter1; i++)
differents[i] = a[pos1[i]];
for (i=counter1; i<counter2; i++)
differents[i] = b[pos2[counter2 - i]];
}
else
{
for (i=0; i<counter2; i++)
differents[i] = b[pos2[i]];
for (i=counter2; i<counter1; i++)
differents[i] = a[pos1[counter1 - i]];
}
if (check)
cout << "yes\n";
else if (check == false)
cout << "no " << min_array(differents, counter1+counter2)<< " " << max_array(differents, counter1+counter2) << endl;
}
int main()
{
int N, i;
cin >> N;
int A[50000], B[50000];
for (i=0;i<N;i++)
cin >> A[i];
for (i=0;i<N;i++)
cin >> B[i];
check_same(A, B, N);
}
Your code is not standard C++, the line int pos1[n], pos2[n]; in check_same is invalid because n is not a compile time constant - VLAs are only allowed in C.
You could make use of the standard library for all of that:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
void check_same(int a[], int b[], int n) {
std::sort(a, a + n);
std::sort(b, b + n);
if(std::equal(a, a + n, b)) {
std::cout << "yes\n";
} else {
std::vector<int> elements_not_in_both;
std::set_symmetric_difference(a, a + n,
b, b + n,
std::back_inserter(elements_not_in_both));
auto [min, max] = std::minmax_element(elements_not_in_both.cbegin(),
elements_not_in_both.cend());
std::cout << "no " << *min << " " << *max << '\n';
}
}
int main()
{
int N;
std::cin >> N;
int A[50000], B[50000];
for (int i=0; i<N; i++)
std::cin >> A[i];
for (int i=0; i<N; i++)
std::cin >> B[i];
check_same(A, B, N);
}
Live demo.
An even better solution is to not use C-style arrays either, then you don't allocate way too much stack space for small input arrays and you can't have too little space when someone decides to run this on more than 50000 elements:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
void check_same(std::vector<int>& a, std::vector<int>& b) {
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
if(a == b) {
std::cout << "yes\n";
} else {
std::vector<int> elements_not_in_both;
std::set_symmetric_difference(a.cbegin(), a.cend(),
b.cbegin(), b.cend(),
std::back_inserter(elements_not_in_both));
auto [min, max] = std::minmax_element(elements_not_in_both.cbegin(),
elements_not_in_both.cend());
std::cout << "no " << *min << " " << *max << '\n';
}
}
int main()
{
int N;
std::cin >> N;
std::vector<int> a, b;
a.reserve(N);
b.reserve(N);
std::copy_n(std::istream_iterator<int>(std::cin), N, std::back_inserter(a));
std::copy_n(std::istream_iterator<int>(std::cin), N, std::back_inserter(b));
check_same(a, b);
}
Please check out for these points for solving segmentation fault issue:
merge function
1) Is this statement int k = s; correct? Shouldn't it be int k = 0;
2) Is this allocation int temp[100]; OK? Or Should it be int temp[e - s + 1];
3) Is this statement a[i] = temp[i]; correct? Shouldn't it be a[i] = temp[i - s];
4) Do you need to have base condition s < e or something? i.e. Handling the case when s == e.
check_same function
1) Is this call mergeSort(a, 0, n); correct? Shouldn't it be mergeSort(a, 0, n - 1);
As far as better approach is concerned, it can be solved in O(n) using hashing.
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
void check_same(int a[], int b[], int n) {
int minNotInA, maxNotInA, minNotInB, maxNotInB;
bool elementMissingInA = false, elementMissingInB = false;
{
unordered_set<int> elementsInB;
for (int i = 0; i < n; i++) {
elementsInB.insert(b[i]);
}
for (int i = 0; i < n; i++) {
if (elementsInB.find(a[i]) == elementsInB.end()) {
if (!elementMissingInA) {
elementMissingInA = true;
minNotInB = maxNotInB = a[i];
} else {
if (minNotInB > a[i]) {
minNotInB = a[i];
} else if (maxNotInB < a[i]) {
maxNotInB = a[i];
}
}
}
}
}
if (elementMissingInA) {
unordered_set<int> elementsInA;
for (int i = 0; i < n; i++) {
elementsInA.insert(a[i]);
}
for (int i = 0; i < n; i++) {
if (elementsInA.find(b[i]) == elementsInA.end()) {
if (!elementMissingInB) {
elementMissingInB = true;
minNotInA = maxNotInA = b[i];
} else {
if (minNotInA > b[i]) {
minNotInA = b[i];
} else if (maxNotInA < b[i]) {
maxNotInA = b[i];
}
}
}
}
}
if (elementMissingInA and elementMissingInB) {
cout << "no " << min(minNotInA, minNotInB) << " " << max(maxNotInA, maxNotInB) << "\n";
} else {
cout << "yes\n";
}
}
int main()
{
int N;
std::cin >> N;
int A[50000], B[50000];
for (int i=0; i<N; i++)
std::cin >> A[i];
for (int i=0; i<N; i++)
std::cin >> B[i];
check_same(A, B, N);
return 0;
}
Thank all of you for your interest and your help.
Because i am not used to those kinds of libraries you use and i dont want to study them at the moment(i am just in the first semester of my ece class) i corrected my code(both improved it and fixed the segmentation fault 11)
You can take a look right here.
#include <iostream>
using namespace std;
void merge(int *a, int s, int e)
{
int mid = (s + e) / 2;
int i = s;
int j = mid + 1;
int k = s;
int temp[50000];
while (i <= mid && j <= e)
{
if (a[i] < a[j])
{
temp[k++] = a[i++];
}
else
{
temp[k++] = a[j++];
}
}
while (i <= mid)
{
temp[k++] = a[i++];
}
while (j <= e)
{
temp[k++] = a[j++];
}
for (int i = s; i <= e; i++)
{
a[i] = temp[i];
}
}
void mergeSort(int a[], int s, int e)
{
if (s >= e)
{
return;
}
int mid = (s + e) / 2;
mergeSort(a, s, mid);
mergeSort(a, mid + 1, e);
merge(a, s, e);
}
int min_array(int array1[], int n1)
{
int min = array1[0];
for (int i = 1; i<n1; i++)
if (array1[i] < min)
min = array1[i];
return min;
}
int max_array(int array2[], int n2)
{
int max = array2[0];
for (int i = 1; i<n2; i++)
if (array2[i] > max)
max = array2[i];
return max;
}
void check_same(int a[], int b[], int n)
{
int differents[50000];
int counter1 = 0, counter2 = 0;
int i = 0, j = 0;
while (i < n && j < n)
{
if (a[i] < b[j])
{
differents[counter1++ + counter2] = a[i];
i++;
}
else if (b[j] < a[i])
{
differents[counter2++ + counter1] = b[j];
j++;
}
else
{
i++;
j++;
}
}
if (counter1 == 0 && counter2 == 0)
cout << "yes\n";
else
cout << "no " << min_array(differents, counter1 + counter2) << " " << max_array(differents, counter1 + counter2) << endl;
}
int main()
{
int A[50000], B[50000];
int N, i;
cin >> N;
for (i = 0; i<N; i++)
cin >> A[i];
for (i = 0; i<N; i++)
cin >> B[i];
mergeSort(A, 0, N-1);
mergeSort(B, 0, N-1);
check_same(A, B, N);
return 0;
}