There is an array of integers. There are also disjoint sets, and, each containing integers. You like all the integers in the set and dislike all the integers in the 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.
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.
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 a unit of happiness for elements and in the set . You lose unit for in a set . The element in set does not exist in the array so it is not included in the calculation.
Hence, the total happiness is.
No Idea! - Hacker Rank Solution
PENDING
count = list(map(int,raw_input().strip().split()))
ReplyDeletem = count[0]
n = count[1]
Arr = list(map(int,raw_input().strip().split()))
array = set(map(int,raw_input().strip().split()))
array2 = set(map(int,raw_input().strip().split()))
points = 0
for i in Arr:
if i in array : points = points + 1
elif i in array2 : points = points - 1
print points
i found it at this webpage: https://mayuriwadkar.blogspot.com/2016/08/hackerrank-no-idea-python.html
& it works perfectly!
print("*********sets play ground*********")
ReplyDeleteprint("enter the numbers 1. array list 2 sets numbers")
m = input("array max lenght >")
n = input ("number of elemets in sets")
m = int(m)
n = int(n)
print( f"enter the elements in array {m}")
arrayvalues = []
for i in range(0,m):
ele = int(input("enter element"))
arrayvalues.append(ele)
set1 = []
set2 = []
for i in range(0,n):
ele = int(input("enter the element in set1 >"))
set1.append(ele)
for i in range(0,n):
ele = int(input("enter the element in set2 >"))
set2.append(ele)
s = set(arrayvalues)
t = set(set1)
y = set(set2)
print(s)
print(t)
print(y)
z = s.intersection(t)
x = s.intersection(y)
print(z)
print(x)
print("happiness is >")
print(len(z)-len(x))
Python3 code :
ReplyDeleten,m = list(map(int,input().split()))
happiness = 0
arr = list()
A,B = set(),set()
arr = list(map(int,input().strip().split()))
A = set(map(int,input().strip().split()))
B = set(map(int,input().strip().split()))
for i in arr:
if i in A:
happiness += 1
elif i in B:
happiness -= 1
print(happiness)
Updated :
Deleten,m = list(map(int,input().split()))
happiness = 0
arr = list(map(int,input().strip().split()))
A = set(map(int,input().strip().split()))
B = set(map(int,input().strip().split()))
for i in arr:
if i in A:
happiness += 1
elif i in B:
happiness -= 1
print(happiness)
n,m = input().split()
ReplyDeletearr = input().split()
A = set(input().split())
B = set(input().split())
print(sum([(i in A)-(i in B) for i in arr]))