Set Union

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?

My code shows correct solution of test 1 when I run it on my compiler. But, It gives wrong answer on test 1 when I submit it. Can anyone tell me the reason?

#include <bits/stdc++.h>

using namespace std;

int main(){
int n, m, len;
cin>>n>>m;
int A[n], B[m];
for (int p = 0; p < n; p++){
cin>>A[p];
}
for (int q = 0; q < m; q++){
cin>>B[q];
}

int i = 0, j = 0;
while (i < n && j < m){
    if (A[i] < B[j]){
        cout<<A[i]<<" ";
        i++;
    }
    else if (B[j] < A[i]){
        cout<<B[j]<<" ";
        j++;
    }
    else{
        cout<<A[i]<<" ";
        i++, j++;
    }
}

/* Print remaining elements of the larger array */
while(i < n){
    cout<<A[i]<<" ";
    i++;
}
while(j < m){
    cout<<B[j]<<" ";
    j++;
}
return 0;

}

Could someone please tell me what’s wrong with this code?
N,M=map(int,input().split())
H=list(map(str,input().split()))
G=list(map(str,input().split()))
L=[]
for i in range(N):
L.append(H[i])
for i in range(M):
if G[i] not in L:
L.append(G[i])
U=sorted(L)
K=" ".join(U)
print(K)

Extra space at the end caused WA :sweat:

a, b = map(int, input().strip().split())
c, d = input().strip().split(), input().strip().split()
c += d # join
c = list(dict.fromkeys©) # make unique
c.sort()
print(’ '.join©)

I can’t understand why getting wrong answer?
Note: © is c in first bracket.

a = input()
c = input()
d = c.split()
d = set(d)
e = input()
f = e.split()
f = set(f)
g = d.union(f)
h = list(g)
h.sort()
j = ' '.join(map(str,h))
print(j)

What’s the problem?
WA in case 4, why?

1 Like

@user.660051 You are sorting strings in lexicographical order (h in h.sort() is a list of strings). You are not sorting integers.

[REDACTED]

It was accepted!
but if I validate the input, that was wrong in second test

x, y = map(int, input().split())
isValid=1
if 0<x<100 and 0<y<100:
    a = {int(num) for num in input().split(" ", x)}
    b = {int(num) for num in input().split(" ", y)}
    a=list(a)
    b=list(b)
    for i in range(len(a)):
        if i==0: continue
        elif a[i-1]>a[i]:
            isValid=0
            break
    for i in range(len(b)):
        if i==0: continue
        elif b[i-1]>b[i]:
            isValid=0
            break
        

    a=set(a)
    b=set(b)
    result= a.union(b)
    if isValid:
        print(' '.join(str(s) for s in result))




@Md.165133 Your test isn’t accurate.

Set is an unordered and unindexed collection of items in Python.

In your code here:

    a = {int(num) for num in input().split(" ", x)}
    b = {int(num) for num in input().split(" ", y)}

You are reading the integers into two sets. You are losing the original order of the elements.

thanks for your help, I solved it

1 Like