HackerRankInterview QuestionsPythonPython InterviewRandom

HackerRank No Idea! problem solution in Python | python question solution

There is an array of  integers. There are also  disjoint sets and , each containing  integers. You like all the integers in set  and dislike all the integers in set . Your initial happiness is . For each  integer in the array, if , you add  to your happiness. If , you add  to your happiness. Otherwise, your happiness does not change. Output your final happiness at the end.

Note: Since  and  are sets, they have no repeated elements. However, the array might contain duplicate elements.

Constraints


Input Format

The first line contains integers  and  separated by a space.
The second line contains  integers, the elements of the array.
The third and fourth lines contain  integers,  and , 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  unit of happiness for elements  and  in set . You lose  unit for  in set . The element  in set  does not exist in the array so it is not included in the calculation.

Hence, the total happiness is .

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

Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science