itertools.product() HackerRank Problem

The problem at hand involves taking two arrays as input and calculating their cartesian product. The cartesian product of two sets is a set of all ordered pairs where the first element is from the first set and the second element is from the second set. This is a common operation in many areas of computer science and mathematics.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the itertools.product() HackerRank Problem HackerRank solution using CodeRankGPT:

The solution uses Python's built-in itertools.product() function to calculate the cartesian product of the two input arrays. This function returns an iterator that produces tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The resulting tuples are emitted in lexicographic order. The script then iterates over the resulting list of tuples and prints each tuple, separated by a space.


import itertools

ar1 = list(map(int, input().split()))
ar2 = list(map(int, input().split()))
cross = list(itertools.product(ar1, ar2))
for i in cross:
    print(i, end=" ")

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform