Sunday, July 28, 2019

No Idea! - Hacker Rank Solution

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

5 comments:

  1. count = list(map(int,raw_input().strip().split()))
    m = 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!

    ReplyDelete
  2. print("*********sets play ground*********")
    print("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))

    ReplyDelete
  3. Python3 code :

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

    ReplyDelete
    Replies
    1. Updated :


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

      Delete
  4. n,m = input().split()
    arr = input().split()

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

    ReplyDelete

Powered by Blogger.