No Idea! in python - HackerRank Solution

Problem

There is an array of n integers. There are also 2 disjoint sets, A and B, each containing m integers. You like all the integers in set A and dislike all the integers in set B. Your initial happiness is 0. For each  integer in the array, if i belongs to A, you add 1 to your happiness. If i belongs to B, you add -1 to your happiness. Otherwise, your happiness does not change. Output your final happiness at the end.

Note: Since A and B are sets, they have no repeated elements. However, the array might contain duplicate elements.


Input Format

The first line contains integers  and  separated by a space.
The second line contains  integers, the elements of the array.
The third and fourth lines contain  integers,  and , respectively.


Output Format

Output a single integer, your total happiness.


Sample Input

3 2
1 5 3
3 1
5 7


Sample Output

1


Explanation

You gain  unit of happiness for elements  and  in set . You lose  unit for  in set . The element  in set  does not exist in the array so it is not included in the calculation.

Hence, the total happiness is .


Solution in python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#solution in python - docodehere.com

n, m = map(int,input().split())
main = list(map(int,input().split()))
a = set(list(map(int,input().split())))
b = set(list(map(int,input().split())))


count=0
for i in main:
    if i in a:
        count+=1
    if i in b:
        count-=1
        
print(count)

#solution in python - docodehere.com 
Next Post Previous Post
No Comment
Add Comment
comment url