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


Hacker Rank Python

Author: Bashir Alam
Reviewer: Deepak Prasad

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

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:

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.

 

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:

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.

 

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

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

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 send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment