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(c)) # make unique
c.sort()
print(’ '.join(c))

I can’t understand why getting wrong answer?
Note: (c) 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))




Screenshot 2022-07-06 at 8.12.41 PM

@Ataullah 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
def make_elements_int(given_li: list):
    all_int_li = []

    for element in given_li:
        if element != " ":
            all_int_li.append(int(element))
        else:
            pass
    
    return all_int_li


def main():
    _ = str(input())
    SET_A = set(input())
    SET_B = set(input())
    
    result = sorted(make_elements_int(list(SET_A | SET_B)))
    
    print(*result, sep=" ")

if __name__ == "__main__":
    main()

It shows WA on test case 2 but I can’t find the problem can anyone help me?

The issue lies in the fact that you are creating a set from the input string, resulting in a set of individual characters instead of a set of numbers. As a consequence, the output is not as expected. For instance, given the test case:

3 3
19 13 14
1 2 3

Your code produces the output
1 2 3 4 9

whereas it should output
1 2 3 13 14 19.

To fix this problem, you can modify the code by using input().split(' ') instead of input() to split the input string into a list of strings based on the space character. This will result in a list of numbers that can be used to create a set of distinct values.

For more information on how to take multiple inputs from the user in Python, you can refer to the following resources:

  1. https://www.geeksforgeeks.org/taking-multiple-inputs-from-user-in-python
  2. 5 Ways of Taking Input in Python | Toph Tutorials

Good luck with your coding endeavors!

2 Likes

Thanks, for the information. While I did solve the problem later on, the answer might help others, who are also having similar problems :slight_smile:

1 Like