Monday, June 2, 2025

Discussion About 'Find Product' (Problem from HackerEarth)

To See All Articles About Technology: Index of Lessons in Technology

Try it out
Problem
You have been given an array A of size N consisting of positive integers. You need to find and print the product of all the number in this array Modulo 
10^9+7.

Input Format:
The first line contains a single integer N denoting the size of the array. The next line contains N space separated integers denoting the elements of the array

Output Format:
Print a single integer denoting the product of all the elements of the array Modulo 10^9 + 7.

Constraints:


Sample Input
5
1 2 3 4 5
Sample Output
120
Time Limit: 1
Memory Limit: 256
Source Limit:

Explanation
There are 5 integers to multiply. Let's store the final answer in answer variable. Since 1 is identity value for multiplication, initialize 
answer as 1.

So the process goes as follows:

answer = 1
answer = (answer * 1) % (10^9 + 7)
answer = (answer * 2) % (10^9 + 7)
answer = (answer * 3) % (10^9 + 7)
answer = (answer * 4) % (10^9 + 7)
answer = (answer * 5) % (10^9 + 7) 

The above process will yield answer as 120.

Can you give psuedo code for this?

Here is the pseudocode for the problem that calculates the product of an array modulo 109+710^9 + 7:


Pseudocode

mathematica
1. Read integer N // size of array 2. Read array A of N space-separated integers 3. Set MOD = 10^9 + 7 4. Initialize answer = 1 5. For each element x in A: answer = (answer * x) % MOD 6. Print answer

💡 Explanation

  • Step 1–2: Read input values.

  • Step 3: Define the modulo value as 109+710^9 + 7, a common modulus to avoid integer overflow.

  • Step 4: Start with answer = 1, since 1 is the identity for multiplication.

  • Step 5: Loop through each number and multiply it to answer, taking modulo after each multiplication to keep numbers manageable.

  • Step 6: Output the final result.


This pseudocode is efficient and handles the problem within the constraints. You can now convert it easily into any programming language like Python, C++, or Java. Let me know if you'd like code in a specific language!

No comments:

Post a Comment