# Set Union

@touhidur I believe @Abdullah_1234 meant that he needs the last loop to print the output. Please resort to polite responses only.

The list `y` doesn’t need sorting. It is being built from `x` and `x` is sorted. The way line 6-8 has been written, `y` is guaranteed to be sorted already.

The actual issue with the code is how the output is being printed. In a case like this:

``````2 2
1 2
1 2
``````

The code will produce this:

``````1 2 2
``````

@Abdullah_1234 Now you can try to debug and fix this issue. Give it a try.

1 Like

Oh! Sorry!
@Abdullah_1234 I didn’t specifically meant the last loop.
I wanted to mean drop the if statement in the last loop.
And also sort the list y before that.
Miscalculations from both side I think.

y is guaranteed to be sorted already but what @Abdullah_1234 is doing is appending new elements of x in its end.
So, he requires to sort it again.
I tested it before commenting.

Are you sure, bhaia? Because, these tree lines of code:

``````for i in x:
if i not in y:
y.append(i)
``````

Is doing the following:

• Take each element from X
• If it satisfies a condition, append it to the back of Y:

Now, if X is originally sorted, then Y will be sorted at the end for sure.

Let’s take this list for example:

``````[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
``````

If you take each element from here from left to right, and push the odd ones to a new list, the new list will have the odd numbers in sorted order. Shouldn’t it?

``````[1]
[1, 3]
[1, 3, 5]
[1, 3, 5, 7]
[1, 3, 5, 7, 9]
``````

I didn’t notice that he took an empty list for calculations.
Hmm, It will work even without sorting.
Btw, @hjr265 bro, It is a long time since I have argued over codes.
Hope to argue with you again.
Can you help me with a problem of mine after the end of the mock contest?

Hahah! Sure. Let the contest end first, and we can discuss in the other thread.

1 Like
``````#include <stdio.h>

int main()
{
int N, M, i, k, j, t;

scanf("%d %d", &N, &M);

int ara1[N + M], ara2[M];

for(i = 0; i < N; i++) {
scanf("%d", &ara1[i]);
}

for(j = 0; j < M; j++) {
scanf("%d", &ara2[j]);
}

for(j = 0; j < M; j++) {
for(k = 0; k < N; k++) {
if(ara2[j] == ara1[k]) {
break;
}
}

if(k == N) {
ara1[i] = ara2[j];
i++;
}
}

for(j = 0; j < i-1; j++) {
for(k = j+1; k < i; k++) {
if(ara1[k] < ara1[j]) {
t = ara1[j];
ara1[j] = ara1[k];
ara1[k] = t;
}
}
}

for(k = 0; k < i; k++) {
printf("%d ", ara1[k]);
}

return 0;
}
``````

THE TEST SHOWS “WRONG ANSWER” AFTER GIVING THIS CODE, WHY?

@arpon.roy In the code that you have shared here, you are printing an extra space at the end of the last integer.

But I noticed that you already figured that out and was still getting wrong answer.

Turns out there was an issue in the dataset. It has been fixed now and all “wrong answer” submissions for this problem are being rejudged.

``````#include <iostream>
#include<algorithm>
using namespace std;

int main()
{
int a, b, m[300], n[120], i;
cin>>a>>b;
for(i=0; i<a; i++)
{
cin>>m[i];
}
for(i=0; i<b; i++)
{
cin>>n[i];
}
for(i=a; i<(a+b); i++)
{
m[i]=n[i-a];
}
sort(m, m+(a+b));

for(i=0; i<(a+b); i++)
{
if(m[i]== m[i-1])
{
continue;
}
else
{
cout<<m[i]<<" ";
}
}
cout<<"\b";
return 0;
}
``````

``````cout << "\b";
^^
``````

Never print a backspace in an online judge !!!
It is counted as an extra character but not shown in the output !!!

1 Like

@touhidur Thank you. Solved it.

Why does your code use `srand` and `rand`?

I see that your latest submission didn’t pass the sample test case. Does your code give the correct result for the sample test case?

``````5 5
1 3 5 7 8
2 3 4 7 9
``````

The expected output is

``````1 2 3 4 5 7 8 9
``````

``````2 3 4 5 7 8 9
``````

Make sure you are running the compiler with optimization level 2. You can see the GCC/G++ flags used by Toph here: Languages | Toph

1 Like

Thanks bro.
I was absent for quite a few days because I had to study to complete my syllabus for the upcoming exam.
Hope you will also help in the future

Thanks bro.
Sorry for being late😅

1 Like

I cannot find the problem

``````def set_union(set1={},set2={}):
for element in set2:
sorted_list=sorted(set1)
result=' '.join(map(str,sorted_list))
return result.strip()

set_count=input()
set1=set(input().split())
set2=set(input().split())

print(set_union(set1,set2))
``````

can u please find problem in this one? It is just passing sample case

``````a,b=input().split()
c=input()
d=input()
if len(c)==2*int(a)-1 and len(d)==2*int(b)-1:
e=set(c)
f=set(d)
g=(e|f)
h=list(g)
h.sort()
h.remove(" ")
string=""
for element in h:
string=string+element
string=string+" "
i=list(string)
i.pop()
str1=""
for elements in i:
str1=str1+elements
print(str1)
``````

why doesn’t this work?

``````#include<stdio.h>
int main()
{
int a, b, i;
scanf("%d %d", &a, &b);
int a1[a], a2[b];
for(i=0;i<a;i++)
scanf("%d", &a1[i]);
for(i=0;i<b;i++)
scanf("%d", &a2[i]);
int m=0, n=0;
while(m<a && n<b) {
if(a1[m] < a2[n]) {
printf("%d ", a1[m]);
m++;
}
else if(a2[n] < a1[m]){
printf("%d ", a2[n]);
n++;
}
else {
printf("%d ", a1[m]);
m++;
n++;
}
}
while(m<a) {
if(m==(a-1))
printf("%d", a1[m++]);
else
printf("%d ", a1[m++]);
}
while(n<b) {
if(n==(b-1))
printf("%d", a2[n++]);
else
printf("%d ", a2[n++]);
}
return 0;
}
``````
``````import java.util.Arrays;
import java.util.Scanner;
class Main_practice{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
sc.nextLine();
String l1 = sc.nextLine();
String l2 = sc.nextLine();
String array1 [] = l1.split(" ");
String array2 [] = l2.split(" ");
String sa [] = new String [array1.length+array2.length];// new array having length of (array1.length + array2.length)

for(int i = 0;i<array1.length;i++){ // taking all the values of array1 in sa array
sa[j++] = array1[i];
}

for(int i=0;i<array2.length;i++){ //// taking all the values of array2 in sa array
sa[j++] = array2[i];
}

Arrays.sort(sa);// sorting the sa array in  in increasing order.

for(int i=1;i<sa.length;i++){// iterating the sa array from 1 index, as i will be checking the previous value in the array is same with sa[i] or not. if not same then i will print the sa[i-1]
if(i<sa.length-1){// checking this if it is the index before the last index of sa array,if so then i will print based on the condition in the same line with a space
if(!sa[i].equals(sa[i-1])){ // star
System.out.print(sa[i-1]+" ");
}
}

else{
if(!sa[i].equals(sa[i-1])){// if last index and its previous index's value is not same, then i will print sa[i] and sa[i-1] both else the a[i-1] index's value
System.out.println(sa[i-1]+" "+sa[i]);
}
else{
System.out.println(sa[i-1]);
}
}
}
}
}[quote="emo_19301245, post:52, topic:1690, full:true"]
import java.util.Arrays;
import java.util.Scanner;
class Main_practice{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
sc.nextLine();
String l1 = sc.nextLine();
String l2 = sc.nextLine();
String array1 [] = l1.split(" ");
String array2 [] = l2.split(" ");
String sa [] = new String [array1.length+array2.length];// new array having length of (array1.length + array2.length)

for(int i = 0;i<array1.length;i++){ // taking all the values of array1 in sa array
sa[j++] = array1[i];
}

for(int i=0;i<array2.length;i++){ //// taking all the values of array2 in sa array
sa[j++] = array2[i];
}

Arrays.sort(sa);// sorting the sa array in  in increasing order.

for(int i=1;i<sa.length;i++){// iterating the sa array from 1 index, as i will be checking the previous value in the array is same with sa[i] or not. if not same then i will print the sa[i-1]
if(i<sa.length-1){// checking this if it is the index before the last index of sa array,if so then i will print based on the condition in the same line with a space
if(!sa[i].equals(sa[i-1])){ // star
System.out.print(sa[i-1]+" ");
}
}

else{
if(!sa[i].equals(sa[i-1])){// if last index and its previous index's value is not same, then i will print sa[i] and sa[i-1] both else the a[i-1] index's value
System.out.println(sa[i-1]+" "+sa[i]);
}
else{
System.out.println(sa[i-1]);
}
}
}
}
}
``````

This is showing WA for sample test 4. why?