No Idea! in Python | HackerRank Solution

Hello coders, today we are going to solve No Idea! HackerRank Solution in Python.

No Idea!

Task

There is an array of n integers. There are also 2 disjoint setsA 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 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.

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 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 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.

Solution – No Idea! in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
if __name__ == "__main__":
    happiness = 0
    n, m = map(int, input().strip().split(' '))
    arr = list(map(int, input().strip().split(' ')))
    
    good = set(map(int, input().strip().split(' ')))
    bad = set(map(int, input().strip().split(' ')))
    
    for i in arr:
        if i in good:
            happiness += 1
        elif i in bad:
            happiness -= 1
    print(happiness)

Disclaimer: The above Problem (No Idea!) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

2 thoughts on “No Idea! in Python | HackerRank Solution”

  1. Sudam Sekkhar

    #program using simple steps
    n=int(input(“enter array size”))
    m= int(input(“enter set size”))
    arr=[]
    A=set()
    B=set()
    for i in range(0,n):
    ele= input()
    arr.append(ele)
    for j in range (0,m):
    ele=input()
    A.add(ele)
    for j in range (0,m):
    ele=input()
    B.add(ele)
    print(arr,A,B)
    happiness=0
    for i in arr:
    if i in A:
    happiness+=1
    elif i in B:
    happiness-=1
    else:
    happiness=0
    print(happiness)

Leave a Comment

Your email address will not be published. Required fields are marked *