Set Union

#include <stdio.h>

int main()
{
int m,n,i,j,k,temp;

scanf("%d %d",&m,&n);

int ara1[m+n],ara2[n]; //m+n because  i'll copy 2nd array to 1st array

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

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

j=0;

//copying elements of 2nd array to 1st array
for(i=m;i<(m+n);i++){
ara1[i]=ara2[j];
j++;
}

//sorting array in increasing order
for(i=0;i<(m+n);i++){
    for(j=i+1;j<(m+n);j++){
        if(ara1[i]>ara1[j]){
            temp = ara1[i];
            ara1[i]=ara1[j];
            ara1[j] = temp;
        }
		
        //removing 2nd element if there are two same elements
        if(ara1[i]==ara1[j]){
            for(k=j;k<(m+n-1);k++){
                ara1[k]=ara1[k+1];
            }
            n--;
            
        }
    }
}

for(i=0;i<(m+n-1);i++){
    printf("%d ",ara1[i]);
}

printf("%d",ara1[i]); //printing the last element without space ahead of it

return 0;

}

I’m getting RUNTIME ERROR in 3rd case. Please help me find where’s the problem.

I need the last loop.
Else my code won’t run

No, you don’t. Try what I told you first, then reply. Don’t reply without trying it out.

@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]

Oh! Sorry, my bad. submission
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. :smiley:
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;
}

What’s wrong in my code? Please help me.

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 wouldn’t be that quick to jump to that conclusion. :slight_smile:

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

What does your code output? I see from your latest submission:

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 :frowning:

def set_union(set1={},set2={}):
    for element in set2:
        set1.add(element)
        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))