Set Mutations | HackerRank Solutions

admin
By -
0

 



Code:

# Enter your code here. Read input from STDIN. Print output to STDOUT
a = int(input())
sa = set(map(int, input().split()))
n = int(input())

for _ in range(n):
    x,y = input().split()
    y = int(y)
    match x,y:
        case ("intersection_update", x):
            z = set(map(int, input().split()[:y]))
            sa.intersection_update(z)
            #print(sa)
        case ("update", x):
            z = set(map(int, input().split()[:y]))
            sa.update(z)
            #print(sa)
        case ("symmetric_difference_update", y):
            z = set(map(int, input().split()[:y]))
            sa.symmetric_difference_update(z)
            #print(sa)
        case ("difference_update", x):
            z = set(map(int, input().split()[:y]))
            sa.difference_update(z)
            #print(sa)
           
print(sum(sa))
       
Time Complexity:Big(O)N

The code snippet contains a loop that iterates 'n' times, where 'n' is the value input by the user. Inside the loop, there are set operations that have a time complexity of O(m), where 'm' is the size of the set being updated. Therefore, the overall time complexity is O(n * m), but since 'n' is the dominant factor, we simplify it to O(n).









Post a Comment

0Comments

Please Select Embedded Mode To show the Comment System.*