Calculate your total happiness, which depends on the integers in the given sets I am a beginner in programming. And I am learning python as my first language. I went on to do some challenges on HackerRank and stumbled upon this problem. The problem goes like this:


  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 \$\ i \$ integer in the array, if \$\ i \in A\$, you add \$\ 1 \$
  to your happiness. If \$\ i \in B\$, you add \$\ -1\$ to your
  happiness. Otherwise, your happiness does not change. Output your
  final happiness at the end.
  
  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 line
  contains \$\ m \$ integers, of \$\ 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


Here's my attempt at the answer. My code is working fine with small inputs but when the values of \$\ n \$ and \$\ m \$ are in millions, it gives a Terminated due to timeout :( error. Please help me out with time-efficient code that will execute within the time limits.

elems_count = input().split()
my_set = input().split()
happy_set = input().split()
sad_set = input().split()

happy = 0
for i in range(int(elems_count[0])):
    if my_set[i] in happy_set:
        happy = happy + 1 
    else:
        happy = happy - 1
print(happy)
As pointed out in the comments by both MrVajid and AJNeufeld, the previous solution (now deleted because it is actually irrelevant now; see post history) actually did not work.
This is because in reality, the task requires the input array to not be a set, aka it should be allowed to have duplicate entries.

In fact, I had solved that challenge successfully a while ago, using:

n, m = [int(x) for x in input().split()]

array = [int(x) for x in input().split()]
A = {int(x) for x in input().split()}
B = {int(x) for x in input().split()}

happiness = 0

for value in array:
    change = 1 if value in A else -1 if value in B else 0
    happiness += change

print(happiness)


This is using set comprehension ({ }) and passes just fine on HackerRank.

Iteration over the input array as a list is still \$O(n)\$, see here, but containment checks for sets is constant time, \$O(1)\$. Previously, with \$A\$ and \$B\$ being lists, this was also linear as \$O(m)\$, leading to \$O(n*m)\$ aka quadratic time complexity.

Since this task on HackerRank is specifically about set usage, it will judge pedantically about time complexity, with only the approach using sets not running into timeout errors.



Notice that your solution also has an error apart from the set one.
You check for containment in happy_set and decrement happy for a negative result.
However, you first need to check for containment in sad_set first.
Otherwise, you are much sadder than required.