Algorithm


Problem Name: Python - No Idea!

Problem Link: https://www.hackerrank.com/challenges/no-idea/problem?isFullScreen=true  

In this HackerRank Functions in PYTHON problem solution,

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 epsolonot A, you add  to your happiness. If i epsolonot 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.

Constraints

1 <= n <= 10**5

1 <= m <= 10**5

1 <= Any integer in the input <= 10**9

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 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 1 unit of happiness for elements 3 and 1 in set A. You lose 1 unit for 5 in set . 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.

 

 

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming


n,m=map(int,input().split())
arr=list(map(int,input().split()))
    
sets_a=set(map(int,input().split()))
    
sets_b=set(map(int,input().split()))
count=0
for i in arr:
    if i in sets_a:
        count+=1
    else:
        count-=1
print(count)
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Find Angle MBC in PYTHON solution in Hackerrank
Next
[Solved] Word Order in PYTHON solution in Hackerrank