Note: Since A and B are set, they have no repeated elements. However, the array might contain duplicate elements.
Constraints:
- 1 <= n <= 105
- 1 <= m <= 105
- 1 <= Any integer in the input <= 109
Input Format:
- The first line contains integers n and m separated by a space.
- The second line contains n integers, the elements of the array.
- The third and fourth lines contain m integers, A and B, respectively.
- Output a single integer, your total happiness.
3 2
1 5 3
3 1
5 7
Sample Output:1
Explanation:- You gain 1 unit of happiness for elements 3 and 1 in set A. You lose 1 unit for 5 in set B. The element 7 in set B does not exist in the array so it is not included in the calculation.
- Hence, the total happiness is 2-1=1.
n,m = input().split()
arr = input().split()
A = set(input().split())
B = set(input().split())
print(sum([(i in A) - (i in B) for i in arr]))
Disclaimer: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!
No comments:
Post a Comment