No Idea - Python HackerRank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank No Idea Python Problem with a very easy explanation. In this article, you will get one or more approaches to solving this problem. So let's start-
{tocify} $title={Table of Contents}
HackerRank Python No Idea Solution - Problem Statement
Task
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 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.
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.
Constraints
1 <= n <= 10^5
1 <= m <= 10^5
1 <= Any integer in the input <= 10^9
Output Format:
Output a single integer, your total happiness.
Sample Input 0
3 2 1 5 3 3 1 5 7 {codeBox}
Sample Output 0
1 {codeBox}
Explanation 0
You gain 1 unit of happiness for elements 3 and 1 in set A. You lose 1 unit for 5 in set B. 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.
Python No Idea - Hacker Rank Solution
1st Approach: No Idea HackerRank Python Solution
# ========================
# Information
# ========================
# Name: No Idea! in Python HackerRank
# Direct Link: https://www.hackerrank.com/challenges/no-idea/problem
# Difficulty: Medium
# Max Score: 50
# Language: Pypy 3
# ========================
# Solution Start
# ========================
# No Idea! in Python - Hacker Rank Solution
# 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)
#No Idea! in Python - Hacker Rank Solution END
# MyEduWaves
Disclaimer: The above Problem ( Python No Idea ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.
I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python No Idea Hackerrank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Python