Iterables and Iterators - HackerRank Solution Python
Tags: python, programming, algorithms, coding
Published: 2023-09-09 03:28:11

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

{% gist https://gist.github.com/codeperfectplus/ff4c68e1cc76b791a175cc191d8443f5.js %}

## 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](https://www.hackerrank.com/domains/python), and the solutions are implemented by **CodePerfectPlus** team_

## Other Article By Author

- [HackerRank Solve Python Algorithm Series](https://codeperfectplus.com/category/Solve-python)
- [HackerRank Problem Solving Algorithm Series](https://codeperfectplus.com/category/Problem-solving)
- [30 Days of Code HackerRank](https://codeperfectplus.com/category/Hackerank-30-days-of-code)
- [10 Days of JavaScript HackerRank](https://codeperfectplus.com/category/10-days-of-javascript)

### [30 Days of Code SubReddit](https://www.reddit.com/r/30daysofcode)