Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank No Idea! problem solution in Python

YASH PAL, 31 July 2024

In this HackerRank No Idea! problem solution in Python 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∈A, you add 1 to your happiness. If i∈B, you add -1 to your happiness. Otherwise, your happiness does not change. Output your final happiness at the end.

HackerRank No Idea! solution in Python

Problem solution in Python 2 programming.

import sys

def main():
    h = Happiness()
    input = get_input()
    m = input["m"]
    n = input["n"]
    n_array = input["n_array"]
    A_set = input["A_set"]
    B_set = input["B_set"]
    for num in n_array:
        if num in A_set:
            h.incr()
        elif num in B_set:
            h.decr()
    print h.val

class Happiness():
    def __init__(self):
        self.val = 0
    def incr(self):
        self.val += 1
    def decr(self):
        self.val -= 1

def get_input():
        input_m_n = sys.stdin.readline().strip()
        input_n_array = sys.stdin.readline().strip()
        input_A_set = sys.stdin.readline().strip()
        input_B_set = sys.stdin.readline().strip()
        m, n = input_m_n.split(' ')
        n_array = input_n_array.split(' ')
        A_set = set(input_A_set.split(' '))
        B_set = set(input_B_set.split(' '))
        result = {"m":m, "n":n, "n_array": n_array, "A_set": A_set, "B_set": B_set}
        return result

if __name__ == '__main__':
    main()

Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
n, m = input().split()

sc_ar = input().split()

A = set(input().split())
B = set(input().split())
print(sum([(i in A) - (i in B) for i in sc_ar]))

Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
n, m = raw_input().split()

arr = raw_input().split()

A = set(raw_input().split())
B = set(raw_input().split())
print sum([(i in A) - (i in B) for i in arr])

Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
numlst =input().split()
l = input().split()
A = set(input().split())
B = set(input().split())
print(len(list(filter(lambda x: x in A, l))) - len(list(filter(lambda x: x in B, l))))

coding problems hackerrank solutions python

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes