DEV Community

Cover image for Iterables and Iterators - HackerRank Solution Python
Deepak Raj
Deepak Raj

Posted on • Originally published at codeperfectplus.com on

Iterables and Iterators - HackerRank Solution Python

Iterables and iterators is medium level problem for python developer in hackerrank, this problem is based on itertools module in python, in this post we will see the solution of hackerrank problem iterables and iterators in python

Problem Statement and Explanation

In the given list of n lowercase English letters, you can select any k indices (assume 1-based indexing) with a uniform probability from the list. Find the probability that at least one of the k indices selected will contain the letter: a.

Input Format

  • The first line contains the integer n , the number of letters in the list.
  • The second line contains the lowercase English letters, separated by a space.
  • The third line contains “k”, the number of indices to be selected.

Output Format

  • Output a single line consisting of the probability that at least one of the “k” indices selected contains the letter: a up to 4 decimal places.

Iterables and Iterators - HackerRank Solution Python

Explanation of Solution

  • The first line imports the itertools module, which provides functions for generating combinations and permutations.

  • The find_probability() function takes two arguments: an array of strings and an integer k. The function first generates all possible combinations of k elements from the array. The itertools.combinations() function is used to generate the combinations.

  • The function then finds the total number of combinations. The len() function is used to get the length of the list of combinations.

  • The function then finds the number of combinations that satisfy the condition. The condition is that the combination must contain the letter “a”. The list() function is used to convert the generator object to a list, and the len() function is used to get the length of the list.

  • The function then finds the probability. The probability is the number of combinations that satisfy the condition divided by the total number of combinations. The round() function is used to round the probability to four decimal places.

  • The function then prints the probability.

  • The if ' __name__' == ' __main__': block is used to execute the function when the code is run as a script. The first line in the block, n = int(input()), prompts the user to enter the number of elements in the array.

  • The next line, arr = list(input().split()), splits the input string into a list of strings. The last line, find_probability(arr, k), calls the find_probability() function to find the probability.

Time Complexity of the Solution

The time complexity of the solution is O(n * k), where n is the number of elements in the array and k is the number of elements in the combination. This is because the itertools.combinations() function takes O(n * k) time to generate all possible combinations.

Space Complexity of the Solution

The space complexity of the solution is O(k), where k is the number of elements in the combination. This is because the list() function creates a list of the combinations, which takes O(k) space.

In conclusion, the time complexity of the solution is O(n * k) and the space complexity of the solution is O(k).

Problem statement is taken from Hackerrank, and the solutions are implemented by CodePerfectPlus team

Other Article By Author

30 Days of Code SubReddit

Top comments (0)