Sum and Prod HackerRank Solution

This Python program uses the numpy library to perform operations on arrays. It first takes two inputs, n and m, which represent the dimensions of the array. The program then takes n lines of input, each containing m integers, and stores them in a 2D array. It then calculates the sum of the elements along the 0th axis (i.e., column-wise) and finally calculates the product of these sums.

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 Sum and Prod HackerRank Solution HackerRank solution using CodeRankGPT:

The solution works by first converting the input list into a numpy array. Numpy arrays are more efficient and provide more functionality for numerical operations than regular Python lists. The numpy.sum function is then used to calculate the sum of elements along the 0th axis (column-wise). The result is another numpy array containing the sums. The numpy.prod function is then used to calculate the product of these sums. The result is printed to the console.


import numpy

n, m = map(int, input().split())
ar = []
for _ in range(n):
    tmp = list(map(int, input().split()))
    ar.append(tmp)
np_ar = numpy.array(ar)
s = numpy.sum(np_ar, axis=0)
print(numpy.prod(s))

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