Missing element

2 strings of numbers : n and n-1 .
The elements in the second string will be in the same order as those in the first, except for one element in the first string that will be missing from the second. Identifies the missing item.
On the first line you will find the number n, and on the next two lines the elements of the two strings.
The program will display on the screen the number X, which shows the missing element in the second row.

Input :
10
34 32 45 -67 89 21 34 5 7 9
34 32 45 89 21 34 5 7 9
Output: -67


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>

using namespace std;

int main() {
    int n, i, j, v[1000];
    cin >> n;
   
    for (i = 1; i <= n; i++) {
        cin >> v[i];
    }
    for (j = 1; j < n; j++) {
        cin >> v[j];
    }
    for (i = 1; i <= n; i++) {
     if (v[i] != v[j]) {
        cout << v[i] << " ";
    }
}

    return 0;
}
Using a set consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <set>
#include <iostream>

int main()
{
	int n {};
	std::set<int> si;

	std::cin >> n;

	for (int i {}, v {}; i < n; ++i) {
		std::cin >> v;
		si.insert(v);
	}

	for (int i {}, v {}; i < n - 1; ++i) {
		std::cin >> v;
		si.erase(v);
	}

	if (si.size() != 1)
		std::cout << "Incorrect input\n";
	else
		std::cout << *si.begin() << '\n';
}



10
34 32 45 -67 89 21 34 5 7 9
34 32 45 89 21 34 5 7 9
-67

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
   const int SIZE = 1000;
   int n, u[SIZE], v;
   cin >> n;
   for ( int i = 0; i < n; i++ ) cin >> u[i];
   for ( int i = 0; i < n; i++ )
   {
      cin >> v;
      if ( v != u[i] )
      {
         cout << u[i] << '\n';
         return 0;
      }
   }
}

Last edited on
Thanks lastchace!
But if there are more missing numbers?
EX:
Input :
7 3
12 32 45 87 100 29 34
12 87 100 34
Output:
32 45 29
And if numbers are not missing cout << " Numbers are nor missing";
What does 7 3 mean? 7 for the number of numbers for first list. What's the second - number of missing numbers??
Last edited on
Assuming 3 means 3 missing numbers, consider using a set:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <set>
#include <iostream>

int main()
{
	int n {}, n1 {};
	std::set<int> si;

	std::cin >> n >> n1;

	for (int i {}, v {}; i < n; ++i) {
		std::cin >> v;
		si.insert(v);
	}

	for (int i {}, v {}; i < n - n1; ++i) {
		std::cin >> v;
		si.erase(v);
	}

	if (si.empty())
		std::cout << "Numbers are not missing";
	else
		for (const auto& i : si)
			std::cout << i << ' ';

	std::cout << '\n';
}

bstroe wrote:
But if there are more missing numbers?


Then you've changed the problem, haven't you. You did originally say
2 strings of numbers : n and n-1 .



And the answer would be ambiguous. What are the missing numbers here:
7 4
1 2 3 4 6 5 3
1 2 3

Is the answer
4 6 5 3  (from 1 2 3 [4 6 5 3] )

or is it
3 4 6 5 (from 1 2 [ 3 4 6 5 ] 3 )

?

And are the numbers allowed to be duplicated? Does the order of output matter?
Last edited on
2 strings of numbers : n and n-m (n = packages sent, n-m = packages received)

The elements in the second vector will be in the same order as those in the first, except for m elements in the first string that will be missing from the second. Identify the missing elements, taking into account that they are not necessarily in consecutive positions in the first string.
7 3
12 32 45 87 100 29 34
12 87 100 34
Output:
32 45 29
And if numbers are not missing cout << " Numbers are nor missing";


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
   const int SIZE = 1000;
   int n, m, u[SIZE], v, missing = 0;
   cin >> n >> m;
   for ( int i = 0; i < n; i++ ) cin >> u[i];
   for ( int i = 0; i < n; i++ )
   {
      cin >> v;
      while ( i < n && v != u[i] )
      {
         cout << u[i] << ' ';
         missing++;
         i++;
      }
   }
   if ( m != missing ) cout << "Incorrect number missing";
}


a.exe < in
32 45 29 


Your problem is still ambiguous if duplicates are allowed and the output order matters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>

int main()
{
    std::multiset<int> first  { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 8 } ;
    std::multiset<int> second { 1, 2, 2, 3,       4, 5,    6, 7,          8 } ; // mising two 3s, one 5 and three 7s

    std::cout << "numbers that are missing in the second set are: " ;
    std::set_difference( first.begin(), first.end(), second.begin(), second.end(),
                         std::ostream_iterator<int>( std::cout, " " ) ) ; // 3 3 5 7 7 7
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/731cef3d18e9400f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
   const int SIZE = 1000;
   int n, m, u[SIZE], v, missing = 0;
   cin >> n >> m;
   for ( int i = 0; i < n; i++ ) cin >> u[i];
   for ( int i = 0; i < n; i++ )
   {
      cin >> v;
      while ( i < n && v != u[i] )
      {
         cout << u[i] << ' ';
         missing++;
         i++;
      }
   }
   if ( m != missing ) cout << "Incorrect number missing";
}

Edit & Run


a.exe < in
32 45 29



The code is good but for ex :
Input: 5 2
1 2 3 4 5
1 2 3

should display Output : 4 5 but display nothing
Last edited on
@bstroe,

Here's the input file, in:
5 2
1 2 3 4 5
1 2 3


Then, I can assure you that
a.exe < in
produces
4 5




Perhaps you can enlighten us once and for all whether the order of the output (or even the input) matters, since that will govern whether you can use sets or not.

Last edited on
@lastchance. It works when cin is redirected to be from a file (as above), but not when input is from the keyboard. The issue is cin>> v. With file input, this fails but with keyboard input it waits for input which isn't coming!
It works when cin is redirected to be from a file (as above), but not when input is from the keyboard. The issue is cin>> v. With file input, this fails but with keyboard input it waits for input which isn't coming!


Ah, my bad! Thanks for finding the error.

I think the following will work (but since I tend to do my debugging by redirecting input files I could be wrong ...).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
   const int SIZE = 1000;
   int n, m, u[SIZE], v, missing = 0;
   cin >> n >> m;
   for ( int i = 0; i < n; i++ ) cin >> u[i];
   for ( int i = 0, j = 0; i < n; i++, j++ )
   {
      if ( j < n - m ) cin >> v;
      while ( i < n && ( j >= n - m || v != u[i] ) )
      {
         cout << u[i] << ' ';
         missing++;
         i++;
      }
   }
   if ( m != missing ) cout << "Incorrect number missing";
}
:) :)
Thanks @seeplus @lastchance !
Topic archived. No new replies allowed.