two arrays, find the missing numbers
Your program seems to be outputting the correct result. However, I felt that I need to refactor your code to improve its readability and remove the bad practices used in it.
The below is the same as your code with a bit of improvement:
#include <iostream>
#include <array>
#include <limits>
int main( )
{
std::array<int, 1201> arr1; // use std::array instead of raw arrays
std::array<int, 1201> arr2;
std::size_t arr1_size { }; // renamed n
std::size_t arr2_size { }; // renamed m
std::cin >> arr1_size >> arr2_size;
if ( arr2_size == 0 ) // this if statement should be here to help end
{ // the program early on to prevent the execution
// of the for-loops
std::cout << "There are no missing numbers.\n";
return 0;
}
for ( std::size_t idx { }; idx < arr1_size; ++idx ) // use std::size_t
{ // for the loop counters
std::cin >> arr1[ idx ];
}
for ( std::size_t idx { }; idx < arr1_size - arr2_size; ++idx )
{
std::cin >> arr2[ idx ];
}
for ( std::size_t arr1_idx { }; arr1_idx < arr1_size; ++arr1_idx )
{
bool isNumberMissing { true }; // this should be of type bool
for ( std::size_t arr2_idx { }; arr2_idx < arr1_size - arr2_size; ++arr2_idx )
{
if ( arr1[ arr1_idx ] == arr2[ arr2_idx ] )
{
isNumberMissing = false;
// this is my trick for solving your code's bug
arr2[ arr2_idx ] = std::numeric_limits<int>::min( );
break; // break here to improve performance
}
}
if ( isNumberMissing )
{
std::cout << arr1[ arr1_idx ] << " ";
}
}
std::cout << '\n';
}
Sample input/output #1:
7 3
12 34 45 29 100 87 32
100 87 12 34
45 29 32
Sample input/output #2:
7 3
2 6 1 9 3 2 4
4 1 2 3
6 9 2
Note: See Why is "using namespace std;" considered bad practice?