No Idea!

  • + 99 comments

    List comprehensions can do this easily, esp when you leverage that True == 1 and False == 0:

    n, m = raw_input().split()
    
    sc_ar = raw_input().split()
    
    A = set(raw_input().split())
    B = set(raw_input().split())
    print sum([(i in A) - (i in B) for i in sc_ar])
    
    • + 2 comments
      [deleted]
      • + 2 comments

        here is problem solution in python programming. https://programs.programmingoneonone.com/2021/01/hackerrank-no-idea-solution-python.html

        • + 0 comments

          Here's my clean and elegant solution. Slightly different approach. Explanation added

          HackerRank No Idea! Solution

        • + 0 comments

          Updated solution is here

          https://www.thecscience.com/2021/08/HackerRank-No-Idea-in-python-problem-solution.html

      • + 5 comments

        I dont know why 5 test cases are wrong.kindly help

        mystr=input()#Taking n.m no value for me so discard

        mystr=input()

        mystr=mystr.rstrip()

        myset=set(mystr.split())

        mystr=input()

        mystr=mystr.rstrip()

        mysetA=set(mystr.split())

        mystr=input()

        mystr=mystr.rstrip()

        mysetB=set(mystr.split())

        x = myset.intersection(mysetA)

        xlen=len(x)

        x = myset.intersection(mysetB)

        ylen=len(x)

        happiness=abs(xlen-ylen) print(happiness)

        • + 3 comments

          dont forget that the first array contains duplicate elements, when we put the array in set we negligate the duplicate elements.

          • + 1 comment

            Now Works perfectly....

            mystr=input()

            n,m=mystr.split()

            mystr=input()

            mystr=mystr.rstrip()

            myset=mystr.split()

            mystr=input()

            mystr=mystr.rstrip()

            mysetA=set(mystr.split())

            mystr=input()

            mystr=mystr.rstrip()

            mysetB=set(mystr.split())

            happiness=0

            for i in myset:

            if i in mysetA:
            
                happiness=happiness+1
            elif i in mysetB:
                happiness=happiness-1
            

            print(happiness)

            • + 1 comment

              Why did you use rstrip?

              • + 0 comments

                It is because when u take input in form of string there is a possiblilty of adding whitespace at end, if that happens then split will also make a list of white space and further it would be bad for us to debug, therefore to avoid this we use rstrip() to remove any whitespaces at the end of stiring. Hope it helps. :)

          • + 0 comments

            so we can't solve using set operations??? like by using intersection operator in this Problem because if we want to solve by set operator we need to convert main input into set

          • + 1 comment

            bro can u please tell me what difference does set makes, why not simple array or list as we r already told that both sets are disjoint

            • + 0 comments

              bro u have to use set for getting unique element ,list can't do that if u use list there is issue of duplicate element. disjoint set means there is no common element in two sets. there is no meaning of disjoint without set cause disjoint is a property of set not list. for e.g if your input is like: A = 3, 4, 4, 2, 7, 2 if u make list of it then size is 6 but there is only 4 unique element but if u make set of it u get 4 different element in size of 4. I hope u got the point if u have still any doublt then say.

        • + 0 comments

          i think its execution time limit.my code is also this lengthy and that;s why it showed me termination error in time.have to reduce the process of execution...

        • + 2 comments

          I think time limit might be the problem. This is a simple way to solve it:

          n,m = input().split() count = 0 array = input().split() set_a,set_b = set(input().split()),set(input().split()) for i in array: if i in set_a and i not in set_b: count += 1 if i in set_b and i not in set_a: count -= 1 print(count)

          • + 0 comments

            there is no use of keeping "and" in "if" statement, cuz setA and setB are already disjoint

          • + 0 comments

            You nailed it bro......Super

        • + 0 comments

          yeah I too face the same problem

        • + 0 comments

          print the intersection result if array have duplicate elements u can see result have only non repeated elements

    • + 2 comments

      this is insanely cool solution

      • + 0 comments

        kasam se

      • + 0 comments

        yes, not intuitive and intriguing how this person thought of it this way

    • + 2 comments

      hi could you please explain the sum([(i in A) - (i in B) for i in sc_ar]) part? does it match i (element in array against a and b and keep the count?)

      • + 0 comments

        No it will simply create a list of 0's and 1's then sum will be taken.

      • + 0 comments

        it will check for each element in sc_ar, if it's present in A count will be incremented. Similarly in B, then count(A) and count(B) will be subtracted.

    • + 1 comment

      If you want, at least in Pyhton 3, you can avoid the creation of the list inside of the sum((i in A) - (i in B) for i in sc_ar) .

      • + 0 comments

        can also do it in python 2 :D

    • + 1 comment

      Awesome solution, but you don't really need to sum list.

      It is enough to sum generator expression which may be defined with no parentheses in this context:

      print sum( (i in A) - (i in B) for i in sc_ar ) (don't mind whitespaces inside of sum() )

      It leads to little economy of time because we don't waste it on creating list and storing it at some temporary place and only then summing elements of it.

      Instead, we calculate each element on-the-fly, only when we need it

      • + 2 comments

        I agree with you about the "economy of time" becuase, my solution is below:

        happiness = 0
        for i in range(0,m):    
            happiness += N.count(A[i])
            happiness -= N.count(B[i])
        print(happiness)
        

        For some of the bigger test cases I was getting a time out error.

        @ccjav; your solution is really efficient.

        • + 18 comments

          The time out error was because you weren't explicitly making A and B as sets.The inputs have duplicate values in A and B and we need to remove them. My code was also facing the same problem but now it's fixed.Have a look at this-

          n,m=map(int,input().split())
          l=input().split(' ')
          A=set(input().split(' '))
          B=set(input().split(' '))
          happiness=0
          
          for i in l:
              if i in A:
                  happiness+=1
              if i in B:
                  happiness-=1
          print(happiness)
          

          I agree that it's complexity is O(n) which is greater than O(m) and it takes one temperory variable.

          • + 4 comments

            this is basically the same solution I came up with but still getting an error(p.s I know I could be more efficient)

            ch = list(map(int, input().split()))
            
            arr = list(map(int, input().split()))
            
            A = set(map(int, input().split()))
            B = set(map(int, input().split()))
            
            happiness = 0
            
            
            for i in arr:
                count = 0
                if i in A:
                    count +=1
                    happiness = happiness + count
            
                elif i in B:
                    count -=1
                    happiness = happiness + count
            
                else:
                    count=0
            return (happiness)
            
            • + 1 comment

              You have to provide some parameter to split() bif.It's default value is None so it won't work. The input is space-seperated so you have to call split(' ') on the input()! Just refer my code above!

              Why the extra variable count?? You could just increment and decrement happiness directly.

              • + 5 comments

                Yeah I know, I couldnt figure out why it was not taking my code so I added the variable. I literally copied you code and still didn't go through

                n,m=map(int,input().split())
                l=input().split(' ')
                A=set(input().split(' '))
                B=set(input().split(' '))
                happiness=0
                for i in l:
                    if i in A:
                        happiness+=1
                    if i in B:
                        happiness-=1
                        
                print(happiness)
                
                • + 1 comment

                  It's working for me though. What is the error you are getting?

                  • + 0 comments

                    WOW! I had the def NoIdea: in my code and it didn't go through nor tell me that was the issue Confidence restored but gosh those are hours I won't get back

                • + 0 comments

                  you are not using n and m, so just do input()

                • + 0 comments

                  I had a similar solutions and also it took too long. So finally I changed from iterating over the entire list for every happy and every sad number to a counter dictionary like this:

                  from collections import Counter
                  
                  n_and_m = input() # i don't use n or m
                  
                  nums = Counter(input().split(' '))
                  
                  get_nums = lambda inp: [n for n in inp.split(' ')]
                  pos = get_nums(input())
                  neg = get_nums(input())
                  
                  happiness = sum(
                  	[nums.get(num, 0) for num in pos]
                  	+ [-nums.get(num, 0) for num in neg]
                  	)
                  
                  print(happiness)
                  
                • + 0 comments

                  your answer doesn't fullfill all the requirement of the question. Please read the input format again. If there is no usuage of n and m then why you inputted it.

                • + 1 comment

                  my solution is also same but time limit is exceeds

                  • + 0 comments

                    Check your solution again, you must be using list(set(map(int,a))) somewhere. Instead of that use set(map(int,a)).

            • + 1 comment

              Donot use count = 0 in else statement instead use count = count

              • + 0 comments

                correct

            • + 0 comments

              what is "ch" here ?

            • + 0 comments

              Dude, i can be present in both A and B. So, use 'if' instead of 'elif'.

          • + 3 comments

            I came up with the similar solution , but I got timeout error ! The second thing I want to ask is , why are we providing space " " between the split() . We have given I/Ps to other problems in this python challenge , does this space really matters??

            PS : I am a beginner level 0 to be precise !

            n,m = map(int, raw_input().split())
            arr = map(int, raw_input().split())
            firstSet = map(int, raw_input().split())
            secSet = map(int, raw_input().split())
            count =0
            score =0
            for i in arr:
                if i in firstSet:
                    count+=1
                elif i in SecSet:
                    score+=1
            print count-score
            
            • + 1 comment

              Refer python documentation for split() function... It's default value is None! Also the inputs for firstSet and secSet are humungous lists!You need to convert them to sets. This will solve the timeout problem. See my code,you'll get the idea.

              • + 0 comments

                It helps me , thank you!

            • + 0 comments

              make first and second set using A=set(map(int,input().split())) B=set(map(int,input().split()))

            • + 0 comments

              Use firstSet = set(map(int,firstSet)) after taking firstSet as input.

          • + 1 comment

            heyy can you explain what line 1,2,3,4 does

            • + 0 comments

              n,m = map(int, raw_input().split())

              ( In this user will enter values,1st value will be assigned to n and 2nd value will be assigned to m)

              arr = map(int, raw_input().split()) (In this all values user enters in a line(along with a space) will be entered into a group called arr..you can convert that arr into list or set afterwards)

          • [deleted]
            + 0 comments

            Well, I don't think there was any use of n,m.

          • + 0 comments

            Their question is hugely misleading, they explicitly claimed that A & B are sets, which made me think that the inputs are already unique...

            Thanks for the tip though

          • + 0 comments

            They explicitly mentioned A and B are two disjoint sets and so it didn't occur to me that the data set will contain comon items between A and B.

          • + 1 comment

            I wrote the exact same code but instead of declaring A & B as sets, I declared them as lists. The code failed for 4 test cases because of timeout. Can someone explain the reason behind this?

            • + 0 comments

              The test cases are stress tests for your code. which means they supply VERY long array of inputs. when you use list instead of set, you are handling all the inputs instead of cutting them short with a set function and by thus reducing caculation time. This it also why some of the for loops are failing and timeout. The time complexity of a loop to go through the array and check every integer if intersect with another array is a non effiecient coding.

          • + 1 comment

            I did face the same problem of time running out. Initially I did use A and B as lists rather than sets. Now that I use it as sets the problem seems to be resolved. Can you let me know the difference

            • + 0 comments

              I'm not quite sure why your solution got faster. I means a set only containes unique items. But I think A and B did only have unique items anyway. Do you want to show your code?

          • + 0 comments

            I thought my input wasnt getting accepted as when i printed the setA it was empty

          • + 0 comments

            but can tell me that why are we using the first line as it seems no need of that

          • + 0 comments

            best code!! with clear understanding. thanxs man

          • + 0 comments

            The question they had asked is very confusing, Hacker rank sataements are ambigious , though your solution helped.

          • + 0 comments

            How would you notified bro.my code also exactly similar to you but in code instead of set I placed a list."Your analysis is great how do you do that?"

          • + 1 comment

            I used the same method but I am getting run time error.

            Edit - I was using list(set(map(int,input().split()))) earlier. Just using set rectifies run time error problem

            • + 0 comments

              dont use list, the access time of list is little more than the set, due to which some testcases will not work properly. try to use set only. And if you are coding in python 2 then use raw_input. this will improve the efficiency of the code.

          • + 1 comment

            Could you elaborate a bit more on timeout issue, why is it happeing when calling list intstead of set on A and B? Here's my code that gave error

            ar = list(map(int, input().rstrip().split())) A = list(map(int, input().rstrip().split())) B = list(map(int, input().rstrip().split()))

            rest of the code is same as above.

            • + 0 comments

              Because set have a proporty to removes the same elements in a list

        • + 0 comments

          My algorithm is similar and timed out as well.

    • + 1 comment

      why do we have to right the first line ? becoz its not used later on this program..

      • + 0 comments

        In this case he had to read the values to get so that he could read in the later ones, since they are inputted in a specific order. It seems that the problem can be solved without n and m, but their inclusion makes finding a solution easier for beginners.

    • + 0 comments

      nice solution man

    • + 0 comments

      This comment changed because I missed something. Now that I see how it works, this is a very cool solution.

    • + 4 comments

      arr_set_size = input().split() arr = input().split() set1 = set(map(int, input().split())) set2 = set(map(int, input().split())) print(sum([(e in set1) - (e in set2) for e in arr])) i wrote the same code but its still showing 0 instead of 1 for given input 3 2 1 5 3 3 1 5 7

      whats wrong with the code

      • + 1 comment

        why are you using .split() for for single input e.g array_set_size

        • + 0 comments

          there are 2 inputs in 1st line size of list and size of set (n and m)

      • + 1 comment

        Hi buddy, [(e in set1) - (e in set2) for e in arr] produces [0,0,0] so you are getting result o. use set1 = set(input().split()) instead of set(map(int, input().split())) this will solve your problem

        • + 1 comment

          Hi can you explain this, why this set(map(int, input().split())) gave a wrong answer

          • + 0 comments

            Hi, we are using map only on set1 and set2 but not on array. I doubt this was causing the error.[(e in set1) - (e in set2) for e in arr] in this code we are getting false(0). You can use map on all three inputs(set1,set2 and array) which would give you desired result.

      • + 0 comments

        @AnishKumarBojan, @sristi_gupta92

        This line will create a list with strings

        arr = input().split()
        

        The following lines create sets with integers.

        set1 = set(map(int, input().split()))
        set2 = set(map(int, input().split()))
        

        Their types are different. Thefore (e in set1) gives false every time. In order to fix it change

        arr = input().split() to 
        arr = list(map(int, input().split()))
        

        or

        set1 = set(input().split())
        set2 = set(input().split())
        

        Both way would work same.

      • + 0 comments

        you are using map in sets that is elements of sets are integers but there is no map in arr that means the elements of array are still strings that's why your answer is 0(integers are being compared with strings) use map in arr = map(int,input().split()) and everything will be fine BTW Nice Beautiful Code Well Done

    • + 0 comments

      if i am changing the first line to map(int,input().split()), it is showing me the wrong answer. why is it so?

    • + 0 comments

      I did a for loop similar to this, but it timed out for some of the test case. Is there nay more optimal solution.

    • + 0 comments

      it is still showing terminated due to timeout

    • + 1 comment

      Would creating sets C = A - B and D = B - A save time by skipping cases where i is in A ( + 1) and in B ( - 1)?

      • + 0 comments

        I don't think so, as we already know the sets are disjoint. It's more of a matter of finding out if a number belongs exclusively to one or the other set. It'll never be both.

    • [deleted]
      + 0 comments

      This is awesome!

    • + 0 comments

      absolute class man the point u have to play with list compression to get best out answer

    • + 0 comments

      You are awesome man. Really nice and easy solution.

    • + 0 comments

      Can anyone explain this line n, m = raw_input().split()?

    • + 0 comments

      Awesome solution.

    • + 0 comments

      Beautiful "True-False == 1".

    • + 1 comment
      [deleted]
      • + 0 comments

        See this python wiki on time complexity for information that you may find helpful. Sets, which use hashing, are needed to get a O(1) runtime on the 'in' operation. The other approach you used would linearly search through the entire collection before returning a false value for 'i in A'.

    • + 0 comments

      But the for and the sum are two separate loops.

      	h = 0
      	for i in sc_ar:
      		h += ((i in A) - (i in B))
      	print(h)
      
    • + 0 comments

      I'm curious whether my solution is appreciably less efficient or just more verbose than @ccjav's. I also used Booleans as 1 and 0:

      n, m = map(int, input().split())
      array = list(map(int, input().split()))
      a = set(map(int, input().split()))
      b = set(map(int, input().split()))
      
      happiness = 0
      for elem in array:
          happiness += elem in a
          happiness -= elem in b
      print(happiness)
      
    • + 0 comments

      happy = 0 n, m = map(int, input().split()) array = list(set(map(int, input().split()))) a = list(map(int, input().split())) b = list(map(int, input().split())) temp = a-b for x in set(array): if x in a: happy+=1 if x in b: happy-=1 print(happy)

      This is my code. How come yours is taking less time and mine is taking more time and hence getting time out

    • + 1 comment

      One thing I learn here is that, set is so much faster than list. Because set uses hash whereas list not. Thanks

      • + 0 comments

        Thanks for this comment. It worked ! My solution passed all test cases n,m=input().split() arr=list(map(int,input().split(' '))) A=set(map(int,input().split(' '))) B=set(map(int,input().split(' '))) h=0 for i in arr: if i in A: h=h+1 if i in B: h=h-1 print(h)

    • + 1 comment

      uber cool! I came up with something similar, i did not know that True equated to 1 in python

      print(sum([1 for x in inputArr if x in arrA])-sum([1 for x in inputArr if x in arrB]))
      
      • + 0 comments

        got wrong answer

    • + 1 comment

      How to input the arrays in python 2?

      • + 0 comments

        instead of input().split() write raw_input().split()

    • + 1 comment

      Thanks @ccjav for your code, but could you help me to fix what is wrong with this, few of the test cases are failing.

      n, m = map(int, input().split())

      t_set = set(input().split())

      s_1, s_2 = (set(input().split()) for _ in range(2))

      print(sum([(_ in s_1) - (_ in s_2) for _ in t_set]))

      • + 0 comments

        The integer array in the second line might contain duplicates.

        You basically get rid of duplicates by converting it to a set, which leads to a wrong solution.

    • + 0 comments

      Test cases having large inputs are getting failes

    • + 0 comments

      Excellent example. Thank you for making it so conscise.

      Just a quick note - the raw_input() is only in Python 2.x, this has been renamed to input() for Python3

    • + 0 comments

      This solution times out for me.

    • + 0 comments

      I just loved it. I did pretty much the same but it is way cooler.

    • + 0 comments

      this was godmode turned on all the way, anyone can explain the set() over there? I had this implemented in a much longer code but it was working, only difference against this solution was the set.

      I was getting timeout errors on the longer inputs.

    • + 1 comment

      Can you help with my code? Last 5 Test cases are failing -


      n, m = map(int, input().split())

      arr = list(map(int, input().split()))

      A = set(map(int, input().split()))

      B = set(map(int, input().split()))

      happiness = 0

      happiness += len(A.intersection(arr))

      happiness -= len(B.intersection(arr))

      print(happiness)


      Thanks in advance.

      • + 1 comment

        It's because duplicates exist in the 'arr' array which must be counted in happiness as well.

        • + 0 comments

          Thanks bro . I was doing similar thing

          n_m = input().split() n_m = map(int, n_m) N = set(map(int, input().split())) A = set(map(int, input().split())) B = set(map(int, input().split()))

          print(len(A.intersection(N))- len(B.intersection(N)))

    • + 0 comments

      Cool solution but doesn't really use sets. (The sets used in this case could easily be replaced by lists)

    • + 0 comments

      where you used n and m?

    • + 0 comments

      This solution rocks dude !!!

    • + 1 comment

      sum([(i in A) - (i in B) for i in sc_ar])

      what does this function means..?

      • + 0 comments

        i in A return a boolean value, i.e. 1 for true and 0 for false. It is just adding 1 for all occurence of elements of sc_ar in A and -1 for B.

    • + 0 comments

      why did you use set for A and B? Is not it already given that, there won't be any repeating element in A and B? By the way, my code, which is same as above except the set part, got TLE. I am confused, if there is a problem with the test cases or, I am missing something?

    • + 0 comments

      Could you please explain the use of variable n, m here ? Seem like they are useless in your code

    • + 1 comment

      firstline=str(input()) happiness=0 arrset=set(input().split()) setA = set(input().split()) setB = set(input().split()) happiness += len(arrset.intersection(setA)) happiness -= len(arrset.intersection(setB)) print(happiness)

      This is my code it works fine for three test cases when input gets highers i am not able to store all the values in my arrset that's your "sc_ar" set for example in input i give 1000 values and i dont know y my length of arrset is only 916 ! please help me by telling where i went wrong

      • + 0 comments

        arrset cant be a set because it contains duplicates, changing it into a set will remove the dupes causing the lesser amount of values in arrset

    • + 0 comments

      forget the limits..

    • + 0 comments

      Cool solution! what if we print instead: print(len(sc_ar.intersection(A))-len(sc_ar.intersection(B)))

    • + 0 comments

      The last 5 test cases were failed after running?Where is the use of getting n and m as inputs,in this code?

    • + 0 comments

      Brother, Please tell how to compute Time complexity of your code,a rough idea: membership operator has worst case complexity O(n) ,so for each value in array, you intend to traverse both the sets (using in oprator).So, basically O(n*m),right,Please Rectify if i'm wrong

    • + 0 comments

      This is not good solluation. Please tell us a right solluation if you know.

    • + 0 comments

      how this answer fullfills below reqiurement: 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.

    • + 0 comments

      I had no idea that True - False gives 1 as answer. Cool solution!

    • + 0 comments

      Not working for all test cases

    • + 0 comments

      YOU KILLED IT BRUHH..HATS OFFF

    • + 0 comments

      GOD

    • + 0 comments

      n, m = input().split()

      (sc_ar, A, B) = [set (map(int,input().split())) for _ in range (3)]

      print (sum([(i in A) - (i in B) for i in sc_ar]))

    • + 1 comment

      Isn't it more efficient to use a for loop? Because sets A and B are disjoint, if you find i in A then you don't need to check it in B. I don't really know list comprehesions, so maybe I'm missing something.

      n, m = input().split()
      array = input().split()
      
      pos = set(input().split())
      neg = set(input().split())
      
      happy = 0
      for a in array:
          if a in pos:
              happy += 1
          elif a in neg:
              happy -= 1
      
      print(happy)
      
      • + 2 comments

        i use the same code but it gives timed out for some test cases how could your passed all test cases

        • + 0 comments

          can you show me your code

    • + 0 comments

      I would have never thought of this , really beautiful.

    • + 1 comment

      would you please explain how the last line work print sum([(i in A) - (i in B) for i in sc_ar]) or any alternative of this

      • + 0 comments

        (i in A) and (i in B) checks for the number i in sets A and B and returns True and False for each check. "For i in sc_ar" goes through each number in sc_ar. Since True = 1 and False = 0 in python, (i in A) - (i in B) will be the net happiness derived from each number in sc_ar, since A adds happiness and B subtracts happiness. This solution thus adds them all in a list via [] (list comprehension) then proceeds to sum all the items in the list (which are all 1 or -1 for each number) to get the net solution.

    • + 1 comment

      n, m = raw_input().split()

      sc_ar = raw_input().split()

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

      can you explain why you use set in A and B i use without set not all test cases are passed

      • + 0 comments

        Time Complexity of searching an element in a set is O(1) in average case and for searching in a list is O(n) in average case while in worst case, it is O(n) for both the data structures. So we can say on an average, it will take less time when we use set in comparison to list. I am attaching a link which will help you in comparing cost of various operations in Python. https://wiki.python.org/moin/TimeComplexity

    • + 0 comments

      can you please explain this line print sum([(i in A) - (i in B) for i in sc_ar]) and can you give its python 3 verion . it would be so nice of you

    • + 0 comments

      but the n and the m here is useless

    • + 0 comments

      what this line mean ??

      set(raw_input().split())

    • + 0 comments

      great

    • + 0 comments

      will it take O(n^2) or O(nlogn) ?

    • + 0 comments

      This is a cool solution but taking extra unnecessary memory.

    • + 0 comments

      wow boss my solution just showed excessive time use up

    • + 0 comments

      my code sucess in all the test cases Note-- >>> 2 line is not a set of elements (key point)

      **i,j=input().split() Arr=[int(r) for r in input().split()] a=set([int(r2) for r2 in input().split()]) b=set([int(r3) for r3 in input().split()]) a1=0

      for x in Arr: if x in a: a1+=1 elif x in b: a1-=1 print(a1) **

    • + 0 comments

      hii nice solution

    • + 0 comments

      I tried this solution but it is passing only 2 test cases. Rest all are failing.

      Even my code below is also passing same 2 test cases only. Somebody can pls help?

      n,m=input().split() arr=set(input().split()) a=set(input().split()) b=set(input().split())

      add=len(arr.intersection(a)) sub=len(arr.intersection(b))

      total=(add-sub)

      print(total)

    • + 0 comments

      for research purpose only:

      print([(i in A) for i in arr])
      print([(i in B) for i in arr])
      print([(i in A) - (i in B) for i in arr])
      print([(i in B) - (i in A) for i in arr])
      print(sum([(i in A) - (i in B) for i in arr]))
      print(sum([(i in B) - (i in A) for i in arr]))
      
    • + 1 comment

      This seems to be inefficient with python 3.x

      • + 0 comments

        yes try this problem with latest version of the python

    • + 1 comment

      These two programs are not same??

      n,m=map(int,input().split()) arr=list(map(int,input().split())) a=list(map(int,input().split())) b=list(map(int,input().split())) s=0 for i in arr: if i in a: s+=1 elif i in b: s-=1 print(s)

      • + 0 comments

        actually i got it i passed all test case because i was unaware that sets are faster than list.Sorry for the regret

    • + 0 comments

      It fails in some cases where test cases are very large. It exceeds time limit.

    • + 1 comment

      Hi, Could you please tell why following code is failing 4 test cases

      n, m = map(int,input().split())

      arr = map(int, input().split()) arr = list(map(int,arr))

      a = set(map(int, input().split())) a = list(map(int,a)) b = set(map(int, input().split())) b = list(map(int,b))

      add = 0 sub = 0

      for i in arr: if i in a: add+=1 elif i in b: sub+=1

      print(add-sub)

      • + 0 comments

        the problem is timeout not wrong answer because if you make a list , the complexity may become n*(m+m) (may be pass all indexes), a list have a positional access. but the data structure set there is no positional access , so the complixity become n https://stackoverflow.com/questions/1035008/what-is-the-difference-between-set-and-list

    • + 0 comments

      please explain me this code and problem statement also

    • + 0 comments

      How did you come up with this code?

    • + 0 comments

      simple solution to simple problem :- python 3 -> n,m = input().split() l = list(map(int,input().split())) s1 = set(map(int,input().split())) s2 = set(map(int,input().split())) c = 0 for i in l: if i in s1: c+=1 if i in s2: c-=1 print(c)

    • + 0 comments

      If you want to check items within it,using sets is more faster than lists. But somehow if you want just iterate values , using lists is slightly faster than sets .

    • + 0 comments

      Whoa! I never knew that this could be done

    • + 0 comments

      Brilliant solution!

    • + 0 comments

      Anyone plz explain me the meaning of the below line.

      print sum([(i in A) - (i in B) for i in sc_ar])

      Thanks in Advance:)

    • + 0 comments

      Updated one for python 3!!!

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

    • + 0 comments

      not argusted for repuitation of elemint in a and b

    • + 0 comments

      Thanks, I didn't know about raw_input and didn't really concieve the power of list comprehensions.

    • + 0 comments

      After 4 Years still its incredible, Mind Blowing !!

    • + 0 comments

      runtime error wrong sol

    • + 0 comments

      using list comprehension fails the time limit for me

    • + 0 comments

      If I change A and B type into list like this: A = list(raw_input().split()) B = list(raw_input().split()) Then it turned out Time out in some test cases ? Why ??

    • + 0 comments

      this code passed all test cases, this approach did'nt use list comprehension. solution in python 3

      ` len_int = list(map(int,input().split(" "))) n , m = len_int[0], len_int[1]

      like_num = list(map(int,input().split(" "))) A = set(map(int,input().split(" "))) B = set(map(int,input().split(" "))) happiness = 0

      for i in like_num: if i in A: happiness += 1 if i in B: happiness -= 1

      print(happiness)

    • + 0 comments

      But isn't that an incorrect sytnax?

    • + 0 comments

      why do we make A and B sets? if i don't make them set 4 test cases fail. just because of the set thing?

    • + 0 comments

      I tried this, but some test cases are running out of time.

    • + 0 comments

      hey why ya wanna confuse python 2 with 3... those are absurdly diff....ahhhhh

    • + 0 comments

      i didn't get what m and n is required for

    • + 0 comments

      This doesn't pass on all test cases due to performance issue.