Hackerrank solution: No Idea! in Python [5 Methods]

Question: No Idea - Hacker Rank (Python)

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 . 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 set, they have no repeated elements. However, the array might contain duplicate elements.

Advertisement

Constraints:

  • 1 <= n <= 105
  • 1 <= m <= 105
  • 1 <= Any integer in the input <= 109

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.

Output Format:

Output a single integer, your total happiness.

Sample Input:

Advertisement
3 2
1 5 3
3 1
5 7

Sample Output:

1

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.

 

Possible solutions

Now, we will try to solve the question using multiple ways.

  1. Using Map and counter methods
  2. Using List comprehension
  3. Using for loop
  4. Using __name__ variable
  5. Using User-defined method

 

Solution-1: Using Map and counter methods

Python's map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop, a technique commonly known as mapping.  We will now use the Map() and counter() methods in our solution to solve the question.

# importing the counter method
from collections import Counter

# using map method for mapping
n, m = map(int, input().split())

# convetring the mapping to list
data = list(map(int, input().split()))

# using conter method
data_counter = Counter(data)
data_set = set(data)

# taking inputs from user
set_a = set(map(int, input().split()))
set_b = set(map(int, input().split()))

# initializing happiness to 0
happiness = 0

# using for loop
for i in data_set & set_a:
    happiness += data_counter[i]
for i in data_set & set_b:
    happiness -= data_counter[i]
    
# printing the output
print(happiness)

Input:

3 2
1 5 3
3 1
5 7

Output:

1

As you can see, we get the desired output.

 

Solution-2: Using List comprehension

A Python list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element in the Python list. Python List comprehension provides a much more short syntax for creating a new list based on the values of an existing list. Let us now use the list comprehension in out solution to get the desired result.

# taking input
n, m = input().split()
sc_ar = input().split()

# converting to set
A = set(input().split())
B = set(input().split())

# printing using list comprehension
print (sum([(i in A) - (i in B) for i in sc_ar]))

Input:

3 2 
1 5 3 
3 1 
5 7

Output:

1

As you can see, we have used list comprehension in our solution to get the required result.

Advertisement

 

Solution-3: Using for loop

Let us now use the for loop and find out the solution as required in the question.

# taking input from the user
a=input().split(' ')
l=([int(i) for i in input().split(' ')])

# converting to set
firstset=set([int(i) for i in input().split(' ')])
secondset=set([int(i) for i in input().split(' ')])

# initalizing with zero
p=0

# using for loop with if statements 
for i in l:
    if i in firstset:
        p+=1
    if i in secondset:
        p-=1
        
# printing
print(p)

Input:

3 2 
1 5 3 
3 1 
5 7

Output:

1

As you can see, we get the desired output as required in the question.

 

Solution-4: Using __name__ variable

In Python, the special name __main__ is used for two important constructs: the name of the top-level environment of the program, which can be checked using the __name__ == '__main__' expression. We will not use it as the solution to the question.

# using __name__ method
if __name__ == "__main__":
    
    #initliazing the happiness to 1
    happiness = 0
    
    #taking inputs from user
    n, m = map(int, input().strip().split(' '))
    arr = list(map(int, input().strip().split(' ')))
    
    #converting to set 
    good = set(map(int, input().strip().split(' ')))
    bad = set(map(int, input().strip().split(' ')))
    
    #for loops
    for i in arr:
        if i in good:
            happiness += 1
        elif i in bad:
            happiness -= 1
            
    #printing output
    print(happiness)

Input:

Advertisement
3 2
1 5 3
3 1
5 7

Output:

1

As you can see, we get the desired output as required by the question.

 

Solution-5: Using User-defined Function

Another way to solve the question is using a user-defined function. A user-defined function is a function that is defined by a user. Let us create a function that will print out the desired output.

# defining the user defined function
def main():
    #initliazing the happiness to 1
    happiness = 0
    
    #taking inputs from user
    n, m = map(int, input().strip().split(' '))
    arr = list(map(int, input().strip().split(' ')))
    
    # converting to set 
    good = set(map(int, input().strip().split(' ')))
    bad = set(map(int, input().strip().split(' ')))
    
    # for loops
    for i in arr:
        if i in good:
            happiness += 1
        elif i in bad:
            happiness -= 1
            
    #printing output
    print(happiness)
    
# calling the function
main()

Input:

3 2
1 5 3
3 1
5 7

Output:

1

We get the desired output as shown above.

Advertisement

 

Summary

In this short article, we solved the No Idea question from Hacker Rank using multiple solutions. We solved the question using the counter method, __name__ variable, and using a user-defined function.

 

References

Python counter 
Question on Hacker Rank: No Idea Solution in Python

 

Related Keywords: Hacker Rank solution for No Idea in Python, Hacker Rank solution for No Idea in Python3, No Idea Hackerrank Solution

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X