The element are not in order

2 strings of numbers : n and n-1 .

The elements in the second string will NOT be in the same order as those in the first, but all the values in the second string are in the first.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
32 34 89 -67 45 21 34 5 9 7
34 32 45 89 34 21 5 7 9
Output: -67

My code is good only if the elements are in order.

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() {
    int n, i, v[1000], u[1000];
    cin >> n;
    for (i = 1; i <= n; i++) {
        cin >> v[i];
    }
    for (i = 1; i <= n; i++) {
        cin >> u[i];
        while (v[i] != u[i]) {
            cout << v[i];
            i++;

        }
    }
    return 0;
}
Last edited on
@bstroe,

In your other thread you were citing lots of examples where the order clearly mattered. So it was necessary to scan sequentially to pick up the missing element(s).
Now you've changed your problem so that it doesn't, so your code won't work.

If the elements in the second string aren't in the same order as in the first then you can go to using multisets as @JLBorges did in your previous thread (multisets to allow for duplicates). Or you could read and sort your arrays and then run your comparators.

But you can't expect code written to solve one problem to work for a completely different problem.

Basically: stop changing your problem!
read first "string" to collection 'all'
for each x in second "string"
  if x is in all
  then remove x from all
show all
I suspect these are exercisers set by a book/site - where the problem parameters are changed to keep making the problem 'harder'.
@lastchance,

This is another problem
Why use 2 sets? Why not just:

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
#include <iostream>
#include <set>

int main()
{
	int n {}, m {1};
	std::multiset<int> Su;

	std::cin >> n; // , m;

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

	for (int j {}, x {}; j < n - m; ++j) {
		std::cin >> x;
		Su.erase(x);
	}

	if (Su.size() != m)
		std::cout << "Summat's up!\n";

	for (const auto e : Su)
		std::cout << e << ' ';
}



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

Why use 2 sets? Why not just:

The problem was the repeated values. I think (but could be wrong) that S.erase(x) will remove ALL the values x, not just one of them, making it difficult to match the repeat counts.
(Sorry - I pulled the plug on that post before you got a chance to post yours.)



With multisets and set_difference():
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 <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
   int n, x;
   int m = 1;      // Likely to be changed in the next incarnation of the problem
   multiset<int> Su, Sv;

   cin >> n;
   for ( int i = 0; i < n  ; i++ ) { cin >> x;   Su.insert( x ); }
   for ( int j = 0; j < n-m; j++ ) { cin >> x;   Sv.insert( x ); }

   vector<int> diff;
   set_difference( Su.begin(), Su.end(), Sv.begin(), Sv.end(), back_inserter( diff ) );

   if ( diff.size() != m )
   {
      cout << "Summat's up!\n";
   }
   else                  
   {
      for ( auto e : diff ) cout << e << ' ';
   }
}



Or with sorted arrays (which you could probably run set_difference on if you preferred):
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 <iostream>
#include <algorithm>
using namespace std;

int main()
{
   const int SIZE = 1000;
   int n, u[SIZE], v[SIZE];
   int m = 1, missing = 0;
   cin >> n;
   for ( int i = 0; i < n  ; i++ ) cin >> u[i];
   for ( int j = 0; j < n-m; j++ ) cin >> v[j];
   sort( u, u + n     );
   sort( v, v + n - m );
   for ( int i = 0, j = 0; i < n; i++, j++ )
   {
      while ( i < n &&    ( j >= n-m || v[j] != u[i] )  )   // messy to deal with largest missing
      {
         cout << u[i] << ' ';
         missing++;
         i++;
      }
   }
   if ( missing != m ) cout << "Summat's up!\n";
}

Last edited on
The problem was the repeated values. I think (but could be wrong) that S.erase(x) will remove ALL the values x, not just one of them, making it difficult to match the repeat counts.


:) :)
In which case, 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
26
27
28
#include <iostream>
#include <set>

int main()
{
	int n {}, m {1};
	std::multiset<int> Su;

	std::cin >> n; // , m;

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

	for (int j {}, x {}; j < n - m; ++j) {
		std::cin >> x;

		if (const auto it {Su.find(x)}; it != Su.end())
			Su.erase(it);
	}

	if (Su.size() != m)
		std::cout << "Summat's up!\n";

	for (const auto e : Su)
		std::cout << e << ' ';
}



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

Topic archived. No new replies allowed.